From 62b4fd77ac1c29f22b5f6d93febd6a0698671daa Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 8 May 2013 00:28:51 +0200 Subject: [PATCH] Using modules instead of classes for Redcarpet with/without Pygments. --- .../converters/markdown/redcarpet_parser.rb | 18 ++++++----- test/test_redcarpet.rb | 32 ++++++++++++++++--- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/lib/jekyll/converters/markdown/redcarpet_parser.rb b/lib/jekyll/converters/markdown/redcarpet_parser.rb index 37e06331..4d74f326 100644 --- a/lib/jekyll/converters/markdown/redcarpet_parser.rb +++ b/lib/jekyll/converters/markdown/redcarpet_parser.rb @@ -10,9 +10,10 @@ module Jekyll end end - class WithPygments < Redcarpet::Render::HTML + 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' }), @@ -21,7 +22,7 @@ module Jekyll end end - class WithoutPygments < Redcarpet::Render::HTML + module WithoutPygments include CommonMethods def block_code(code, lang) lang = lang && lang.split.first || "text" @@ -31,19 +32,20 @@ module Jekyll def initialize(config) require 'redcarpet' - require 'pygments' @config = config @redcarpet_extensions = {} @config['redcarpet']['extensions'].each { |e| @redcarpet_extensions[e.to_sym] = true } @renderer ||= if @config['pygments'] - WithPygments + Class.new(Redcarpet::Render::HTML) do + include WithPygments + end else - WithoutPygments + Class.new(Redcarpet::Render::HTML) do + include WithoutPygments + end end - - end - rescue LoadError + 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..b65d2d3e 100644 --- a/test/test_redcarpet.rb +++ b/test/test_redcarpet.rb @@ -26,14 +26,36 @@ class TestRedcarpet < Test::Unit::TestCase assert_equal "

bad code not here: i am bad

", @markdown.convert('**bad code not here**: ').strip end - should "render fenced code blocks" do - assert_equal "
puts "Hello world"\n
", @markdown.convert( - <<-EOS + context "with pygments enabled" do + setup do + @markdown.config['pygments'] = true + end + + should "render fenced code blocks" 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.config['pygments'] = false + end + + should "render fenced code blocks" do + assert_equal "
puts "Hello world"\n
", @markdown.convert( + <<-EOS +```ruby +puts "Hello world" +``` + EOS + ).strip + end end end end