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

View File

@ -26,14 +26,36 @@ 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
end
context "with pygments enabled" do
setup do
@markdown.config['pygments'] = true
end
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(
<<-EOS
```ruby
puts "Hello world"
```
EOS
EOS
).strip
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