Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move parsing of !!!LANG up one level to add lang classes to <pre> tags #479

Merged
merged 5 commits into from
Jan 31, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions lib/yard/templates/helpers/html_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ def htmlify(text, markup = options[:markup])
end
html = resolve_links(html)
html = html.gsub(/<pre\s*(?:lang="(.+?)")?>(?:\s*<code\s*(?:class="(.+?)")?\s*>)?(.+?)(?:<\/code>\s*)?<\/pre>/m) do
language = $1 || $2
string = $3
# handle !!!LANG prefix to send to html_syntax_highlight_LANG
language, _ = parse_lang_for_codeblock(string)
language ||= ($1 || $2 || object.source_type || :ruby).to_sym

string = html_syntax_highlight(CGI.unescapeHTML(string), language) unless options[:no_highlight]
classes = ['code', language].compact.join(' ')
Expand Down Expand Up @@ -128,11 +130,11 @@ def html_markup_none(text)
def html_markup_html(text)
text
end

# Highlights Ruby source. Similar to {#html_syntax_highlight}, but
# this method is meant to be called from {#htmlify} when markup is
# set to "ruby".
#
#
# @param [String] source the Ruby source
# @return [String] the highlighted HTML
# @since 0.7.0
Expand Down Expand Up @@ -160,14 +162,8 @@ def html_syntax_highlight(source, type = nil)
return "" unless source
return h(source) if options[:no_highlight]

type ||= object.source_type || :ruby

# handle !!!LANG prefix to send to html_syntax_highlight_LANG
if source =~ /\A(?:[ \t]*\r?\n)?[ \t]*!!!([\w.+-]+)[ \t]*\r?\n/
type, source = $1, $'
source = $'
end

new_type, source = parse_lang_for_codeblock(source)
type ||= new_type || object.source_type || :ruby
meth = "html_syntax_highlight_#{type}"
respond_to?(meth) ? send(meth, source) : h(source)
end
Expand Down Expand Up @@ -201,12 +197,12 @@ def resolve_links(text)
next(match[1..-1]) if escape

next(match) if name[0,1] == '|'

if name == '<a' && title =~ /href=["'](.+?)["'].*>.*<\/a>\s*(.*)\Z/
name, title = $1, $2
title = nil if title.empty?
end

if object.is_a?(String)
object
else
Expand Down Expand Up @@ -235,7 +231,7 @@ def link_file(filename, title = nil, anchor = nil)
return title || file.title unless serializer
link_url(url_for_file(file, anchor), title || file.title)
end

# (see BaseHelper#link_include_file)
def link_include_file(file)
unless file.is_a?(CodeObjects::ExtraFileObject)
Expand Down Expand Up @@ -514,6 +510,17 @@ def convert_method_to_overload(meth)
end
meth
end

# @param [String] source the source code whose language to determine
# @return [Array(Symbol, String)] the language, if any, and the remaining source
def parse_lang_for_codeblock(source)
type = nil
if source =~ /\A(?:[ \t]*\r?\n)?[ \t]*!!!([\w.+-]+)[ \t]*\r?\n/
type, source = $1.to_sym, $'
end

[type, source]
end
end
end
end
Expand Down