Update Redcarpet support to version 2 #570

Closed
wants to merge 7 commits into
from
View
@@ -37,7 +37,7 @@ Gem::Specification.new do |s|
s.add_development_dependency('cucumber', "1.1")
s.add_development_dependency('RedCloth', "~> 4.2")
s.add_development_dependency('rdiscount', "~> 1.6")
- s.add_development_dependency('redcarpet', "~> 1.9")
+ s.add_development_dependency('redcarpet', "~> 2.1.1")
# = MANIFEST =
s.files = %w[
@@ -13,7 +13,24 @@ def setup
when 'redcarpet'
begin
require 'redcarpet'
- @redcarpet_extensions = @config['redcarpet']['extensions'].map { |e| e.to_sym }
+
+ @renderer = Class.new(Redcarpet::Render::HTML) do
+ def block_code(code, lang)
+ lang ||= 'text'
+ output = add_code_tags(
+ Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }),
+ lang
+ )
+ end
+
+ def add_code_tags(code, lang)
+ code = code.sub(/<pre>/,'<pre><code class="' + lang + '">')
+ code = code.sub(/<\/pre>/,"</code></pre>")
+ end
+ end
+
+ @redcarpet_extensions = {}
+ @config['redcarpet']['extensions'].each { |e| @redcarpet_extensions[e.to_sym] = true }
rescue LoadError
STDERR.puts 'You are missing a library required for Markdown. Please run:'
STDERR.puts ' $ [sudo] gem install redcarpet'
@@ -30,8 +47,6 @@ def setup
when 'rdiscount'
begin
require 'rdiscount'
-
- # Load rdiscount extensions
@rdiscount_extensions = @config['rdiscount']['extensions'].map { |e| e.to_sym }
rescue LoadError
STDERR.puts 'You are missing a library required for Markdown. Please run:'
@@ -88,7 +103,10 @@ def convert(content)
setup
case @config['markdown']
when 'redcarpet'
- Redcarpet.new(content, *@redcarpet_extensions).to_html
+ @redcarpet_extensions[:fenced_code_blocks] = !@redcarpet_extensions[:no_fenced_code_blocks]
+ @renderer.send :include, Redcarpet::Render::SmartyPants if @redcarpet_extensions[:smart]
+ markdown = Redcarpet::Markdown.new(@renderer.new(@redcarpet_extensions), @redcarpet_extensions)
+ markdown.render(content)
when 'kramdown'
# Check for use of coderay
if @config['kramdown']['use_coderay']
View
@@ -4,7 +4,7 @@ class TestRedcarpet < Test::Unit::TestCase
context "redcarpet" do
setup do
config = {
- 'redcarpet' => { 'extensions' => ['smart'] },
+ 'redcarpet' => { 'extensions' => ['smart', 'strikethrough', 'filter_html'] },
'markdown' => 'redcarpet'
}
@markdown = MarkdownConverter.new config
@@ -14,8 +14,26 @@ class TestRedcarpet < Test::Unit::TestCase
assert_equal "<h1>Some Header</h1>", @markdown.convert('# Some Header #').strip
end
- should "pass redcarpet extensions" do
+ should "pass redcarpet SmartyPants options" do
assert_equal "<p>&ldquo;smart&rdquo;</p>", @markdown.convert('"smart"').strip
end
+
+ should "pass redcarpet extensions" do
+ assert_equal "<p><del>deleted</del></p>", @markdown.convert('~~deleted~~').strip
+ end
+
+ should "pass redcarpet render options" do
+ 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
+
+ should "render fenced code blocks" do
+ assert_equal "<div class=\"highlight\"><pre><code class=\"ruby\"><span class=\"nb\">puts</span> <span class=\"s2\">&quot;Hello world&quot;</span>\n</code></pre>\n</div>", @markdown.convert(
+ <<-EOS
+```ruby
+puts "Hello world"
+```
+EOS
+ ).strip
+ end
end
end