class Kramdown::Document
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!
Attributes
The options hash which holds the options for parsing/converting the Kramdown
document.
An array of warning messages. It is filled with warnings during the parsing phase (i.e. in ::new
) and the conversion phase.
Public Class Methods
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.
# File lib/kramdown/document.rb 95 def initialize(source, options = {}) 96 @options = Options.merge(options).freeze 97 parser = (@options[:input] || 'kramdown').to_s 98 parser = parser[0..0].upcase + parser[1..-1] 99 try_require('parser', parser) 100 if Parser.const_defined?(parser) 101 @root, @warnings = Parser.const_get(parser).parse(source, @options) 102 else 103 raise Kramdown::Error, "kramdown has no parser to handle the specified " \ 104 "input format: #{@options[:input]}" 105 end 106 end
Public Instance Methods
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.
# File lib/kramdown/document.rb 112 def method_missing(id, *attr, &block) 113 if id.to_s =~ /^to_(\w+)$/ && (name = Utils.camelize($1)) && 114 try_require('converter', name) && Converter.const_defined?(name) 115 output, warnings = Converter.const_get(name).convert(@root, @options) 116 @warnings.concat(warnings) 117 output 118 else 119 super 120 end 121 end
Protected Instance Methods
Try requiring a parser or converter class and don't raise an error if the file is not found.
# File lib/kramdown/document.rb 128 def try_require(type, name) 129 require("kramdown/#{type}/#{Utils.snake_case(name)}") 130 true 131 rescue LoadError 132 true 133 end