Class: Kramdown::Document
- Inherits:
-
Object
- Object
- Kramdown::Document
- Defined in:
- lib/kramdown/document.rb
Overview
The main interface to kramdown.
This class provides a one-stop-shop for using kramdown to convert text into various output formats. Use it like this:
require 'kramdown'
doc = Kramdown::Document.new('This *is* some kramdown text')
puts doc.to_html
The #to_html method is a shortcut for using the Converter::Html class. See #method_missing for more information.
The second argument to the ::new method is an options hash for customizing the behaviour of the used parser and the converter. See ::new for more information!
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
The options hash which holds the options for parsing/converting the Kramdown document.
-
#root ⇒ Object
The root Element of the element tree.
-
#warnings ⇒ Object
readonly
An array of warning messages.
Instance Method Summary collapse
-
#initialize(source, options = {}) ⇒ Document
constructor
Create a new Kramdown document from the string
source
and use the providedoptions
. -
#inspect ⇒ Object
:nodoc:.
-
#method_missing(id, *attr, &block) ⇒ Object
Check if a method is invoked that begins with
to_
and if so, try to instantiate a converter class (i.e. a class in the Kramdown::Converter module) and use it for converting the document.
Constructor Details
#initialize(source, options = {}) ⇒ Document
Create a new Kramdown document from the string source
and use the provided options
. The options that can be used are defined in the Options module.
The special options key :input can be used to select the parser that should parse the source
. It has to be the name of a class in the Kramdown::Parser module. For example, to select the kramdown parser, one would set the :input key to Kramdown
. If this key is not set, it defaults to Kramdown
.
The source
is immediately parsed by the selected parser so that the root element is immediately available and the output can be generated.
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/kramdown/document.rb', line 87 def initialize(source, = {}) @options = Options.merge().freeze parser = ([:input] || 'kramdown').to_s parser = parser[0..0].upcase + parser[1..-1] if Parser.const_defined?(parser) @root, @warnings = Parser.const_get(parser).parse(source, @options) else raise Kramdown::Error.new("kramdown has no parser to handle the specified input format: #{[:input]}") end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *attr, &block) ⇒ Object
Check if a method is invoked that begins with to_
and if so, try to instantiate a converter class (i.e. a class in the Kramdown::Converter module) and use it for converting the document.
For example, to_html
would instantiate the Kramdown::Converter::Html class.
102 103 104 105 106 107 108 109 110 |
# File 'lib/kramdown/document.rb', line 102 def method_missing(id, *attr, &block) if id.to_s =~ /^to_(\w+)$/ && (name = Utils.camelize($1)) && Converter.const_defined?(name) output, warnings = Converter.const_get(name).convert(@root, @options) @warnings.concat(warnings) output else super end end |
Instance Attribute Details
#options ⇒ Object (readonly)
The options hash which holds the options for parsing/converting the Kramdown document.
70 71 72 |
# File 'lib/kramdown/document.rb', line 70 def @options end |
#root ⇒ Object
The root Element of the element tree. It is immediately available after the ::new method has been called.
67 68 69 |
# File 'lib/kramdown/document.rb', line 67 def root @root end |
#warnings ⇒ Object (readonly)
An array of warning messages. It is filled with warnings during the parsing phase (i.e. in ::new) and the conversion phase.
74 75 76 |
# File 'lib/kramdown/document.rb', line 74 def warnings @warnings end |
Instance Method Details
#inspect ⇒ Object
:nodoc:
112 113 114 |
# File 'lib/kramdown/document.rb', line 112 def inspect #:nodoc: "<KD:Document: options=#{@options.inspect} root=#{@root.inspect} warnings=#{@warnings.inspect}>" end |