1
Fork 0
blog/_plugins/kramdown-with-pygments/kramdown_pygments.rb

90 lines
2.7 KiB
Ruby
Raw Normal View History

2014-10-22 13:05:34 +00:00
# We define the an additional option for the kramdown parser to look for
module Kramdown
module Options
define(:kramdown_default_lang, Symbol, nil, <<EOF)
Sets the default language for highlighting code blocks
If no language is set for a code block, the default language is used
instead. The value has to be one of the languages supported by pygments
or nil if no default language should be used.
Default: nil
Used by: PygmentsHtml converter
EOF
end
end
# This class is a plugin for kramdown, to make it use pygments instead of coderay
# It has nothing to do with Jekyll, it is simply used by the custom converter below
module Kramdown
module Converter
class PygmentsHtml < Html
begin
require 'pygments'
rescue LoadError
STDERR.puts 'You are missing a library required for syntax highlighting. Please run:'
STDERR.puts ' $ [sudo] gem install pygments'
raise FatalException.new("Missing dependency: Pygments")
end
def extract_code_language_with_fix!(attr)
lang = extract_code_language!(attr) || @options[:kramdown_default_lang]
lang = "bash" if lang == "shell"
lang
end
def convert_codeblock(el, indent)
attr = el.attr.dup
lang = extract_code_language_with_fix!(attr)
code = pygmentize(el.value, lang)
code_attr = {}
code_attr['class'] = "language-#{lang}" if lang
"#{' '*indent}<div class=\"highlight\"><pre#{html_attributes(attr)}><code#{html_attributes(code_attr)}>#{code}</code></pre></div>\n"
end
def convert_codespan(el, indent)
attr = el.attr.dup
lang = extract_code_language_with_fix!(attr)
code = pygmentize(el.value, lang)
if lang
attr['class'] = "highlight"
if attr.has_key?('class')
attr['class'] += " language-#{lang}"
else
attr['class'] = "language-#{lang}"
end
end
"<code#{html_attributes(attr)}>#{code}</code>"
end
def pygmentize(code, lang)
if lang
Pygments.highlight(code,
:lexer => lang,
:options => { :startinline => true, :encoding => 'utf-8', :nowrap => true })
else
escape_html(code)
end
end
end
end
end
# This class is the actual custom Jekyll converter.
class Jekyll::Converters::Markdown::KramdownPygments
def initialize(config)
require 'kramdown'
@config = config
rescue LoadError
STDERR.puts 'You are missing a library required for Markdown. Please run:'
STDERR.puts ' $ [sudo] gem install kramdown'
raise FatalException.new("Missing dependency: kramdown")
end
def convert(content)
2015-07-14 22:15:04 +00:00
Kramdown::Document.new(content, @config['kramdown']).to_pygments_html
2014-10-22 13:05:34 +00:00
end
end