Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect pygments config option in Redcarpet renderer. #1053

Merged
merged 4 commits into from
May 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions lib/jekyll/converters/markdown/redcarpet_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,57 @@ module Jekyll
module Converters
class Markdown
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)
require 'redcarpet'
require 'pygments'
@config = config
@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

def add_code_tags(code, lang)
code = code.sub(/<pre>/, "<pre><code class=\"#{lang} language-#{lang}\">")
code = code.sub(/<\/pre>/,"</code></pre>")
end
end
rescue LoadError
@renderer ||= if @config['pygments']
Class.new(Redcarpet::Render::HTML) do
include WithPygments
end
else
Class.new(Redcarpet::Render::HTML) do
include WithoutPygments
end
end
rescue LoadErro

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, good catch :)

STDERR.puts 'You are missing a library required for Markdown. Please run:'
STDERR.puts ' $ [sudo] gem install redcarpet'
raise FatalException.new("Missing dependency: redcarpet")
Expand Down
36 changes: 29 additions & 7 deletions test/test_redcarpet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
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
Expand All @@ -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

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
context "with pygments enabled" do
setup do
@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\">&quot;Hello world&quot;</span>\n</code></pre></div>", @markdown.convert(
<<-EOS
```ruby
puts "Hello world"
```
EOS
).strip
EOS
).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 &quot;Hello world&quot;\n</code></pre></div>", @markdown.convert(
<<-EOS
```ruby
puts "Hello world"
```
EOS
).strip
end
end
end
end