diff --git a/lib/jekyll/converters/markdown/redcarpet_parser.rb b/lib/jekyll/converters/markdown/redcarpet_parser.rb index b7a25c46..ceecd85e 100644 --- a/lib/jekyll/converters/markdown/redcarpet_parser.rb +++ b/lib/jekyll/converters/markdown/redcarpet_parser.rb @@ -2,28 +2,57 @@ module Jekyll module Converters class Markdown class RedcarpetParser + + module CommonMethods + def add_code_tags(code, lang) + code = code.sub(/
/, "") + end + end + + module WithPygments + include CommonMethods + def block_code(code, lang) + require 'pygments' + lang = lang && lang.split.first || "text" + output = add_code_tags( + Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }), + lang + ) + end + end + + module WithoutPygments + require 'cgi' + + include CommonMethods + + def code_wrap(code) + "") + code = code.sub(/<\/pre>/,"
" + end + + def block_code(code, lang) + lang = lang && lang.split.first || "text" + output = add_code_tags(code_wrap(code), lang) + end + end + def initialize(config) require 'redcarpet' - require 'pygments' @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(/#{CGI::escapeHTML(code)}/, "") - end - end - rescue LoadError + @renderer ||= if @config['pygments'] + Class.new(Redcarpet::Render::HTML) do + include WithPygments + end + else + Class.new(Redcarpet::Render::HTML) do + include WithoutPygments + end + end + rescue LoadErro STDERR.puts 'You are missing a library required for Markdown. Please run:' STDERR.puts ' $ [sudo] gem install redcarpet' raise FatalException.new("Missing dependency: redcarpet") diff --git a/test/test_redcarpet.rb b/test/test_redcarpet.rb index 5c7b6da1..ce4cb826 100644 --- a/test/test_redcarpet.rb +++ b/test/test_redcarpet.rb @@ -3,11 +3,11 @@ require 'helper' class TestRedcarpet < Test::Unit::TestCase context "redcarpet" do setup do - config = { + @config = { 'redcarpet' => { 'extensions' => ['smart', 'strikethrough', 'filter_html'] }, 'markdown' => 'redcarpet' } - @markdown = Converters::Markdown.new config + @markdown = Converters::Markdown.new @config end should "pass redcarpet options" do @@ -26,14 +26,36 @@ class TestRedcarpet < Test::Unit::TestCase assert_equal "") - code = code.sub(/<\/pre>/,"
bad code not here: i am bad
", @markdown.convert('**bad code not here**: ').strip end - should "render fenced code blocks" do - assert_equal "", @markdown.convert( - <<-EOS + context "with pygments enabled" do + setup do + @markdown = Converters::Markdown.new @config.merge({ 'pygments' => true }) + end + + should "render fenced code blocks with syntax highlighting" do + assert_equal "puts "Hello world"\n
", @markdown.convert( + <<-EOS ```ruby puts "Hello world" ``` -EOS - ).strip + EOS + ).strip + end + end + + context "with pygments disabled" do + setup do + @markdown = Converters::Markdown.new @config.merge({ 'pygments' => false }) + end + + should "render fenced code blocks without syntax highlighting" do + assert_equal "puts "Hello world"\n
", @markdown.convert( + <<-EOS +```ruby +puts "Hello world" +``` + EOS + ).strip + end end end endputs "Hello world"\n