Class: Kramdown::BridgetownDocument
- Inherits:
-
Document
- Object
- Document
- Kramdown::BridgetownDocument
- Defined in:
- lib/bridgetown-core/converters/markdown/kramdown_parser.rb
Overview
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 Bridgetown site.
Class Attribute Summary collapse
-
.options ⇒ Object
readonly
Returns the value of attribute options.
-
.parser ⇒ Object
readonly
Returns the value of attribute parser.
Class Method Summary collapse
-
.setup(options) ⇒ Object
The implementation is basically the core logic in +Kramdown::Document#initialize+.
Instance Method Summary collapse
-
#initialize(source, options = {}) ⇒ BridgetownDocument
constructor
rubocop:disable Lint/MissingSuper.
-
#to_html ⇒ Object
Use Kramdown::Converter::Html class to convert this document into HTML.
Constructor Details
#initialize(source, options = {}) ⇒ BridgetownDocument
rubocop:disable Lint/MissingSuper
47 48 49 50 51 52 |
# File 'lib/bridgetown-core/converters/markdown/kramdown_parser.rb', line 47 def initialize(source, = {}) # rubocop:disable Lint/MissingSuper BridgetownDocument.setup() @options = BridgetownDocument. @root, @warnings = BridgetownDocument.parser.parse(source, @options) end |
Class Attribute Details
.options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/bridgetown-core/converters/markdown/kramdown_parser.rb', line 11 def @options end |
.parser ⇒ Object (readonly)
Returns the value of attribute parser.
11 12 13 |
# File 'lib/bridgetown-core/converters/markdown/kramdown_parser.rb', line 11 def parser @parser end |
Class Method Details
.setup(options) ⇒ Object
The implementation is basically the core logic in +Kramdown::Document#initialize+
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bridgetown-core/converters/markdown/kramdown_parser.rb', line 14 def setup() @cache ||= {} # reset variables on a subsequent set up with a different options Hash unless @cache[:id] == .hash @options = @parser = nil @cache[:id] = .hash end @options ||= Options.merge().freeze @parser ||= begin parser_name = (@options[:input] || "kramdown").to_s parser_name = parser_name[0..0].upcase + parser_name[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 |
Instance Method Details
#to_html ⇒ Object
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.
58 59 60 61 62 |
# File 'lib/bridgetown-core/converters/markdown/kramdown_parser.rb', line 58 def to_html output, warnings = Kramdown::Converter::Html.convert(@root, @options) @warnings.concat(warnings) output end |