Using modules instead of classes for Redcarpet with/without Pygments.
This commit is contained in:
parent
2114bb3c2e
commit
62b4fd77ac
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
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\">"Hello world"</span>\n</code></pre></div>", @markdown.convert(
|
||||
<<-EOS
|
||||
|
@ -36,4 +41,21 @@ 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 "Hello world"\n</code></pre></div>", @markdown.convert(
|
||||
<<-EOS
|
||||
```ruby
|
||||
puts "Hello world"
|
||||
```
|
||||
EOS
|
||||
).strip
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue