Respect pygments config option in Redcarpet renderer.
This commit is contained in:
parent
3fef9607bc
commit
2114bb3c2e
|
@ -2,6 +2,33 @@ 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
|
||||||
|
|
||||||
|
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)
|
def initialize(config)
|
||||||
require 'redcarpet'
|
require 'redcarpet'
|
||||||
require 'pygments'
|
require 'pygments'
|
||||||
|
@ -9,19 +36,12 @@ module Jekyll
|
||||||
@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)
|
WithPygments
|
||||||
lang = lang && lang.split.first || "text"
|
else
|
||||||
output = add_code_tags(
|
WithoutPygments
|
||||||
Pygments.highlight(code, :lexer => lang, :options => { :encoding => 'utf-8' }),
|
end
|
||||||
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
|
end
|
||||||
rescue LoadError
|
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:'
|
||||||
|
|
Loading…
Reference in New Issue