From 2114bb3c2ebab040d712b021ee2386fc6d65a714 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 8 May 2013 00:10:18 +0200 Subject: [PATCH 1/4] Respect pygments config option in Redcarpet renderer. --- .../converters/markdown/redcarpet_parser.rb | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/jekyll/converters/markdown/redcarpet_parser.rb b/lib/jekyll/converters/markdown/redcarpet_parser.rb index b7a25c46..37e06331 100644 --- a/lib/jekyll/converters/markdown/redcarpet_parser.rb +++ b/lib/jekyll/converters/markdown/redcarpet_parser.rb @@ -2,6 +2,33 @@ module Jekyll module Converters class Markdown class RedcarpetParser + + module CommonMethods + def add_code_tags(code, lang) + code = code.sub(/
/, "
")
+            code = code.sub(/<\/pre>/,"
") + end + end + + class WithPygments < Redcarpet::Render::HTML + include CommonMethods + def block_code(code, lang) + lang = lang && lang.split.first || "text" + output = add_code_tags( + Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }), + lang + ) + end + end + + class WithoutPygments < Redcarpet::Render::HTML + include CommonMethods + def block_code(code, lang) + lang = lang && lang.split.first || "text" + output = add_code_tags(code, lang) + end + end + def initialize(config) require 'redcarpet' require 'pygments' @@ -9,19 +36,12 @@ module Jekyll @redcarpet_extensions = {} @config['redcarpet']['extensions'].each { |e| @redcarpet_extensions[e.to_sym] = true } - @renderer ||= Class.new(Redcarpet::Render::HTML) do - def block_code(code, lang) - lang = lang && lang.split.first || "text" - output = add_code_tags( - Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }), - lang - ) - end + @renderer ||= if @config['pygments'] + WithPygments + else + WithoutPygments + end - def add_code_tags(code, lang) - code = code.sub(/
/, "
")
-              code = code.sub(/<\/pre>/,"
") - end end rescue LoadError STDERR.puts 'You are missing a library required for Markdown. Please run:' From 62b4fd77ac1c29f22b5f6d93febd6a0698671daa Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 8 May 2013 00:28:51 +0200 Subject: [PATCH 2/4] Using modules instead of classes for Redcarpet with/without Pygments. --- .../converters/markdown/redcarpet_parser.rb | 18 ++++++----- test/test_redcarpet.rb | 32 ++++++++++++++++--- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/lib/jekyll/converters/markdown/redcarpet_parser.rb b/lib/jekyll/converters/markdown/redcarpet_parser.rb index 37e06331..4d74f326 100644 --- a/lib/jekyll/converters/markdown/redcarpet_parser.rb +++ b/lib/jekyll/converters/markdown/redcarpet_parser.rb @@ -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 - - 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") diff --git a/test/test_redcarpet.rb b/test/test_redcarpet.rb index 5c7b6da1..b65d2d3e 100644 --- a/test/test_redcarpet.rb +++ b/test/test_redcarpet.rb @@ -26,14 +26,36 @@ class TestRedcarpet < Test::Unit::TestCase assert_equal "

bad code not here: i am bad

", @markdown.convert('**bad code not here**: ').strip end - should "render fenced code blocks" do - assert_equal "
puts "Hello world"\n
", @markdown.convert( - <<-EOS + context "with pygments enabled" do + setup do + @markdown.config['pygments'] = true + end + + should "render fenced code blocks" do + assert_equal "
puts "Hello world"\n
", @markdown.convert( + <<-EOS ```ruby puts "Hello world" ``` -EOS - ).strip + EOS + ).strip + end + end + + context "with pygments disabled" do + setup do + @markdown.config['pygments'] = false + end + + should "render fenced code blocks" do + assert_equal "
puts "Hello world"\n
", @markdown.convert( + <<-EOS +```ruby +puts "Hello world" +``` + EOS + ).strip + end end end end From f69c39c3aceaee10ecdf81eb5dc0249f3294a206 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 8 May 2013 00:45:42 +0200 Subject: [PATCH 3/4] fix up tests for pygments-aware redcarpet --- test/test_redcarpet.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/test_redcarpet.rb b/test/test_redcarpet.rb index b65d2d3e..ce4cb826 100644 --- a/test/test_redcarpet.rb +++ b/test/test_redcarpet.rb @@ -3,11 +3,11 @@ require 'helper' class TestRedcarpet < Test::Unit::TestCase context "redcarpet" do setup do - config = { + @config = { 'redcarpet' => { 'extensions' => ['smart', 'strikethrough', 'filter_html'] }, 'markdown' => 'redcarpet' } - @markdown = Converters::Markdown.new config + @markdown = Converters::Markdown.new @config end should "pass redcarpet options" do @@ -28,10 +28,10 @@ class TestRedcarpet < Test::Unit::TestCase context "with pygments enabled" do setup do - @markdown.config['pygments'] = true + @markdown = Converters::Markdown.new @config.merge({ 'pygments' => true }) end - should "render fenced code blocks" do + should "render fenced code blocks with syntax highlighting" do assert_equal "
puts "Hello world"\n
", @markdown.convert( <<-EOS ```ruby @@ -44,10 +44,10 @@ puts "Hello world" context "with pygments disabled" do setup do - @markdown.config['pygments'] = false + @markdown = Converters::Markdown.new @config.merge({ 'pygments' => false }) end - should "render fenced code blocks" do + should "render fenced code blocks without syntax highlighting" do assert_equal "
puts "Hello world"\n
", @markdown.convert( <<-EOS ```ruby From 739f7525802f343ae673f9b921b641c5bb5acf5b Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 8 May 2013 00:45:55 +0200 Subject: [PATCH 4/4] Properly wrap and encode non-pygmentized code. --- lib/jekyll/converters/markdown/redcarpet_parser.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/jekyll/converters/markdown/redcarpet_parser.rb b/lib/jekyll/converters/markdown/redcarpet_parser.rb index 4d74f326..ceecd85e 100644 --- a/lib/jekyll/converters/markdown/redcarpet_parser.rb +++ b/lib/jekyll/converters/markdown/redcarpet_parser.rb @@ -23,10 +23,17 @@ module Jekyll end module WithoutPygments + require 'cgi' + include CommonMethods + + def code_wrap(code) + "
#{CGI::escapeHTML(code)}
" + end + def block_code(code, lang) lang = lang && lang.split.first || "text" - output = add_code_tags(code, lang) + output = add_code_tags(code_wrap(code), lang) end end