forked from jekyll/jekyll
-
Notifications
You must be signed in to change notification settings - Fork 3
/
kramdown_parser.rb
198 lines (169 loc) · 6.28 KB
/
kramdown_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Frozen-string-literal: true
module Kramdown
# A Kramdown::Document subclass meant to optimize memory usage from initializing
# a kramdown document for parsing.
#
# The optimization is by using the same options Hash (and its derivatives) for
# converting all Markdown documents in a Jekyll site.
class JekyllDocument < Document
class << self
attr_reader :options, :parser
# The implementation is basically the core logic in +Kramdown::Document#initialize+
#
# rubocop:disable Naming/MemoizedInstanceVariableName
def setup(options)
@cache ||= {}
# reset variables on a subsequent set up with a different options Hash
unless @cache[:id] == options.hash
@options = @parser = nil
@cache[:id] = options.hash
end
@options ||= Options.merge(options).freeze
@parser ||= begin
parser_name = (@options[:input] || "kramdown").to_s
parser_name = parser_name[0..0].upcase + parser_name[1..-1]
try_require("parser", parser_name)
if Parser.const_defined?(parser_name)
Parser.const_get(parser_name)
else
raise Kramdown::Error, "kramdown has no parser to handle the specified " \
"input format: #{@options[:input]}"
end
end
end
# rubocop:enable Naming/MemoizedInstanceVariableName
private
def try_require(type, name)
require "kramdown/#{type}/#{Utils.snake_case(name)}"
rescue LoadError
false
end
end
def initialize(source, options = {})
JekyllDocument.setup(options)
@options = JekyllDocument.options
@root, @warnings = JekyllDocument.parser.parse(source, @options)
end
# Use Kramdown::Converter::Html class to convert this document into HTML.
#
# The implementation is basically an optimized version of core logic in
# +Kramdown::Document#method_missing+ from kramdown-2.1.0.
def to_html
output, warnings = Kramdown::Converter::Html.convert(@root, @options)
@warnings.concat(warnings)
output
end
end
end
#
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",
}.freeze
def initialize(config)
@main_fallback_highlighter = config["highlighter"] || "rouge"
@config = config["kramdown"] || {}
@highlighter = nil
setup
load_dependencies
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["syntax_highlighter_opts"]["guess_lang"] = @config["guess_lang"]
@config["coderay"] ||= {} # XXX: Legacy.
modernize_coderay_config
end
def convert(content)
document = Kramdown::JekyllDocument.new(content, @config)
html_output = document.to_html
if @config["show_warnings"]
document.warnings.each do |warning|
Jekyll.logger.warn "Kramdown warning:", warning
end
end
html_output
end
private
def load_dependencies
require "kramdown-parser-gfm" if @config["input"] == "GFM"
if highlighter == "coderay"
Jekyll::External.require_with_graceful_fail("kramdown-syntax-coderay")
end
# `mathjax` emgine is bundled within kramdown-2.x and will be handled by
# kramdown itself.
if (math_engine = @config["math_engine"]) && math_engine != "mathjax"
Jekyll::External.require_with_graceful_fail("kramdown-math-#{math_engine}")
end
end
# config[kramdown][syntax_higlighter] >
# config[kramdown][enable_coderay] >
# config[highlighter]
# Where `enable_coderay` is now deprecated because Kramdown
# supports Rouge now too.
def highlighter
return @highlighter if @highlighter
if @config["syntax_highlighter"]
return @highlighter = @config[
"syntax_highlighter"
]
end
@highlighter = begin
if @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
def strip_coderay_prefix(hash)
hash.each_with_object({}) do |(key, val), hsh|
cleaned_key = key.to_s.gsub(%r!\Acoderay_!, "")
if key != cleaned_key
Jekyll::Deprecator.deprecation_message(
"You are using '#{key}'. Normalizing to #{cleaned_key}."
)
end
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.
def modernize_coderay_config
unless @config["coderay"].empty?
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
end