Respect pygments config option in Redcarpet renderer.

This commit is contained in:
Parker Moore 2013-05-08 00:10:18 +02:00
parent 3fef9607bc
commit 2114bb3c2e
1 changed files with 32 additions and 12 deletions

View File

@ -2,14 +2,16 @@ module Jekyll
module Converters
class Markdown
class RedcarpetParser
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
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
class WithPygments < Redcarpet::Render::HTML
include CommonMethods
def block_code(code, lang)
lang = lang && lang.split.first || "text"
output = add_code_tags(
@ -17,11 +19,29 @@ module Jekyll
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
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'
@config = config
@redcarpet_extensions = {}
@config['redcarpet']['extensions'].each { |e| @redcarpet_extensions[e.to_sym] = true }
@renderer ||= if @config['pygments']
WithPygments
else
WithoutPygments
end
end
rescue LoadError
STDERR.puts 'You are missing a library required for Markdown. Please run:'