Merge pull request #4931 from pathawks/converters

Merge pull request 4931
This commit is contained in:
jekyllbot 2016-05-26 10:28:21 -07:00
commit 8d0a4be5e0
4 changed files with 131 additions and 112 deletions

View File

@ -7,10 +7,6 @@ AllCops:
- lib/jekyll/collection.rb - lib/jekyll/collection.rb
- lib/jekyll/command.rb - lib/jekyll/command.rb
- lib/jekyll/configuration.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/convertible.rb
- lib/jekyll/deprecator.rb - lib/jekyll/deprecator.rb
- lib/jekyll/document.rb - lib/jekyll/document.rb

View File

@ -9,23 +9,33 @@ module Jekyll
return if @setup ||= false return if @setup ||= false
unless (@parser = get_processor) unless (@parser = get_processor)
Jekyll.logger.error "Invalid Markdown processor given:", @config["markdown"] Jekyll.logger.error "Invalid Markdown processor given:", @config["markdown"]
Jekyll.logger.info "", "Custom processors are not loaded in safe mode" if @config["safe"] if @config["safe"]
Jekyll.logger.error "", "Available processors are: #{valid_processors.join(", ")}" 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." raise Errors::FatalException, "Bailing out; invalid Markdown processor."
end end
@setup = true @setup = true
end 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 def get_processor
case @config["markdown"].downcase case @config["markdown"].downcase
when "redcarpet" then return RedcarpetParser.new(@config) when "redcarpet" then return RedcarpetParser.new(@config)
when "kramdown" then return KramdownParser.new(@config) when "kramdown" then return KramdownParser.new(@config)
when "rdiscount" then return RDiscountParser.new(@config) when "rdiscount" then return RDiscountParser.new(@config)
else else
get_custom_processor custom_processor
end end
end end
# rubocop:enable Style/AccessorMethodName
# Public: Provides you with a list of processors, the ones we # Public: Provides you with a list of processors, the ones we
# support internally and the ones that you have provided to us (if you # support internally and the ones that you have provided to us (if you
@ -41,13 +51,13 @@ module Jekyll
def third_party_processors def third_party_processors
self.class.constants - \ self.class.constants - \
%w(KramdownParser RDiscountParser RedcarpetParser PRIORITIES).map( %w(KramdownParser RDiscountParser RedcarpetParser PRIORITIES).map(
&:to_sym &:to_sym
) )
end end
def extname_list def extname_list
@extname_list ||= @config['markdown_ext'].split(',').map do |e| @extname_list ||= @config["markdown_ext"].split(",").map do |e|
".#{e.downcase}" ".#{e.downcase}"
end end
end end
@ -66,7 +76,7 @@ module Jekyll
end end
private private
def get_custom_processor def custom_processor
converter_name = @config["markdown"] converter_name = @config["markdown"]
if custom_class_allowed?(converter_name) if custom_class_allowed?(converter_name)
self.class.const_get(converter_name).new(@config) self.class.const_get(converter_name).new(@config)

View File

@ -24,7 +24,8 @@ module Jekyll
# Setup and normalize the configuration: # Setup and normalize the configuration:
# * Create Kramdown if it doesn't exist. # * 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_. # * Merge kramdown[coderay] into syntax_highlighter_opts stripping coderay_.
# * Make sure `syntax_highlighter_opts` exists. # * Make sure `syntax_highlighter_opts` exists.
@ -52,7 +53,9 @@ module Jekyll
end end
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 # Where `enable_coderay` is now deprecated because Kramdown
# supports Rouge now too. # supports Rouge now too.
@ -68,8 +71,10 @@ module Jekyll
@highlighter = begin @highlighter = begin
if @config.key?("enable_coderay") && @config["enable_coderay"] 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." "use syntax_highlighter: coderay in your configuration file."
)
"coderay" "coderay"
else else
@ -100,8 +105,10 @@ module Jekyll
private private
def modernize_coderay_config def modernize_coderay_config
if highlighter == "coderay" 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." "please use 'syntax_highlighter_opts' instead."
)
@config["syntax_highlighter_opts"] = begin @config["syntax_highlighter_opts"] = begin
strip_coderay_prefix( strip_coderay_prefix(

View File

@ -1,102 +1,108 @@
module Jekyll class Jekyll::Converters::Markdown::RedcarpetParser
module Converters module CommonMethods
class Markdown def add_code_tags(code, lang)
class RedcarpetParser code = code.to_s
module CommonMethods code = code.sub(
def add_code_tags(code, lang) /<pre>/,
code = code.to_s "<pre><code class=\"language-#{lang}\" data-lang=\"#{lang}\">"
code = code.sub(/<pre>/, "<pre><code class=\"language-#{lang}\" data-lang=\"#{lang}\">") )
code = code.sub(/<\/pre>/, "</code></pre>") code = code.sub(%r!</pre>!, "</code></pre>")
code code
end 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)
"<figure class=\"highlight\"><pre>#{CGI.escapeHTML(code)}</pre></figure>"
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 = "<pre>#{super}</pre>"
output = "<div class=\"highlight\">"
output << add_code_tags(code, lang)
output << "</div>"
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 end
module WithPygments include Rouge::Plugins::Redcarpet
include CommonMethods include CommonMethods
def block_code(code, lang) include WithRouge
Jekyll::External.require_with_graceful_fail("pygments") else
lang = lang && lang.split.first || "text" include WithoutHighlighting
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)
"<figure class=\"highlight\"><pre>#{CGI::escapeHTML(code)}</pre></figure>"
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 = "<pre>#{super}</pre>"
output = "<div class=\"highlight\">"
output << add_code_tags(code, lang)
output << "</div>"
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
end end
end 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 end