Skip to content

Commit 6bd7802

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

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

lib/yard/templates/helpers/html_helper.rb

Lines changed: 19 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
@@ -202,6 +202,11 @@ def html_syntax_highlight(source, type = nil)
202202

203203
new_type, source = parse_lang_for_codeblock(source)
204204
type ||= new_type || :ruby
205+
206+
if type.to_s == "ruby" && source =~ /<span\s+class=/
207+
return source
208+
end
209+
205210
meth = "html_syntax_highlight_#{type}"
206211
respond_to?(meth) ? send(meth, source) : h(source)
207212
end
@@ -294,7 +299,7 @@ def link_include_object(obj)
294299

295300
# Inserts an include link while respecting inlining
296301
def insert_include(text, markup = options.markup)
297-
htmlify(text, markup).gsub(%r{\A\s*<p>|</p>\s*\Z}, '')
302+
htmlify(text, markup, true).gsub(%r{\A\s*<p>|</p>\s*\Z}, '')
298303
end
299304

300305
# (see BaseHelper#link_object)
@@ -637,20 +642,25 @@ def parse_lang_for_codeblock(source)
637642
# @param [String] html the html to search for code in
638643
# @return [String] highlighted html
639644
# @see #html_syntax_highlight
640-
def parse_codeblocks(html)
645+
def parse_codeblocks(html, internal = false)
641646
html.gsub(%r{<pre((?:\s+\w+="(?:.+?)")*)\s*>(?:\s*<code((?:\s+\w+="(?:.+?)")*)\s*>)?(.+?)(?:</code>\s*)?</pre>}m) do
642-
string = $3
647+
pre_match, code_match, string = $1, $2, $3
643648

644649
# handle !!!LANG prefix to send to html_syntax_highlight_LANG
645650
language, = parse_lang_for_codeblock(string)
646-
language ||= detect_lang_in_codeblock_attributes($1, $2)
651+
language ||= detect_lang_in_codeblock_attributes(pre_match, code_match)
647652
language ||= object.source_type
648653

649-
if options.highlight
654+
pre_attrs = [%(class="code #{language}")]
655+
656+
if options.highlight && /\bdata-highlighted="true"/ !~ pre_match
650657
string = html_syntax_highlight(CGI.unescapeHTML(string), language)
658+
pre_attrs << 'data-highlighted="true"' if internal
651659
end
652-
classes = ['code', language].compact.join(' ')
653-
%(<pre class="#{classes}"><code class="#{language}">#{string}</code></pre>)
660+
661+
pre_attrs = pre_attrs.compact.join(' ')
662+
663+
%(<pre #{pre_attrs}><code class="#{language}">#{string}</code></pre>)
654664
end
655665
end
656666

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)