Skip to content

Commit c32f642

Browse files
committed
Add internal flag on highlighted <pre>s, to prevent them from being processed twice
1 parent 92bf2dc commit c32f642

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

lib/yard/templates/helpers/html_helper.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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 && /\bdata-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

lib/yard/templates/helpers/html_syntax_highlight_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def html_syntax_highlight_ruby_ripper(source)
3939
end
4040
output
4141
rescue Parser::ParserSyntaxError
42-
source =~ /^<span\s+class=/ ? source : h(source)
42+
h(source)
4343
end
4444

4545
def html_syntax_highlight_ruby_legacy(source)

0 commit comments

Comments
 (0)