From 663a1321d25379d49a6b18caf0cf2c0d353da3e0 Mon Sep 17 00:00:00 2001 From: Tom Bell Date: Sat, 19 Jan 2013 23:07:38 +0000 Subject: [PATCH] Update converter classes moving into a module --- lib/jekyll/converter.rb | 4 +- lib/jekyll/converters/identity.rb | 29 ++-- lib/jekyll/converters/markdown.rb | 270 +++++++++++++++--------------- lib/jekyll/converters/textile.rb | 84 +++++----- lib/jekyll/filters.rb | 4 +- test/test_kramdown.rb | 6 +- test/test_post.rb | 10 +- test/test_rdiscount.rb | 2 +- test/test_redcarpet.rb | 2 +- test/test_redcloth.rb | 12 +- test/test_tags.rb | 4 +- 11 files changed, 212 insertions(+), 215 deletions(-) diff --git a/lib/jekyll/converter.rb b/lib/jekyll/converter.rb index c2528d14..e2dc2796 100644 --- a/lib/jekyll/converter.rb +++ b/lib/jekyll/converter.rb @@ -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 \ No newline at end of file +end diff --git a/lib/jekyll/converters/identity.rb b/lib/jekyll/converters/identity.rb index 7d9628ca..69171b00 100644 --- a/lib/jekyll/converters/identity.rb +++ b/lib/jekyll/converters/identity.rb @@ -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 diff --git a/lib/jekyll/converters/markdown.rb b/lib/jekyll/converters/markdown.rb index 3c6ba539..14a7d4da 100644 --- a/lib/jekyll/converters/markdown.rb +++ b/lib/jekyll/converters/markdown.rb @@ -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(/
/,'
')
+                  code = code.sub(/<\/pre>/,"
") + end end - def add_code_tags(code, lang) - code = code.sub(/
/,'
')
-                code = code.sub(/<\/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 diff --git a/lib/jekyll/converters/textile.rb b/lib/jekyll/converters/textile.rb index 6572b518..54e93749 100644 --- a/lib/jekyll/converters/textile.rb +++ b/lib/jekyll/converters/textile.rb @@ -1,50 +1,50 @@ module Jekyll + module Converters + class Textile < Converter + safe true - class TextileConverter < Converter - safe true + pygments_prefix '' + pygments_suffix '' - pygments_prefix '' - pygments_suffix '' - - 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 diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 0012c25f..bcd8d2e7 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -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 diff --git a/test/test_kramdown.rb b/test/test_kramdown.rb index 043752c3..8aa72faf 100644 --- a/test/test_kramdown.rb +++ b/test/test_kramdown.rb @@ -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 "

Some Header

", markdown.convert('# Some Header #').strip end should "convert quotes to smart quotes" do - markdown = MarkdownConverter.new(@config) + markdown = Converters::Markdown.new(@config) assert_equal "

“Pit’hy”

", 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 "

«Pit›hy»

", markdown.convert(%{"Pit'hy"}).strip end end diff --git a/test/test_post.rb b/test/test_post.rb index 16aff4f1..b29db02a 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -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 diff --git a/test/test_rdiscount.rb b/test/test_rdiscount.rb index 01f18eb3..ed2c6f98 100644 --- a/test/test_rdiscount.rb +++ b/test/test_rdiscount.rb @@ -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 diff --git a/test/test_redcarpet.rb b/test/test_redcarpet.rb index 05cb0445..74204136 100644 --- a/test/test_redcarpet.rb +++ b/test/test_redcarpet.rb @@ -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 diff --git a/test/test_redcloth.rb b/test/test_redcloth.rb index 55e52cdf..6e635b96 100644 --- a/test/test_redcloth.rb +++ b/test/test_redcloth.rb @@ -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 "

NSC

", @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 diff --git a/test/test_tags.rb b/test/test_tags.rb index 816fd809..3d5e80e4 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -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