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)
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)
def adapt_source(source) source.gsub(/\r\n?/, "\n").chomp + "\n" end
def add_text(text, tree = @tree, type = @text_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)
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)
def initialize(doc) @doc = doc @text_type = :text end
def warning(text)
def warning(text) @doc.warnings << text #TODO: add position information end