rubocop: fix highlight tag code style

This commit is contained in:
Anatoliy Yastreb 2016-05-24 17:03:06 +03:00
parent e02049727b
commit 41a8b10780
2 changed files with 48 additions and 35 deletions

View File

@ -56,7 +56,6 @@ AllCops:
- lib/jekyll/site.rb - lib/jekyll/site.rb
- lib/jekyll/static_file.rb - lib/jekyll/static_file.rb
- lib/jekyll/stevenson.rb - lib/jekyll/stevenson.rb
- lib/jekyll/tags/highlight.rb
- lib/jekyll/tags/include.rb - lib/jekyll/tags/include.rb
- lib/jekyll/tags/link.rb - lib/jekyll/tags/link.rb
- lib/jekyll/tags/post_url.rb - lib/jekyll/tags/post_url.rb

View File

@ -14,22 +14,9 @@ module Jekyll
super super
if markup.strip =~ SYNTAX if markup.strip =~ SYNTAX
@lang = Regexp.last_match(1).downcase @lang = Regexp.last_match(1).downcase
@highlight_options = {} @highlight_options = parse_options(Regexp.last_match(2))
if defined?(Regexp.last_match(2)) && Regexp.last_match(2) != ''
# Split along 3 possible forms -- key="<quoted list>", key=value, or key
Regexp.last_match(2).scan(/(?:\w="[^"]*"|\w=\w|\w)+/) do |opt|
key, value = opt.split('=')
# If a quoted list, convert to array
if value && value.include?("\"")
value.delete!('"')
value = value.split
end
@highlight_options[key.to_sym] = value || true
end
end
@highlight_options[:linenos] = "inline" if @highlight_options.key?(:linenos) && @highlight_options[:linenos] == true
else else
raise SyntaxError.new <<-eos raise SyntaxError, <<-eos
Syntax Error in tag 'highlight' while parsing the following markup: Syntax Error in tag 'highlight' while parsing the following markup:
#{markup} #{markup}
@ -42,15 +29,15 @@ eos
def render(context) def render(context)
prefix = context["highlighter_prefix"] || "" prefix = context["highlighter_prefix"] || ""
suffix = context["highlighter_suffix"] || "" suffix = context["highlighter_suffix"] || ""
code = super.to_s.gsub(/\A(\n|\r)+|(\n|\r)+\z/, '') code = super.to_s.gsub(/\A(\n|\r)+|(\n|\r)+\z/, "")
is_safe = !!context.registers[:site].safe is_safe = !!context.registers[:site].safe
output = output =
case context.registers[:site].highlighter case context.registers[:site].highlighter
when 'pygments' when "pygments"
render_pygments(code, is_safe) render_pygments(code, is_safe)
when 'rouge' when "rouge"
render_rouge(code) render_rouge(code)
else else
render_codehighlighter(code) render_codehighlighter(code)
@ -66,7 +53,7 @@ eos
[:startinline, opts.fetch(:startinline, nil)], [:startinline, opts.fetch(:startinline, nil)],
[:hl_lines, opts.fetch(:hl_lines, nil)], [:hl_lines, opts.fetch(:hl_lines, nil)],
[:linenos, opts.fetch(:linenos, nil)], [:linenos, opts.fetch(:linenos, nil)],
[:encoding, opts.fetch(:encoding, 'utf-8')], [:encoding, opts.fetch(:encoding, "utf-8")],
[:cssclass, opts.fetch(:cssclass, nil)] [:cssclass, opts.fetch(:cssclass, nil)]
].reject { |f| f.last.nil? }] ].reject { |f| f.last.nil? }]
else else
@ -74,8 +61,30 @@ eos
end end
end end
private
def parse_options(input)
options = {}
if defined?(input) && input != ""
# Split along 3 possible forms -- key="<quoted list>", key=value, or key
input.scan(/(?:\w="[^"]*"|\w=\w|\w)+/) do |opt|
key, value = opt.split("=")
# If a quoted list, convert to array
if value && value.include?("\"")
value.delete!('"')
value = value.split
end
options[key.to_sym] = value || true
end
end
if options.key?(:linenos) && options[:linenos] == true
options[:linenos] = "inline"
end
options
end
def render_pygments(code, is_safe) def render_pygments(code, is_safe)
Jekyll::External.require_with_graceful_fail('pygments') Jekyll::External.require_with_graceful_fail("pygments")
highlighted_code = Pygments.highlight( highlighted_code = Pygments.highlight(
code, code,
@ -84,22 +93,26 @@ eos
) )
if highlighted_code.nil? if highlighted_code.nil?
Jekyll.logger.error "There was an error highlighting your code:" Jekyll.logger.error <<-eos
puts There was an error highlighting your code:
Jekyll.logger.error code
puts #{code}
Jekyll.logger.error "While attempting to convert the above code, Pygments.rb" \
" returned an unacceptable value." While attempting to convert the above code, Pygments.rb returned an unacceptable value.
Jekyll.logger.error "This is usually a timeout problem solved by running `jekyll build` again." This is usually a timeout problem solved by running `jekyll build` again.
raise ArgumentError.new("Pygments.rb returned an unacceptable value when attempting to highlight some code.") eos
raise ArgumentError, "Pygments.rb returned an unacceptable value "\
"when attempting to highlight some code."
end end
highlighted_code.sub('<div class="highlight"><pre>', '').sub('</pre></div>', '') highlighted_code.sub('<div class="highlight"><pre>', "").sub("</pre></div>", "")
end end
def render_rouge(code) def render_rouge(code)
Jekyll::External.require_with_graceful_fail('rouge') Jekyll::External.require_with_graceful_fail("rouge")
formatter = Rouge::Formatters::HTML.new(:line_numbers => @highlight_options[:linenos], :wrap => false) formatter = Rouge::Formatters::HTML.new(
:line_numbers => @highlight_options[:linenos], :wrap => false
)
lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText
formatter.format(lexer.lex(code)) formatter.format(lexer.lex(code))
end end
@ -110,13 +123,14 @@ eos
def add_code_tag(code) def add_code_tag(code)
code_attributes = [ code_attributes = [
"class=\"language-#{@lang.to_s.tr('+', '-')}\"", "class=\"language-#{@lang.to_s.tr("+", "-")}\"",
"data-lang=\"#{@lang}\"" "data-lang=\"#{@lang}\""
].join(" ") ].join(" ")
"<figure class=\"highlight\"><pre><code #{code_attributes}>#{code.chomp}</code></pre></figure>" "<figure class=\"highlight\"><pre><code #{code_attributes}>"\
"#{code.chomp}</code></pre></figure>"
end end
end end
end end
end end
Liquid::Template.register_tag('highlight', Jekyll::Tags::HighlightBlock) Liquid::Template.register_tag("highlight", Jekyll::Tags::HighlightBlock)