Update converter classes moving into a module

This commit is contained in:
Tom Bell 2013-01-19 23:07:38 +00:00
parent 6514fe2d22
commit 663a1321d2
11 changed files with 212 additions and 215 deletions

View File

@ -1,5 +1,4 @@
module Jekyll
class Converter < Plugin
# Public: Get or set the pygments prefix. When an argument is specified,
# the prefix will be set. If no argument is specified, the current prefix
@ -46,5 +45,4 @@ module Jekyll
self.class.pygments_suffix
end
end
end
end

View File

@ -1,22 +1,21 @@
module Jekyll
module Converters
class Identity < Converter
safe true
class IdentityConverter < Converter
safe true
priority :lowest
priority :lowest
def matches(ext)
true
end
def matches(ext)
true
def output_ext(ext)
ext
end
def convert(content)
content
end
end
def output_ext(ext)
ext
end
def convert(content)
content
end
end
end

View File

@ -1,149 +1,149 @@
module Jekyll
module Converters
class Markdown < Converter
safe true
class MarkdownConverter < Converter
safe true
pygments_prefix "\n"
pygments_suffix "\n"
pygments_prefix "\n"
pygments_suffix "\n"
def setup
return if @setup
case @config['markdown']
when 'redcarpet'
begin
require 'redcarpet'
def setup
return if @setup
case @config['markdown']
when 'redcarpet'
begin
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
@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
)
def add_code_tags(code, lang)
code = code.sub(/<pre>/,'<pre><code class="' + lang + '">')
code = code.sub(/<\/pre>/,"</code></pre>")
end
end
def add_code_tags(code, lang)
code = code.sub(/<pre>/,'<pre><code class="' + lang + '">')
code = code.sub(/<\/pre>/,"</code></pre>")
@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'
begin
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'
begin
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'
begin
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
@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'
begin
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'
begin
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'
begin
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
STDERR.puts "Invalid Markdown processor: #{@config['markdown']}"
STDERR.puts " Valid options are [ maruku | rdiscount | kramdown ]"
raise FatalException.new("Invalid Markdown process: #{@config['markdown']}")
end
@setup = true
end
def matches(ext)
rgx = '(' + @config['markdown_ext'].gsub(',','|') +')'
ext =~ Regexp.new(rgx, Regexp::IGNORECASE)
end
def output_ext(ext)
".html"
end
def convert(content)
setup
case @config['markdown']
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)
end
html
when 'maruku'
Maruku.new(content).to_html
STDERR.puts "Invalid Markdown processor: #{@config['markdown']}"
STDERR.puts " Valid options are [ maruku | rdiscount | kramdown ]"
raise FatalException.new("Invalid Markdown process: #{@config['markdown']}")
end
@setup = true
end
def matches(ext)
rgx = '(' + @config['markdown_ext'].gsub(',','|') +')'
ext =~ Regexp.new(rgx, Regexp::IGNORECASE)
end
def output_ext(ext)
".html"
end
def convert(content)
setup
case @config['markdown']
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)
end
html
when 'maruku'
Maruku.new(content).to_html
end
end
end
end
end

View File

