diff --git a/.rubocop.yml b/.rubocop.yml index 0af6931c..b49c8c0c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -7,10 +7,6 @@ AllCops: - lib/jekyll/collection.rb - lib/jekyll/command.rb - lib/jekyll/configuration.rb - - lib/jekyll/converters/identity.rb - - lib/jekyll/converters/markdown/kramdown_parser.rb - - lib/jekyll/converters/markdown/redcarpet_parser.rb - - lib/jekyll/converters/markdown.rb - lib/jekyll/convertible.rb - lib/jekyll/deprecator.rb - lib/jekyll/document.rb diff --git a/lib/jekyll/converters/markdown.rb b/lib/jekyll/converters/markdown.rb index 6e44b3b2..ab886d06 100644 --- a/lib/jekyll/converters/markdown.rb +++ b/lib/jekyll/converters/markdown.rb @@ -9,23 +9,33 @@ module Jekyll return if @setup ||= false unless (@parser = get_processor) Jekyll.logger.error "Invalid Markdown processor given:", @config["markdown"] - Jekyll.logger.info "", "Custom processors are not loaded in safe mode" if @config["safe"] - Jekyll.logger.error "", "Available processors are: #{valid_processors.join(", ")}" + if @config["safe"] + Jekyll.logger.info "", "Custom processors are not loaded in safe mode" + end + Jekyll.logger.error( + "", + "Available processors are: #{valid_processors.join(", ")}" + ) raise Errors::FatalException, "Bailing out; invalid Markdown processor." end @setup = true end + # Rubocop does not allow reader methods to have names starting with `get_` + # To ensure compatibility, this check has been disabled on this method + # + # rubocop:disable Style/AccessorMethodName def get_processor case @config["markdown"].downcase when "redcarpet" then return RedcarpetParser.new(@config) when "kramdown" then return KramdownParser.new(@config) when "rdiscount" then return RDiscountParser.new(@config) else - get_custom_processor + custom_processor end end + # rubocop:enable Style/AccessorMethodName # Public: Provides you with a list of processors, the ones we # support internally and the ones that you have provided to us (if you @@ -41,13 +51,13 @@ module Jekyll def third_party_processors self.class.constants - \ - %w(KramdownParser RDiscountParser RedcarpetParser PRIORITIES).map( - &:to_sym - ) + %w(KramdownParser RDiscountParser RedcarpetParser PRIORITIES).map( + &:to_sym + ) end def extname_list - @extname_list ||= @config['markdown_ext'].split(',').map do |e| + @extname_list ||= @config["markdown_ext"].split(",").map do |e| ".#{e.downcase}" end end @@ -66,7 +76,7 @@ module Jekyll end private - def get_custom_processor + def custom_processor converter_name = @config["markdown"] if custom_class_allowed?(converter_name) self.class.const_get(converter_name).new(@config) diff --git a/lib/jekyll/converters/markdown/kramdown_parser.rb b/lib/jekyll/converters/markdown/kramdown_parser.rb index 4d84a5bc..6e65d43c 100644 --- a/lib/jekyll/converters/markdown/kramdown_parser.rb +++ b/lib/jekyll/converters/markdown/kramdown_parser.rb @@ -24,7 +24,8 @@ module Jekyll # Setup and normalize the configuration: # * Create Kramdown if it doesn't exist. - # * Set syntax_highlighter, detecting enable_coderay and merging highlighter if none. + # * Set syntax_highlighter, detecting enable_coderay and merging + # highlighter if none. # * Merge kramdown[coderay] into syntax_highlighter_opts stripping coderay_. # * Make sure `syntax_highlighter_opts` exists. @@ -52,7 +53,9 @@ module Jekyll end end - # config[kramdown][syntax_higlighter] > config[kramdown][enable_coderay] > config[highlighter] + # config[kramdown][syntax_higlighter] > + # config[kramdown][enable_coderay] > + # config[highlighter] # Where `enable_coderay` is now deprecated because Kramdown # supports Rouge now too. @@ -68,8 +71,10 @@ module Jekyll @highlighter = begin if @config.key?("enable_coderay") && @config["enable_coderay"] - Jekyll::Deprecator.deprecation_message "You are using 'enable_coderay', " \ + Jekyll::Deprecator.deprecation_message( + "You are using 'enable_coderay', " \ "use syntax_highlighter: coderay in your configuration file." + ) "coderay" else @@ -100,8 +105,10 @@ module Jekyll private def modernize_coderay_config if highlighter == "coderay" - Jekyll::Deprecator.deprecation_message "You are using 'kramdown.coderay' in your configuration, " \ + Jekyll::Deprecator.deprecation_message( + "You are using 'kramdown.coderay' in your configuration, " \ "please use 'syntax_highlighter_opts' instead." + ) @config["syntax_highlighter_opts"] = begin strip_coderay_prefix( diff --git a/lib/jekyll/converters/markdown/redcarpet_parser.rb b/lib/jekyll/converters/markdown/redcarpet_parser.rb index 79133a2a..8e759385 100644 --- a/lib/jekyll/converters/markdown/redcarpet_parser.rb +++ b/lib/jekyll/converters/markdown/redcarpet_parser.rb @@ -1,102 +1,108 @@ -module Jekyll - module Converters - class Markdown - class RedcarpetParser - module CommonMethods - def add_code_tags(code, lang) - code = code.to_s - code = code.sub(/
/, "
")
-            code = code.sub(/<\/pre>/, "
") - code - end +class Jekyll::Converters::Markdown::RedcarpetParser + module CommonMethods + def add_code_tags(code, lang) + code = code.to_s + code = code.sub( + /
/,
+        "
"
+      )
+      code = code.sub(%r!
!, "
") + code + end + end + + module WithPygments + include CommonMethods + def block_code(code, lang) + Jekyll::External.require_with_graceful_fail("pygments") + lang = lang && lang.split.first || "text" + add_code_tags( + Pygments.highlight( + code, + { + :lexer => lang, + :options => { :encoding => "utf-8" } + } + ), + lang + ) + end + end + + module WithoutHighlighting + require "cgi" + + include CommonMethods + + def code_wrap(code) + "
#{CGI.escapeHTML(code)}
" + end + + def block_code(code, lang) + lang = lang && lang.split.first || "text" + add_code_tags(code_wrap(code), lang) + end + end + + module WithRouge + def block_code(code, lang) + code = "
#{super}
" + + output = "
" + output << add_code_tags(code, lang) + output << "
" + end + + protected + def rouge_formatter(_lexer) + Rouge::Formatters::HTML.new(:wrap => false) + end + end + + def initialize(config) + Jekyll::External.require_with_graceful_fail("redcarpet") + @config = config + @redcarpet_extensions = {} + @config["redcarpet"]["extensions"].each do |e| + @redcarpet_extensions[e.to_sym] = true + end + + @renderer ||= class_with_proper_highlighter(@config["highlighter"]) + end + + def class_with_proper_highlighter(highlighter) + Class.new(Redcarpet::Render::HTML) do + case highlighter + when "pygments" + include WithPygments + when "rouge" + Jekyll::External.require_with_graceful_fail(%w( + rouge rouge/plugins/redcarpet + )) + + unless Gem::Version.new(Rouge.version) > Gem::Version.new("1.3.0") + abort "Please install Rouge 1.3.0 or greater and try running Jekyll again." end - module WithPygments - include CommonMethods - def block_code(code, lang) - Jekyll::External.require_with_graceful_fail("pygments") - lang = lang && lang.split.first || "text" - add_code_tags( - Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }), - lang - ) - end - end - - module WithoutHighlighting - require 'cgi' - - include CommonMethods - - def code_wrap(code) - "
#{CGI::escapeHTML(code)}
" - end - - def block_code(code, lang) - lang = lang && lang.split.first || "text" - add_code_tags(code_wrap(code), lang) - end - end - - module WithRouge - def block_code(code, lang) - code = "
#{super}
" - - output = "
" - output << add_code_tags(code, lang) - output << "
" - end - - protected - def rouge_formatter(_lexer) - Rouge::Formatters::HTML.new(:wrap => false) - end - end - - def initialize(config) - External.require_with_graceful_fail("redcarpet") - @config = config - @redcarpet_extensions = {} - @config['redcarpet']['extensions'].each { |e| @redcarpet_extensions[e.to_sym] = true } - - @renderer ||= class_with_proper_highlighter(@config['highlighter']) - end - - def class_with_proper_highlighter(highlighter) - case highlighter - when "pygments" - Class.new(Redcarpet::Render::HTML) do - include WithPygments - end - when "rouge" - Class.new(Redcarpet::Render::HTML) do - Jekyll::External.require_with_graceful_fail(%w( - rouge - rouge/plugins/redcarpet - )) - - unless Gem::Version.new(Rouge.version) > Gem::Version.new("1.3.0") - abort "Please install Rouge 1.3.0 or greater and try running Jekyll again." - end - - include Rouge::Plugins::Redcarpet - include CommonMethods - include WithRouge - end - else - Class.new(Redcarpet::Render::HTML) do - include WithoutHighlighting - end - end - end - - def convert(content) - @redcarpet_extensions[:fenced_code_blocks] = !@redcarpet_extensions[:no_fenced_code_blocks] - @renderer.send :include, Redcarpet::Render::SmartyPants if @redcarpet_extensions[:smart] - markdown = Redcarpet::Markdown.new(@renderer.new(@redcarpet_extensions), @redcarpet_extensions) - markdown.render(content) - end + include Rouge::Plugins::Redcarpet + include CommonMethods + include WithRouge + else + include WithoutHighlighting end end end + + def convert(content) + @redcarpet_extensions[:fenced_code_blocks] = \ + !@redcarpet_extensions[:no_fenced_code_blocks] + if @redcarpet_extensions[:smart] + @renderer.send :include, Redcarpet::Render::SmartyPants + end + markdown = Redcarpet::Markdown.new( + @renderer.new(@redcarpet_extensions), + @redcarpet_extensions + ) + markdown.render(content) + end end