Merge pull request #1053 from mojombo/redcarpet-respect-pygments
Respect pygments config option in Redcarpet renderer.
This commit is contained in:
commit
cb0054b6d7
|
@ -2,28 +2,57 @@ module Jekyll
|
||||||
module Converters
|
module Converters
|
||||||
class Markdown
|
class Markdown
|
||||||
class RedcarpetParser
|
class RedcarpetParser
|
||||||
|
|
||||||
|
module CommonMethods
|
||||||
|
def add_code_tags(code, lang)
|
||||||
|
code = code.sub(/<pre>/, "<pre><code class=\"#{lang} language-#{lang}\">")
|
||||||
|
code = code.sub(/<\/pre>/,"</code></pre>")
|
||||||
|
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)
|
||||||
|
"<div class=\"highlight\"><pre>#{CGI::escapeHTML(code)}</pre></div>"
|
||||||
|
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)
|
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 ||= Class.new(Redcarpet::Render::HTML) do
|
@renderer ||= if @config['pygments']
|
||||||
def block_code(code, lang)
|
Class.new(Redcarpet::Render::HTML) do
|
||||||
lang = lang && lang.split.first || "text"
|
include WithPygments
|
||||||
output = add_code_tags(
|
end
|
||||||
Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }),
|
else
|
||||||
lang
|
Class.new(Redcarpet::Render::HTML) do
|
||||||
)
|
include WithoutPygments
|
||||||
end
|
end
|
||||||
|
end
|
||||||
def add_code_tags(code, lang)
|
rescue LoadErro
|
||||||
code = code.sub(/<pre>/, "<pre><code class=\"#{lang} language-#{lang}\">")
|
|
||||||
code = code.sub(/<\/pre>/,"</code></pre>")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
rescue LoadError
|
|
||||||
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")
|
||||||
|
|
|
@ -3,11 +3,11 @@ require 'helper'
|
||||||
class TestRedcarpet < Test::Unit::TestCase
|
class TestRedcarpet < Test::Unit::TestCase
|
||||||
context "redcarpet" do
|
context "redcarpet" do
|
||||||
setup do
|
setup do
|
||||||
config = {
|
@config = {
|
||||||
'redcarpet' => { 'extensions' => ['smart', 'strikethrough', 'filter_html'] },
|
'redcarpet' => { 'extensions' => ['smart', 'strikethrough', 'filter_html'] },
|
||||||
'markdown' => 'redcarpet'
|
'markdown' => 'redcarpet'
|
||||||
}
|
}
|
||||||
@markdown = Converters::Markdown.new config
|
@markdown = Converters::Markdown.new @config
|
||||||
end
|
end
|
||||||
|
|
||||||
should "pass redcarpet options" do
|
should "pass redcarpet options" do
|
||||||
|
@ -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
|
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
|
||||||
|
|
||||||
should "render fenced code blocks" do
|
context "with pygments enabled" 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(
|
setup do
|
||||||
<<-EOS
|
@markdown = Converters::Markdown.new @config.merge({ 'pygments' => true })
|
||||||
|
end
|
||||||
|
|
||||||
|
should "render fenced code blocks with syntax highlighting" 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
|
||||||
```ruby
|
```ruby
|
||||||
puts "Hello world"
|
puts "Hello world"
|
||||||
```
|
```
|
||||||
EOS
|
EOS
|
||||||
).strip
|
).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 "<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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue