Skip to content

Commit

Permalink
Merge c7975eb into 890d4ad
Browse files Browse the repository at this point in the history
  • Loading branch information
skalee committed Aug 30, 2019
2 parents 890d4ad + c7975eb commit f17c4b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
23 changes: 20 additions & 3 deletions lib/yard/templates/helpers/html_helper.rb
Expand Up @@ -65,7 +65,7 @@ def htmlify(text, markup = options.markup)
html = html.encode(:invalid => :replace, :replace => '?')
end
html = resolve_links(html)
unless [:text, :none, :pre].include?(markup)
unless [:text, :none, :pre, :ruby].include?(markup)
html = parse_codeblocks(html)
end
html
Expand Down Expand Up @@ -628,11 +628,13 @@ def parse_lang_for_codeblock(source)
# @return [String] highlighted html
# @see #html_syntax_highlight
def parse_codeblocks(html)
html.gsub(%r{<pre\s*(?:lang="(.+?)")?>(?:\s*<code\s*(?:class="(.+?)")?\s*>)?(.+?)(?:</code>\s*)?</pre>}m) do
html.gsub(%r{<pre((?:\s+\w+="(?:.+?)")*)\s*>(?:\s*<code((?:\s+\w+="(?:.+?)")*)\s*>)?(.+?)(?:</code>\s*)?</pre>}m) do
string = $3

# handle !!!LANG prefix to send to html_syntax_highlight_LANG
language, = parse_lang_for_codeblock(string)
language ||= $1 || $2 || object.source_type
language ||= detect_lang_in_codeblock_attributes($1, $2)
language ||= object.source_type

if options.highlight
string = html_syntax_highlight(CGI.unescapeHTML(string), language)
Expand All @@ -641,6 +643,21 @@ def parse_codeblocks(html)
%(<pre class="#{classes}"><code class="#{language}">#{string}</code></pre>)
end
end

# Parses code block's HTML attributes in order to detect the programming
# language of what's enclosed in that code block.
#
# @param [String, nil] pre_html_attrs HTML attribute list of +pre+ element
# @param [String, nil] code_html_attrs HTML attribute list of +code+
# element
# @return [String, nil] detected programming language
def detect_lang_in_codeblock_attributes(pre_html_attrs, code_html_attrs)
detected = nil
detected ||= (/\bdata-lang="(.+?)"/ =~ code_html_attrs && $1)
detected ||= (/\blang="(.+?)"/ =~ pre_html_attrs && $1)
detected ||= (/\bclass="(.+?)"/ =~ code_html_attrs && $1)
detected
end
end
end
end
Expand Up @@ -51,7 +51,6 @@
end

it 'renders fenced and annotated block of Ruby code, and applies syntax highlight' do
pending "Fails due to https://github.com/lsegal/yard/issues/1239"
expect(rendered_document).to match(highlighted_ruby_regexp('x', '=', '3'))
end

Expand Down

0 comments on commit f17c4b7

Please sign in to comment.