Move initialization of parsers to separate classes
This commit is contained in:
parent
47653381b1
commit
63cdd21353
|
@ -46,6 +46,7 @@ require 'jekyll/command'
|
||||||
|
|
||||||
require_all 'jekyll/commands'
|
require_all 'jekyll/commands'
|
||||||
require_all 'jekyll/converters'
|
require_all 'jekyll/converters'
|
||||||
|
require_all 'jekyll/converters/parsers'
|
||||||
require_all 'jekyll/generators'
|
require_all 'jekyll/generators'
|
||||||
require_all 'jekyll/tags'
|
require_all 'jekyll/tags'
|
||||||
|
|
||||||
|
|
|
@ -8,79 +8,15 @@ 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 ]"
|
||||||
|
@ -88,7 +24,7 @@ module Jekyll
|
||||||
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)
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,43 @@
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,17 @@
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,35 @@
|
||||||
|
module Jekyll
|
||||||
|
module Converters
|
||||||
|
class Markdown
|
||||||
|
class RedcarpetParser
|
||||||
|
def initialize(config)
|
||||||
|
begin
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue