Skip to content

Commit

Permalink
Merge pull request jekyll#4931 from pathawks/converters
Browse files Browse the repository at this point in the history
Merge pull request 4931
  • Loading branch information
jekyllbot committed May 26, 2016
2 parents 0ac9c7f + acc9f51 commit 8d0a4be
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 100 deletions.
4 changes: 0 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 18 additions & 8 deletions lib/jekyll/converters/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,33 @@ def setup
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
Expand All @@ -41,13 +51,13 @@ def valid_processors

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
Expand All @@ -66,7 +76,7 @@ def convert(content)
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)
Expand Down
15 changes: 11 additions & 4 deletions lib/jekyll/converters/markdown/kramdown_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def initialize(config)

# 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.

Expand Down Expand Up @@ -52,7 +53,9 @@ def make_accessible(hash = @config)
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.

Expand All @@ -68,8 +71,10 @@ def highlighter

@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
Expand Down Expand Up @@ -100,8 +105,10 @@ def strip_coderay_prefix(hash)
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(
Expand Down
174 changes: 90 additions & 84 deletions lib/jekyll/converters/markdown/redcarpet_parser.rb
Original file line number Diff line number Diff line change
@@ -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(/<pre>/, "<pre><code class=\"language-#{lang}\" data-lang=\"#{lang}\">")
code = code.sub(/<\/pre>/, "</code></pre>")
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
class Jekyll::Converters::Markdown::RedcarpetParser
module CommonMethods
def add_code_tags(code, lang)
code = code.to_s
code = code.sub(
/<pre>/,
"<pre><code class=\"language-#{lang}\" data-lang=\"#{lang}\">"
)
code = code.sub(%r!</pre>!, "</code></pre>")
code
end
end

module WithoutHighlighting
require 'cgi'
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

include CommonMethods
module WithoutHighlighting
require "cgi"

def code_wrap(code)
"<figure class=\"highlight\"><pre>#{CGI::escapeHTML(code)}</pre></figure>"
end
include CommonMethods

def block_code(code, lang)
lang = lang && lang.split.first || "text"
add_code_tags(code_wrap(code), lang)
end
end
def code_wrap(code)
"<figure class=\"highlight\"><pre>#{CGI.escapeHTML(code)}</pre></figure>"
end

module WithRouge
def block_code(code, lang)
code = "<pre>#{super}</pre>"
def block_code(code, lang)
lang = lang && lang.split.first || "text"
add_code_tags(code_wrap(code), lang)
end
end

output = "<div class=\"highlight\">"
output << add_code_tags(code, lang)
output << "</div>"
end
module WithRouge
def block_code(code, lang)
code = "<pre>#{super}</pre>"

protected
def rouge_formatter(_lexer)
Rouge::Formatters::HTML.new(:wrap => false)
end
end
output = "<div class=\"highlight\">"
output << add_code_tags(code, lang)
output << "</div>"
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 }
protected
def rouge_formatter(_lexer)
Rouge::Formatters::HTML.new(:wrap => false)
end
end

@renderer ||= class_with_proper_highlighter(@config['highlighter'])
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

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
))
@renderer ||= class_with_proper_highlighter(@config["highlighter"])
end

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
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
))

include Rouge::Plugins::Redcarpet
include CommonMethods
include WithRouge
end
else
Class.new(Redcarpet::Render::HTML) do
include WithoutHighlighting
end
end
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

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

0 comments on commit 8d0a4be

Please sign in to comment.