@ -1,50 +1,50 @@
module Jekyll
module Converters
class Textile < Converter
safe true
class TextileConverter < Converter
safe true
pygments_prefix '<notextile>'
pygments_suffix '</notextile>'
pygments_prefix '<notextile>'
pygments_suffix '</notextile>'
def setup
return if @setup
require 'redcloth'
@setup = true
rescue LoadError
STDERR.puts 'You are missing a library required for Textile. Please run:'
STDERR.puts ' $ [sudo] gem install RedCloth'
raise FatalException.new("Missing dependency: RedCloth")
end
def matches(ext)
rgx = '(' + @config['textile_ext'].gsub(',','|') +')'
ext =~ Regexp.new(rgx, Regexp::IGNORECASE)
end
def output_ext(ext)
".html"
end
def convert(content)
setup
# Shortcut if config doesn't contain RedCloth section
return RedCloth.new(content).to_html if @config['redcloth'].nil?
# List of attributes defined on RedCloth
# (from http://redcloth.rubyforge.org/classes/RedCloth/TextileDoc.html)
attrs = ['filter_classes', 'filter_html', 'filter_ids', 'filter_styles',
'hard_breaks', 'lite_mode', 'no_span_caps', 'sanitize_html']
r = RedCloth.new(content)
# Set attributes in r if they are NOT nil in the config
attrs.each do |attr|
r.instance_variable_set("@#{attr}".to_sym, @config['redcloth'][attr]) unless @config['redcloth'][attr].nil?
def setup
return if @setup
require 'redcloth'
@setup = true
rescue LoadError
STDERR.puts 'You are missing a library required for Textile. Please run:'
STDERR.puts ' $ [sudo] gem install RedCloth'
raise FatalException.new("Missing dependency: RedCloth")
end
r.to_html
def matches(ext)
rgx = '(' + @config['textile_ext'].gsub(',','|') +')'
ext =~ Regexp.new(rgx, Regexp::IGNORECASE)
end
def output_ext(ext)
".html"
end
def convert(content)
setup
# Shortcut if config doesn't contain RedCloth section
return RedCloth.new(content).to_html if @config['redcloth'].nil?
# List of attributes defined on RedCloth
# (from http://redcloth.rubyforge.org/classes/RedCloth/TextileDoc.html)
attrs = ['filter_classes', 'filter_html', 'filter_ids', 'filter_styles',
'hard_breaks', 'lite_mode', 'no_span_caps', 'sanitize_html']
r = RedCloth.new(content)
# Set attributes in r if they are NOT nil in the config
attrs.each do |attr|
r.instance_variable_set("@#{attr}".to_sym, @config['redcloth'][attr]) unless @config['redcloth'][attr].nil?
end
r.to_html
end
end
end
end

View File

@ -10,7 +10,7 @@ module Jekyll
# Returns the HTML formatted String.
def textilize(input)
site = @context.registers[:site]
converter = site.getConverterImpl(Jekyll::TextileConverter)
converter = site.getConverterImpl(Jekyll::Converters::Textile)
converter.convert(input)
end
@ -21,7 +21,7 @@ module Jekyll
# Returns the HTML formatted String.
def markdownify(input)
site = @context.registers[:site]
converter = site.getConverterImpl(Jekyll::MarkdownConverter)
converter = site.getConverterImpl(Jekyll::Converters::Markdown)
converter.convert(input)
end

View File

@ -17,16 +17,16 @@ class TestKramdown < Test::Unit::TestCase
# http://kramdown.rubyforge.org/converter/html.html#options
should "pass kramdown options" do
markdown = MarkdownConverter.new(@config)
markdown = Converters::Markdown.new(@config)
assert_equal "<h1>Some Header</h1>", markdown.convert('# Some Header #').strip
end
should "convert quotes to smart quotes" do
markdown = MarkdownConverter.new(@config)
markdown = Converters::Markdown.new(@config)
assert_equal "<p>&ldquo;Pit&rsquo;hy&rdquo;</p>", markdown.convert(%{"Pit'hy"}).strip
override = { 'kramdown' => { 'smart_quotes' => 'lsaquo,rsaquo,laquo,raquo' } }
markdown = MarkdownConverter.new(@config.deep_merge(override))
markdown = Converters::Markdown.new(@config.deep_merge(override))
assert_equal "<p>&laquo;Pit&rsaquo;hy&raquo;</p>", markdown.convert(%{"Pit'hy"}).strip
end
end

View File

