Merge pull request #4931 from pathawks/converters
Merge pull request 4931
This commit is contained in:
commit
8d0a4be5e0
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
@ -47,7 +57,7 @@ module Jekyll
|
||||||
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)
|
||||||
|
|
|
@ -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(
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
module Jekyll
|
class Jekyll::Converters::Markdown::RedcarpetParser
|
||||||
module Converters
|
|
||||||
class Markdown
|
|
||||||
class RedcarpetParser
|
|
||||||
module CommonMethods
|
module CommonMethods
|
||||||
def add_code_tags(code, lang)
|
def add_code_tags(code, lang)
|
||||||
code = code.to_s
|
code = code.to_s
|
||||||
code = code.sub(/<pre>/, "<pre><code class=\"language-#{lang}\" data-lang=\"#{lang}\">")
|
code = code.sub(
|
||||||
code = code.sub(/<\/pre>/, "</code></pre>")
|
/<pre>/,
|
||||||
|
"<pre><code class=\"language-#{lang}\" data-lang=\"#{lang}\">"
|
||||||
|
)
|
||||||
|
code = code.sub(%r!</pre>!, "</code></pre>")
|
||||||
code
|
code
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -17,19 +17,25 @@ module Jekyll
|
||||||
Jekyll::External.require_with_graceful_fail("pygments")
|
Jekyll::External.require_with_graceful_fail("pygments")
|
||||||
lang = lang && lang.split.first || "text"
|
lang = lang && lang.split.first || "text"
|
||||||
add_code_tags(
|
add_code_tags(
|
||||||
Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }),
|
Pygments.highlight(
|
||||||
|
code,
|
||||||
|
{
|
||||||
|
:lexer => lang,
|
||||||
|
:options => { :encoding => "utf-8" }
|
||||||
|
}
|
||||||
|
),
|
||||||
lang
|
lang
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module WithoutHighlighting
|
module WithoutHighlighting
|
||||||
require 'cgi'
|
require "cgi"
|
||||||
|
|
||||||
include CommonMethods
|
include CommonMethods
|
||||||
|
|
||||||
def code_wrap(code)
|
def code_wrap(code)
|
||||||
"<figure class=\"highlight\"><pre>#{CGI::escapeHTML(code)}</pre></figure>"
|
"<figure class=\"highlight\"><pre>#{CGI.escapeHTML(code)}</pre></figure>"
|
||||||
end
|
end
|
||||||
|
|
||||||
def block_code(code, lang)
|
def block_code(code, lang)
|
||||||
|
@ -54,25 +60,24 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(config)
|
def initialize(config)
|
||||||
External.require_with_graceful_fail("redcarpet")
|
Jekyll::External.require_with_graceful_fail("redcarpet")
|
||||||
@config = config
|
@config = config
|
||||||
@redcarpet_extensions = {}
|
@redcarpet_extensions = {}
|
||||||
@config['redcarpet']['extensions'].each { |e| @redcarpet_extensions[e.to_sym] = true }
|
@config["redcarpet"]["extensions"].each do |e|
|
||||||
|
@redcarpet_extensions[e.to_sym] = true
|
||||||
|
end
|
||||||
|
|
||||||
@renderer ||= class_with_proper_highlighter(@config['highlighter'])
|
@renderer ||= class_with_proper_highlighter(@config["highlighter"])
|
||||||
end
|
end
|
||||||
|
|
||||||
def class_with_proper_highlighter(highlighter)
|
def class_with_proper_highlighter(highlighter)
|
||||||
|
Class.new(Redcarpet::Render::HTML) do
|
||||||
case highlighter
|
case highlighter
|
||||||
when "pygments"
|
when "pygments"
|
||||||
Class.new(Redcarpet::Render::HTML) do
|
|
||||||
include WithPygments
|
include WithPygments
|
||||||
end
|
|
||||||
when "rouge"
|
when "rouge"
|
||||||
Class.new(Redcarpet::Render::HTML) do
|
|
||||||
Jekyll::External.require_with_graceful_fail(%w(
|
Jekyll::External.require_with_graceful_fail(%w(
|
||||||
rouge
|
rouge rouge/plugins/redcarpet
|
||||||
rouge/plugins/redcarpet
|
|
||||||
))
|
))
|
||||||
|
|
||||||
unless Gem::Version.new(Rouge.version) > Gem::Version.new("1.3.0")
|
unless Gem::Version.new(Rouge.version) > Gem::Version.new("1.3.0")
|
||||||
|
@ -82,21 +87,22 @@ module Jekyll
|
||||||
include Rouge::Plugins::Redcarpet
|
include Rouge::Plugins::Redcarpet
|
||||||
include CommonMethods
|
include CommonMethods
|
||||||
include WithRouge
|
include WithRouge
|
||||||
end
|
|
||||||
else
|
else
|
||||||
Class.new(Redcarpet::Render::HTML) do
|
|
||||||
include WithoutHighlighting
|
include WithoutHighlighting
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def convert(content)
|
def convert(content)
|
||||||
@redcarpet_extensions[:fenced_code_blocks] = !@redcarpet_extensions[:no_fenced_code_blocks]
|
@redcarpet_extensions[:fenced_code_blocks] = \
|
||||||
@renderer.send :include, Redcarpet::Render::SmartyPants if @redcarpet_extensions[:smart]
|
!@redcarpet_extensions[:no_fenced_code_blocks]
|
||||||
markdown = Redcarpet::Markdown.new(@renderer.new(@redcarpet_extensions), @redcarpet_extensions)
|
if @redcarpet_extensions[:smart]
|
||||||
|
@renderer.send :include, Redcarpet::Render::SmartyPants
|
||||||
|
end
|
||||||
|
markdown = Redcarpet::Markdown.new(
|
||||||
|
@renderer.new(@redcarpet_extensions),
|
||||||
|
@redcarpet_extensions
|
||||||
|
)
|
||||||
markdown.render(content)
|
markdown.render(content)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
Loading…
Reference in New Issue