@@ -54,7 +54,7 @@ def urlencode(text)
5454 # @param [Symbol] markup examples are +:markdown+, +:textile+, +:rdoc+.
5555 # To add a custom markup type, see {MarkupHelper}
5656 # @return [String] the HTML
57- def htmlify ( text , markup = options . markup )
57+ def htmlify ( text , markup = options . markup , internal : false )
5858 markup_meth = "html_markup_#{ markup } "
5959 return text unless respond_to? ( markup_meth )
6060 return "" unless text
@@ -66,7 +66,7 @@ def htmlify(text, markup = options.markup)
6666 end
6767 html = resolve_links ( html )
6868 unless [ :text , :none , :pre , :ruby ] . include? ( markup )
69- html = parse_codeblocks ( html )
69+ html = parse_codeblocks ( html , internal )
7070 end
7171 html
7272 end
@@ -294,7 +294,7 @@ def link_include_object(obj)
294294
295295 # Inserts an include link while respecting inlining
296296 def insert_include ( text , markup = options . markup )
297- htmlify ( text , markup ) . gsub ( %r{\A \s *<p>|</p>\s *\Z } , '' )
297+ htmlify ( text , markup , internal : true ) . gsub ( %r{\A \s *<p>|</p>\s *\Z } , '' )
298298 end
299299
300300 # (see BaseHelper#link_object)
@@ -637,20 +637,25 @@ def parse_lang_for_codeblock(source)
637637 # @param [String] html the html to search for code in
638638 # @return [String] highlighted html
639639 # @see #html_syntax_highlight
640- def parse_codeblocks ( html )
640+ def parse_codeblocks ( html , internal = false )
641641 html . gsub ( %r{<pre((?:\s +\w +="(?:.+?)")*)\s *>(?:\s *<code((?:\s +\w +="(?:.+?)")*)\s *>)?(.+?)(?:</code>\s *)?</pre>}m ) do
642- string = $3
642+ pre_match , code_match , string = $1 , $2 , $3
643643
644644 # handle !!!LANG prefix to send to html_syntax_highlight_LANG
645645 language , = parse_lang_for_codeblock ( string )
646- language ||= detect_lang_in_codeblock_attributes ( $1 , $2 )
646+ language ||= detect_lang_in_codeblock_attributes ( pre_match , code_match )
647647 language ||= object . source_type
648648
649- if options . highlight
649+ pre_attrs = Array %(class="code #{ language } ")
650+
651+ if options . highlight && /\b data-highlighted="true"/ !~ pre_match
650652 string = html_syntax_highlight ( CGI . unescapeHTML ( string ) , language )
653+ pre_attrs << 'data-highlighted="true"' if internal
651654 end
652- classes = [ 'code' , language ] . compact . join ( ' ' )
653- %(<pre class="#{ classes } "><code class="#{ language } ">#{ string } </code></pre>)
655+
656+ pre_attrs = pre_attrs . compact . join ( ' ' )
657+
658+ %(<pre #{ pre_attrs } ><code class="#{ language } ">#{ string } </code></pre>)
654659 end
655660 end
656661
0 commit comments