Using modules instead of classes for Redcarpet with/without Pygments.

This commit is contained in:
Parker Moore 2013-05-08 00:28:51 +02:00
parent 2114bb3c2e
commit 62b4fd77ac
2 changed files with 37 additions and 13 deletions

View File

@ -10,9 +10,10 @@ module Jekyll
end end
end end
class WithPygments < Redcarpet::Render::HTML module WithPygments
include CommonMethods include CommonMethods
def block_code(code, lang) def block_code(code, lang)
require 'pygments'
lang = lang && lang.split.first || "text" lang = lang && lang.split.first || "text"
output = add_code_tags( output = add_code_tags(
Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }), Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }),
@ -21,7 +22,7 @@ module Jekyll
end end
end end
class WithoutPygments < Redcarpet::Render::HTML module WithoutPygments
include CommonMethods include CommonMethods
def block_code(code, lang) def block_code(code, lang)
lang = lang && lang.split.first || "text" lang = lang && lang.split.first || "text"
@ -31,19 +32,20 @@ module Jekyll
def initialize(config) def initialize(config)
require 'redcarpet' require 'redcarpet'
require 'pygments'
@config = config @config = config
@redcarpet_extensions = {} @redcarpet_extensions = {}
@config['redcarpet']['extensions'].each { |e| @redcarpet_extensions[e.to_sym] = true } @config['redcarpet']['extensions'].each { |e| @redcarpet_extensions[e.to_sym] = true }
@renderer ||= if @config['pygments'] @renderer ||= if @config['pygments']
WithPygments Class.new(Redcarpet::Render::HTML) do
include WithPygments
end
else else
WithoutPygments Class.new(Redcarpet::Render::HTML) do
include WithoutPygments
end end
end end
rescue LoadError rescue LoadErro
STDERR.puts 'You are missing a library required for Markdown. Please run:' STDERR.puts 'You are missing a library required for Markdown. Please run:'
STDERR.puts ' $ [sudo] gem install redcarpet' STDERR.puts ' $ [sudo] gem install redcarpet'
raise FatalException.new("Missing dependency: redcarpet") raise FatalException.new("Missing dependency: redcarpet")

View File

@ -26,6 +26,11 @@ class TestRedcarpet < Test::Unit::TestCase
assert_equal "<p><strong>bad code not here</strong>: i am bad</p>", @markdown.convert('**bad code not here**: <script>i am bad</script>').strip assert_equal "<p><strong>bad code not here</strong>: i am bad</p>", @markdown.convert('**bad code not here**: <script>i am bad</script>').strip
end end
context "with pygments enabled" do
setup do
@markdown.config['pygments'] = true
end
should "render fenced code blocks" do should "render fenced code blocks" do
assert_equal "<div class=\"highlight\"><pre><code class=\"ruby language-ruby\"><span class=\"nb\">puts</span> <span class=\"s2\">&quot;Hello world&quot;</span>\n</code></pre></div>", @markdown.convert( assert_equal "<div class=\"highlight\"><pre><code class=\"ruby language-ruby\"><span class=\"nb\">puts</span> <span class=\"s2\">&quot;Hello world&quot;</span>\n</code></pre></div>", @markdown.convert(
<<-EOS <<-EOS
@ -36,4 +41,21 @@ EOS
).strip ).strip
end end
end end
context "with pygments disabled" do
setup do
@markdown.config['pygments'] = false
end
should "render fenced code blocks" do
assert_equal "<div class=\"highlight\"><pre><code class=\"ruby language-ruby\">puts &quot;Hello world&quot;\n</code></pre></div>", @markdown.convert(
<<-EOS
```ruby
puts "Hello world"
```
EOS
).strip
end
end
end
end end