Class: Kramdown::Parser::Base

### Home Classes Methods

### In Files

- lib/kramdown/parser/base.rb

### Parent

Object

### Methods

- ::parse
- #adapt_source
- #add_text
- #extract_string
- #parse
- #warning

### 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::Ritex
- Kramdown::Converter::RemoveHtmlTags
- Kramdown::Converter::SyntaxHighlighter
- Kramdown::Converter::SyntaxHighlighter::Coderay
- 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::Parser::Base

## Base class for parsers

This class serves as base class for parsers. It provides common methods that can/should be used by all parsers, especially by those using StringScanner(Kramdown) for parsing.

A parser object is used as a throw-away object, i.e. it is only used for storing the needed state information during parsing. Therefore one can't instantiate a parser object directly but only use the Base::parse method.

## Implementing a parser

Implementing a new parser is rather easy: just derive a new class from this class and put it in the Kramdown::Parser module -- the latter is needed so that the auto-detection of the new parser works correctly. Then you need to implement the `#parse` method which has to contain the parsing code.

Have a look at the Base::parse, Base::new and Base#parse methods for additional information!

### Attributes

options[R]

The hash with the parsing options.

root[R]

The root element of element tree that is created from the source string.

source[R]

The original source string.

warnings[R]

The array with the parser warnings.

### Public Class Methods

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

Parse the `source` string into an element tree, possibly using the parsing `options`, and return the root element of the element tree and an array with warning messages.

Initializes a new instance of the calling class and then calls the `#parse` method that must be implemented by each subclass.

    # File lib/kramdown/parser/base.rb, line 64
    def self.parse(source, options = {})
      parser = new(source, options)
      parser.parse
      [parser.root, parser.warnings]
    end

### Public Instance Methods

adapt_source(source) click to toggle source

Modify the string `source` to be usable by the parser (unifies line ending characters to `\n` and makes sure `source` ends with a new line character).

    # File lib/kramdown/parser/base.rb, line 88
    def adapt_source(source)
      if source.respond_to?(:encode)
        raise "The encoding of the source text is not valid!" if !source.valid_encoding?
        source = source.encode('UTF-8')
      end
      source.gsub(/\r\n?/, "\n").chomp + "\n"
    end

add_text(text, tree = @tree, type = @text_type) click to toggle source

This helper method adds the given `text` either to the last element in the `tree` if it is a `type` element or creates a new text element with the given `type`.

    # File lib/kramdown/parser/base.rb, line 98
    def add_text(text, tree = @tree, type = @text_type)
      last = tree.children.last
      if last && last.type == type
        last.value << text
      elsif !text.empty?
        tree.children << Element.new(type, text, nil, :location => (last && last.options[:location] || tree.options[:location]))
      end
    end

extract_string(range, strscan) click to toggle source

Extract the part of the StringScanner `strscan` backed string specified by the `range`. This method works correctly under Ruby 1.8 and Ruby 1.9.

    # File lib/kramdown/parser/base.rb, line 109
    def extract_string(range, strscan)
      result = nil
      if strscan.string.respond_to?(:encoding)
        begin
          enc = strscan.string.encoding
          strscan.string.force_encoding('ASCII-8BIT')
          result = strscan.string[range].force_encoding(enc)
        ensure
          strscan.string.force_encoding(enc)
        end
      else
        result = strscan.string[range]
      end
      result
    end

parse() click to toggle source

Parse the source string into an element tree.

The parsing code should parse the source provided in @source and build an element tree the root of which should be @root.

This is the only method that has to be implemented by sub-classes!

    # File lib/kramdown/parser/base.rb, line 76
    def parse
      raise NotImplementedError
    end

warning(text) click to toggle source

Add the given warning `text` to the warning array.

    # File lib/kramdown/parser/base.rb, line 81
    def warning(text)
      @warnings << text
      #TODO: add position information
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.