Merge pull request #955 from mojombo/refactor-markdown
Refactor Markdown Parser Logic
This commit is contained in:
commit
81e44bf5f4
|
@ -47,6 +47,7 @@ require 'jekyll/command'
|
||||||
|
|
||||||
require_all 'jekyll/commands'
|
require_all 'jekyll/commands'
|
||||||
require_all 'jekyll/converters'
|
require_all 'jekyll/converters'
|
||||||
|
require_all 'jekyll/converters/markdown'
|
||||||
require_all 'jekyll/generators'
|
require_all 'jekyll/generators'
|
||||||
require_all 'jekyll/tags'
|
require_all 'jekyll/tags'
|
||||||
|
|
||||||
|
|
|
@ -8,87 +8,23 @@ module Jekyll
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
return if @setup
|
return if @setup
|
||||||
case @config['markdown']
|
@parser = case @config['markdown']
|
||||||
when 'redcarpet'
|
when 'redcarpet'
|
||||||
begin
|
RedcarpetParser.new @config
|
||||||
require 'redcarpet'
|
|
||||||
|
|
||||||
@renderer ||= Class.new(Redcarpet::Render::HTML) do
|
|
||||||
def block_code(code, lang)
|
|
||||||
lang = lang && lang.split.first || "text"
|
|
||||||
output = add_code_tags(
|
|
||||||
Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }),
|
|
||||||
lang
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
def add_code_tags(code, lang)
|
|
||||||
code = code.sub(/<pre>/,'<pre><code class="' + lang + '">')
|
|
||||||
code = code.sub(/<\/pre>/,"</code></pre>")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@redcarpet_extensions = {}
|
|
||||||
@config['redcarpet']['extensions'].each { |e| @redcarpet_extensions[e.to_sym] = true }
|
|
||||||
rescue LoadError
|
|
||||||
STDERR.puts 'You are missing a library required for Markdown. Please run:'
|
|
||||||
STDERR.puts ' $ [sudo] gem install redcarpet'
|
|
||||||
raise FatalException.new("Missing dependency: redcarpet")
|
|
||||||
end
|
|
||||||
when 'kramdown'
|
when 'kramdown'
|
||||||
begin
|
KramdownParser.new @config
|
||||||
require 'kramdown'
|
|
||||||
rescue LoadError
|
|
||||||
STDERR.puts 'You are missing a library required for Markdown. Please run:'
|
|
||||||
STDERR.puts ' $ [sudo] gem install kramdown'
|
|
||||||
raise FatalException.new("Missing dependency: kramdown")
|
|
||||||
end
|
|
||||||
when 'rdiscount'
|
when 'rdiscount'
|
||||||
begin
|
RDiscountParser.new @config
|
||||||
require 'rdiscount'
|
|
||||||
@rdiscount_extensions = @config['rdiscount']['extensions'].map { |e| e.to_sym }
|
|
||||||
rescue LoadError
|
|
||||||
STDERR.puts 'You are missing a library required for Markdown. Please run:'
|
|
||||||
STDERR.puts ' $ [sudo] gem install rdiscount'
|
|
||||||
raise FatalException.new("Missing dependency: rdiscount")
|
|
||||||
end
|
|
||||||
when 'maruku'
|
when 'maruku'
|
||||||
begin
|
MarukuParser.new @config
|
||||||
require 'maruku'
|
|
||||||
|
|
||||||
if @config['maruku']['use_divs']
|
|
||||||
require 'maruku/ext/div'
|
|
||||||
STDERR.puts 'Maruku: Using extended syntax for div elements.'
|
|
||||||
end
|
|
||||||
|
|
||||||
if @config['maruku']['use_tex']
|
|
||||||
require 'maruku/ext/math'
|
|
||||||
STDERR.puts "Maruku: Using LaTeX extension. Images in `#{@config['maruku']['png_dir']}`."
|
|
||||||
|
|
||||||
# Switch off MathML output
|
|
||||||
MaRuKu::Globals[:html_math_output_mathml] = false
|
|
||||||
MaRuKu::Globals[:html_math_engine] = 'none'
|
|
||||||
|
|
||||||
# Turn on math to PNG support with blahtex
|
|
||||||
# Resulting PNGs stored in `images/latex`
|
|
||||||
MaRuKu::Globals[:html_math_output_png] = true
|
|
||||||
MaRuKu::Globals[:html_png_engine] = @config['maruku']['png_engine']
|
|
||||||
MaRuKu::Globals[:html_png_dir] = @config['maruku']['png_dir']
|
|
||||||
MaRuKu::Globals[:html_png_url] = @config['maruku']['png_url']
|
|
||||||
end
|
|
||||||
rescue LoadError
|
|
||||||
STDERR.puts 'You are missing a library required for Markdown. Please run:'
|
|
||||||
STDERR.puts ' $ [sudo] gem install maruku'
|
|
||||||
raise FatalException.new("Missing dependency: maruku")
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
STDERR.puts "Invalid Markdown processor: #{@config['markdown']}"
|
STDERR.puts "Invalid Markdown processor: #{@config['markdown']}"
|
||||||
STDERR.puts " Valid options are [ maruku | rdiscount | kramdown ]"
|
STDERR.puts " Valid options are [ maruku | rdiscount | kramdown | redcarpet ]"
|
||||||
raise FatalException.new("Invalid Markdown process: #{@config['markdown']}")
|
raise FatalException.new("Invalid Markdown process: #{@config['markdown']}")
|
||||||
end
|
end
|
||||||
@setup = true
|
@setup = true
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches(ext)
|
def matches(ext)
|
||||||
rgx = '(' + @config['markdown_ext'].gsub(',','|') +')'
|
rgx = '(' + @config['markdown_ext'].gsub(',','|') +')'
|
||||||
ext =~ Regexp.new(rgx, Regexp::IGNORECASE)
|
ext =~ Regexp.new(rgx, Regexp::IGNORECASE)
|
||||||
|
@ -100,49 +36,7 @@ module Jekyll
|
||||||
|
|
||||||
def convert(content)
|
def convert(content)
|
||||||
setup
|
setup
|
||||||
case @config['markdown']
|
@parser.convert(content)
|
||||||
when 'redcarpet'
|
|
||||||
@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)
|
|
||||||
when 'kramdown'
|
|
||||||
# Check for use of coderay
|
|
||||||
if @config['kramdown']['use_coderay']
|
|
||||||
Kramdown::Document.new(content, {
|
|
||||||
:auto_ids => @config['kramdown']['auto_ids'],
|
|
||||||
:footnote_nr => @config['kramdown']['footnote_nr'],
|
|
||||||
:entity_output => @config['kramdown']['entity_output'],
|
|
||||||
:toc_levels => @config['kramdown']['toc_levels'],
|
|
||||||
:smart_quotes => @config['kramdown']['smart_quotes'],
|
|
||||||
|
|
||||||
:coderay_wrap => @config['kramdown']['coderay']['coderay_wrap'],
|
|
||||||
:coderay_line_numbers => @config['kramdown']['coderay']['coderay_line_numbers'],
|
|
||||||
:coderay_line_number_start => @config['kramdown']['coderay']['coderay_line_number_start'],
|
|
||||||
:coderay_tab_width => @config['kramdown']['coderay']['coderay_tab_width'],
|
|
||||||
:coderay_bold_every => @config['kramdown']['coderay']['coderay_bold_every'],
|
|
||||||
:coderay_css => @config['kramdown']['coderay']['coderay_css']
|
|
||||||
}).to_html
|
|
||||||
else
|
|
||||||
# not using coderay
|
|
||||||
Kramdown::Document.new(content, {
|
|
||||||
:auto_ids => @config['kramdown']['auto_ids'],
|
|
||||||
:footnote_nr => @config['kramdown']['footnote_nr'],
|
|
||||||
:entity_output => @config['kramdown']['entity_output'],
|
|
||||||
:toc_levels => @config['kramdown']['toc_levels'],
|
|
||||||
:smart_quotes => @config['kramdown']['smart_quotes']
|
|
||||||
}).to_html
|
|
||||||
end
|
|
||||||
when 'rdiscount'
|
|
||||||
rd = RDiscount.new(content, *@rdiscount_extensions)
|
|
||||||
html = rd.to_html
|
|
||||||
if rd.generate_toc and html.include?(@config['rdiscount']['toc_token'])
|
|
||||||
html.gsub!(@config['rdiscount']['toc_token'], rd.toc_content.force_encoding('utf-8'))
|
|
||||||
end
|
|
||||||
html
|
|
||||||
when 'maruku'
|
|
||||||
Maruku.new(content).to_html
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
module Jekyll
|
||||||
|
module Converters
|
||||||
|
class Markdown
|
||||||
|
class KramdownParser
|
||||||
|
def initialize(config)
|
||||||
|
require 'kramdown'
|
||||||
|
@config = config
|
||||||
|
rescue LoadError
|
||||||
|
STDERR.puts 'You are missing a library required for Markdown. Please run:'
|
||||||
|
STDERR.puts ' $ [sudo] gem install kramdown'
|
||||||
|
raise FatalException.new("Missing dependency: kramdown")
|
||||||
|
end
|
||||||
|
|
||||||
|
def convert(content)
|
||||||
|
# Check for use of coderay
|
||||||
|
kramdown_configs = if @config['kramdown']['use_coderay']
|
||||||
|
base_kramdown_configs.merge({
|
||||||
|
:coderay_wrap => @config['kramdown']['coderay']['coderay_wrap'],
|
||||||
|
:coderay_line_numbers => @config['kramdown']['coderay']['coderay_line_numbers'],
|
||||||
|
:coderay_line_number_start => @config['kramdown']['coderay']['coderay_line_number_start'],
|
||||||
|
:coderay_tab_width => @config['kramdown']['coderay']['coderay_tab_width'],
|
||||||
|
:coderay_bold_every => @config['kramdown']['coderay']['coderay_bold_every'],
|
||||||
|
:coderay_css => @config['kramdown']['coderay']['coderay_css']
|
||||||
|
})
|
||||||
|
else
|
||||||
|
# not using coderay
|
||||||
|
base_kramdown_configs
|
||||||
|
end
|
||||||
|
Kramdown::Document.new(content, kramdown_configs).to_html
|
||||||
|
end
|
||||||
|
|
||||||
|
def base_kramdown_configs
|
||||||
|
{
|
||||||
|
:auto_ids => @config['kramdown']['auto_ids'],
|
||||||
|
:footnote_nr => @config['kramdown']['footnote_nr'],
|
||||||
|
:entity_output => @config['kramdown']['entity_output'],
|
||||||
|
:toc_levels => @config['kramdown']['toc_levels'],
|
||||||
|
:smart_quotes => @config['kramdown']['smart_quotes']
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,47 @@
|
||||||
|
module Jekyll
|
||||||
|
module Converters
|
||||||
|
class Markdown
|
||||||
|
class MarukuParser
|
||||||
|
def initialize(config)
|
||||||
|
require 'maruku'
|
||||||
|
@config = config
|
||||||
|
if @config['maruku']['use_divs']
|
||||||
|
load_divs_library
|
||||||
|
end
|
||||||
|
if @config['maruku']['use_tex']
|
||||||
|
load_blahtext_library
|
||||||
|
end
|
||||||
|
rescue LoadError
|
||||||
|
STDERR.puts 'You are missing a library required for Markdown. Please run:'
|
||||||
|
STDERR.puts ' $ [sudo] gem install maruku'
|
||||||
|
raise FatalException.new("Missing dependency: maruku")
|
||||||
|
end
|
||||||
|
|
||||||
|
def load_divs_library
|
||||||
|
require 'maruku/ext/div'
|
||||||
|
STDERR.puts 'Maruku: Using extended syntax for div elements.'
|
||||||
|
end
|
||||||
|
|
||||||
|
def load_blahtext_library
|
||||||
|
require 'maruku/ext/math'
|
||||||
|
STDERR.puts "Maruku: Using LaTeX extension. Images in `#{@config['maruku']['png_dir']}`."
|
||||||
|
|
||||||
|
# Switch off MathML output
|
||||||
|
MaRuKu::Globals[:html_math_output_mathml] = false
|
||||||
|
MaRuKu::Globals[:html_math_engine] = 'none'
|
||||||
|
|
||||||
|
# Turn on math to PNG support with blahtex
|
||||||
|
# Resulting PNGs stored in `images/latex`
|
||||||
|
MaRuKu::Globals[:html_math_output_png] = true
|
||||||
|
MaRuKu::Globals[:html_png_engine] = @config['maruku']['png_engine']
|
||||||
|
MaRuKu::Globals[:html_png_dir] = @config['maruku']['png_dir']
|
||||||
|
MaRuKu::Globals[:html_png_url] = @config['maruku']['png_url']
|
||||||
|
end
|
||||||
|
|
||||||
|
def convert(content)
|
||||||
|
Maruku.new(content).to_html
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,26 @@
|
||||||
|
module Jekyll
|
||||||
|
module Converters
|
||||||
|
class Markdown
|
||||||
|
class RDiscountParser
|
||||||
|
def initialize(config)
|
||||||
|
require 'rdiscount'
|
||||||
|
@config = config
|
||||||
|
@rdiscount_extensions = @config['rdiscount']['extensions'].map { |e| e.to_sym }
|
||||||
|
rescue LoadError
|
||||||
|
STDERR.puts 'You are missing a library required for Markdown. Please run:'
|
||||||
|
STDERR.puts ' $ [sudo] gem install rdiscount'
|
||||||
|
raise FatalException.new("Missing dependency: rdiscount")
|
||||||
|
end
|
||||||
|
|
||||||
|
def convert(content)
|
||||||
|
rd = RDiscount.new(content, *@rdiscount_extensions)
|
||||||
|
html = rd.to_html
|
||||||
|
if rd.generate_toc and html.include?(@config['rdiscount']['toc_token'])
|
||||||
|
html.gsub!(@config['rdiscount']['toc_token'], rd.toc_content.force_encoding('utf-8'))
|
||||||
|
end
|
||||||
|
html
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,40 @@
|
||||||
|
module Jekyll
|
||||||
|
module Converters
|
||||||
|
class Markdown
|
||||||
|
class RedcarpetParser
|
||||||
|
def initialize(config)
|
||||||
|
require 'redcarpet'
|
||||||
|
@config = config
|
||||||
|
@redcarpet_extensions = {}
|
||||||
|
@config['redcarpet']['extensions'].each { |e| @redcarpet_extensions[e.to_sym] = true }
|
||||||
|
|
||||||
|
@renderer ||= Class.new(Redcarpet::Render::HTML) do
|
||||||
|
def block_code(code, lang)
|
||||||
|
lang = lang && lang.split.first || "text"
|
||||||
|
output = add_code_tags(
|
||||||
|
Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }),
|
||||||
|
lang
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_code_tags(code, lang)
|
||||||
|
code = code.sub(/<pre>/,'<pre><code class="' + lang + '">')
|
||||||
|
code = code.sub(/<\/pre>/,"</code></pre>")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
rescue LoadError
|
||||||
|
STDERR.puts 'You are missing a library required for Markdown. Please run:'
|
||||||
|
STDERR.puts ' $ [sudo] gem install redcarpet'
|
||||||
|
raise FatalException.new("Missing dependency: redcarpet")
|
||||||
|
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
|
Loading…
Reference in New Issue