@ -445,34 +445,34 @@ class TestPost < Test::Unit::TestCase
should "process .md as markdown under default configuration" do
post = setup_post '2011-04-12-md-extension.md'
conv = post.converter
assert conv.kind_of? Jekyll::MarkdownConverter
assert conv.kind_of? Jekyll::Converters::Markdown
end
should "process .text as indentity under default configuration" do
post = setup_post '2011-04-12-text-extension.text'
conv = post.converter
assert conv.kind_of? Jekyll::IdentityConverter
assert conv.kind_of? Jekyll::Converters::Identity
end
should "process .text as markdown under alternate configuration" do
@site.config['markdown_ext'] = 'markdown,mdw,mdwn,md,text'
post = setup_post '2011-04-12-text-extension.text'
conv = post.converter
assert conv.kind_of? Jekyll::MarkdownConverter
assert conv.kind_of? Jekyll::Converters::Markdown
end
should "process .md as markdown under alternate configuration" do
@site.config['markdown_ext'] = 'markdown,mkd,mkdn,md,text'
post = setup_post '2011-04-12-text-extension.text'
conv = post.converter
assert conv.kind_of? Jekyll::MarkdownConverter
assert conv.kind_of? Jekyll::Converters::Markdown
end
should "process .text as textile under alternate configuration" do
@site.config['textile_ext'] = 'textile,text'
post = setup_post '2011-04-12-text-extension.text'
conv = post.converter
assert conv.kind_of? Jekyll::TextileConverter
assert conv.kind_of? Jekyll::Converters::Textile
end
end

View File

@ -8,7 +8,7 @@ class TestRdiscount < Test::Unit::TestCase
'markdown' => 'rdiscount',
'rdiscount' => { 'extensions' => ['smart', 'generate_toc'], 'toc_token' => '{:toc}' }
}
@markdown = MarkdownConverter.new config
@markdown = Converters::Markdown.new config
end
should "pass rdiscount extensions" do

View File

@ -7,7 +7,7 @@ class TestRedcarpet < Test::Unit::TestCase
'redcarpet' => { 'extensions' => ['smart', 'strikethrough', 'filter_html'] },
'markdown' => 'redcarpet'
}
@markdown = MarkdownConverter.new config
@markdown = Converters::Markdown.new config
end
should "pass redcarpet options" do

View File

@ -4,7 +4,7 @@ class TestRedCloth < Test::Unit::TestCase
context "RedCloth default (no explicit config) hard_breaks enabled" do
setup do
@textile = TextileConverter.new
@textile = Converters::Textile.new
end
should "preserve single line breaks in HTML output" do
@ -17,7 +17,7 @@ class TestRedCloth < Test::Unit::TestCase
config = {
'redcloth' => {}
}
@textile = TextileConverter.new config
@textile = Converters::Textile.new config
end
should "preserve single line breaks in HTML output" do
@ -32,7 +32,7 @@ class TestRedCloth < Test::Unit::TestCase
'hard_breaks' => true # default
}
}
@textile = TextileConverter.new config
@textile = Converters::Textile.new config
end
should "preserve single line breaks in HTML output" do
@ -47,7 +47,7 @@ class TestRedCloth < Test::Unit::TestCase
'hard_breaks' => false
}
}
@textile = TextileConverter.new config
@textile = Converters::Textile.new config
end
should "not generate break tags in HTML output" do
@ -62,7 +62,7 @@ class TestRedCloth < Test::Unit::TestCase
'no_span_caps' => false
}
}
@textile = TextileConverter.new config
@textile = Converters::Textile.new config
end
should "generate span tags around capitalized words" do
assert_equal "<p><span class=\"caps\">NSC</span></p>", @textile.convert("NSC").strip
@ -76,7 +76,7 @@ class TestRedCloth < Test::Unit::TestCase
'no_span_caps' => true
}
}
@textile = TextileConverter.new config
@textile = Converters::Textile.new config
end
should "not generate span tags around capitalized words" do

View File

@ -4,7 +4,7 @@ require 'helper'
class TestTags < Test::Unit::TestCase
def create_post(content, override = {}, converter_class = Jekyll::MarkdownConverter)
def create_post(content, override = {}, converter_class = Jekyll::Converters::Markdown)
stub(Jekyll).configuration do
Jekyll::DEFAULTS.deep_merge({'pygments' => true}).deep_merge(override)
end
@ -129,7 +129,7 @@ CONTENT
context "using Textile" do
setup do
create_post(@content, {}, Jekyll::TextileConverter)
create_post(@content, {}, Jekyll::Converters::Textile)
end
# Broken in RedCloth 4.1.9