Merge pull request #4109 from jekyll/cleanup-kramdown-converter

Merge pull request 4109
This commit is contained in:
Parker Moore
2015-12-04 09:21:22 -08:00
6 changed files with 176 additions and 75 deletions

View File

@@ -65,20 +65,12 @@ module Jekyll
'kramdown' => {
'auto_ids' => true,
'footnote_nr' => 1,
'entity_output' => 'as_char',
'toc_levels' => '1..6',
'entity_output' => 'as_char',
'smart_quotes' => 'lsquo,rsquo,ldquo,rdquo',
'enable_coderay' => false,
'coderay' => {
'coderay_wrap' => 'div',
'coderay_line_numbers' => 'inline',
'coderay_line_number_start' => 1,
'coderay_tab_width' => 4,
'coderay_bold_every' => 10,
'coderay_css' => 'style'
}
'input' => "GFM",
'hard_wrap' => false,
'footnote_nr' => 1
}
}]

View File

@@ -1,33 +1,86 @@
# Frozen-string-literal: true
# Encoding: utf-8
module Jekyll
module Converters
class Markdown
class KramdownParser
CODERAY_DEFAULTS = {
"css" => "style",
"bold_every" => 10,
"line_numbers" => "inline",
"line_number_start" => 1,
"tab_width" => 4,
"wrap" => "div"
}
def initialize(config)
require 'kramdown'
@config = config
# If kramdown supported highlighter enabled, use that
highlighter = @config['highlighter']
if highlighter == 'rouge' || highlighter == 'coderay'
@config['kramdown']['syntax_highlighter'] ||= highlighter
end
rescue LoadError
STDERR.puts 'You are missing a library required for Markdown. Please run:'
STDERR.puts ' $ [sudo] gem install kramdown'
raise Errors::FatalException.new("Missing dependency: kramdown")
Jekyll::External.require_with_graceful_fail "kramdown"
@main_fallback_highlighter = config["highlighter"] || "rogue"
@config = config["kramdown"] || {}
setup
end
# Setup and normalize the configuration:
# * Create Kramdown if it doesn't exist.
# * Set syntax_highlighter, detecting enable_coderay and merging highlighter if none.
# * Merge kramdown[coderay] into syntax_highlighter_opts stripping coderay_.
# * Make sure `syntax_highlighter_opts` exists.
def setup
@config["syntax_highlighter"] ||= highlighter
@config["syntax_highlighter_opts"] ||= {}
@config["coderay"] ||= {} # XXX: Legacy.
modernize_coderay_config
end
def convert(content)
# Check for use of coderay
if @config['kramdown']['enable_coderay']
%w[wrap line_numbers line_numbers_start tab_width bold_every css default_lang].each do |opt|
key = "coderay_#{opt}"
@config['kramdown'][key] = @config['kramdown']['coderay'][key] unless @config['kramdown'].key?(key)
end
end
Kramdown::Document.new(content, Utils.symbolize_hash_keys(@config['kramdown'])).to_html
Kramdown::Document.new(content, @config).to_html
end
# config[kramdown][syntax_higlighter] > config[kramdown][enable_coderay] > config[highlighter]
# Where `enable_coderay` is now deprecated because Kramdown
# supports Rouge now too.
private
def highlighter
@highlighter ||= begin
if highlighter = @config["syntax_highlighter"] then highlighter
elsif @config.key?("enable_coderay") && @config["enable_coderay"]
Jekyll::Deprecator.deprecation_message "You are using 'enable_coderay', use syntax_highlighter: coderay in your configuration file."
"coderay"
else
@main_fallback_highlighter
end
end
end
private
def strip_coderay_prefix(hash)
hash.each_with_object({}) do |(key, val), hsh|
cleaned_key = key.gsub(/\Acoderay_/, "")
Jekyll::Deprecator.deprecation_message "You are using '#{key}'. Normalizing to #{cleaned_key}." if key != cleaned_key
hsh[cleaned_key] = val
end
end
# If our highlighter is CodeRay we go in to merge the CodeRay defaults
# with your "coderay" key if it's there, deprecating it in the
# process of you using it.
private
def modernize_coderay_config
if highlighter == "coderay"
Jekyll::Deprecator.deprecation_message "You are using 'kramdown.coderay' in your configuration, please use 'syntax_highlighter_opts' instead."
@config["syntax_highlighter_opts"] = begin
strip_coderay_prefix(
@config["syntax_highlighter_opts"] \
.merge(CODERAY_DEFAULTS) \
.merge(@config["coderay"])
)
end
end
end
end
end
end

View File

@@ -8,6 +8,10 @@ module Jekyll
SLUGIFY_DEFAULT_REGEXP = Regexp.new('[^[:alnum:]]+').freeze
SLUGIFY_PRETTY_REGEXP = Regexp.new("[^[:alnum:]._~!$&'()+,;=@]+").freeze
def strip_heredoc(str)
str.gsub(/^[ \t]{#{(str.scan(/^[ \t]*(?=\S)/).min || "").size}}/, "")
end
# Non-destructive version of deep_merge_hashes! See that method.
#
# Returns the merged hashes.