class Kramdown::Parser::Base


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

def self.parse(source, doc)

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

Kramdown document +doc+.
Parse the +source+ string into an element tree, using the information provided by the
def self.parse(source, doc)
  new(doc).parse(source)
end

def adapt_source(source)

Modify the string +source+ to be usable by the parser.
def adapt_source(source)
  source.gsub(/\r\n?/, "\n").chomp + "\n"
end

def add_text(text, tree = @tree, type = @text_type)

+type+ element or creates a new text element with the given +type+.
This helper method adds the given +text+ either to the last element in the +tree+ if it is a
def add_text(text, tree = @tree, type = @text_type)
  if tree.children.last && tree.children.last.type == type
    tree.children.last.value << text
  elsif !text.empty?
    tree.children << Element.new(type, text)
  end
end

def extract_string(range, strscan)

method also works correctly under Ruby 1.9.
Extract the part of the StringScanner +srcscan+ backed string specified by the +range+. This
def extract_string(range, strscan)
  result = nil
  if RUBY_VERSION >= '1.9'
    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

def initialize(doc)

Initialize the parser with the given Kramdown document +doc+.
def initialize(doc)
  @doc = doc
  @text_type = :text
end

def warning(text)

Add the given warning +text+ to the warning array of the Kramdown document.
def warning(text)
  @doc.warnings << text
  #TODO: add position information
end