Class: Kramdown::Document

### Home Classes Methods

### In Files

- lib/kramdown/document.rb

### Parent

Object

### Methods

- ::new
- #method_missing
- #try_require

### Class/Module Index

Quicksearch

- Kramdown
- Kramdown::Converter
- Kramdown::Converter::Base
- Kramdown::Converter::Html
- Kramdown::Converter::Kramdown
- Kramdown::Converter::Latex
- Kramdown::Converter::MathEngine
- Kramdown::Converter::MathEngine::Itex2MML
- Kramdown::Converter::MathEngine::Mathjax
- Kramdown::Converter::MathEngine::MathjaxNode
- Kramdown::Converter::MathEngine::Ritex
- Kramdown::Converter::RemoveHtmlTags
- Kramdown::Converter::SyntaxHighlighter
- Kramdown::Converter::SyntaxHighlighter::Coderay
- Kramdown::Converter::SyntaxHighlighter::Minted
- Kramdown::Converter::SyntaxHighlighter::Rouge
- Kramdown::Converter::Toc
- Kramdown::Document
- Kramdown::Element
- Kramdown::Error
- Kramdown::Options
- Kramdown::Options::Boolean
- Kramdown::Parser
- Kramdown::Parser::Base
- Kramdown::Parser::GFM
- Kramdown::Parser::Html
- Kramdown::Parser::Html::Constants
- Kramdown::Parser::Html::ElementConverter
- Kramdown::Parser::Html::Parser
- Kramdown::Parser::Kramdown
- Kramdown::Parser::Markdown
- Kramdown::Utils
- Kramdown::Utils::Configurable
- Kramdown::Utils::Entities
- Kramdown::Utils::Entities::Entity
- Kramdown::Utils::Html
- Kramdown::Utils::OrderedHash
- Kramdown::Utils::StringScanner
- Kramdown::Utils::Unidecoder

No matching classes.

# 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

options[R]

The options hash which holds the options for parsing/converting the Kramdown document.

root[RW]

The root Element of the element tree. It is immediately available after the ::new method has been called.

warnings[R]

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

new(source, options = {}) click to toggle source

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, line 100
    def initialize(source, options = {})
      @options = Options.merge(options).freeze
      parser = (options[:input] || 'kramdown').to_s
      parser = parser[0..0].upcase + parser[1..-1]
      try_require('parser', parser)
      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: #{options[:input]}")
      end
    end

### Public Instance Methods

method_missing(id, *attr, &block) click to toggle source

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, line 116
    def method_missing(id, *attr, &block)
      if id.to_s =~ /^to_(\w+)$/ && (name = Utils.camelize($1)) &&
          try_require('converter', name) && Converter.const_defined?(name)
        output, warnings = Converter.const_get(name).convert(@root, @options)
        @warnings.concat(warnings)
        output
      else
        super
      end
    end

### Protected Instance Methods

try_require(type, name) click to toggle source

Try requiring a parser or converter class and don't raise an error if the file is not found.

    # File lib/kramdown/document.rb, line 132
    def try_require(type, name)
      require("kramdown/#{type}/#{Utils.snake_case(name)}")
      true
    rescue LoadError
      true
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.