class Kramdown::Converter::Base

Latex converters do the transformation.
method per tree element type is a straight forward way to do it - this is how the Html and
The actual transformation of the document tree can be done in any way. However, writing one
and can be used to store information about the conversion process.
document instance provides a hash called ‘conversion_infos` that is also automatically cleared
The document instance is automatically set as @doc in Base#initialize. Furthermore, the
tree and should return the converted output.
work properly). Then you need to implement the #convert(tree) method which takes a document
put it in the Kramdown::Converter module (the latter is only needed if auto-detection should
Implementing a new converter is rather easy: just create a new sub class from this class and
== Implementing a converter
automatically applied to the result (for example, embedding the output into a template).
used by all converters (like #generate_id) as well as common functionality that is
This class serves as base class for all converters. It provides methods that can/should be
== Base class for converters

def self.apply_template(doc, body)

Apply the template specified in the +doc+ options, using +body+ as the body string.
def self.apply_template(doc, body)
  erb = ERB.new(get_template(doc.options[:template]))
  obj = Object.new
  obj.instance_variable_set(:@doc, doc)
  obj.instance_variable_set(:@body, body)
  erb.result(obj.instance_eval{binding})
end

def self.convert(doc)

result is rendered into the specified template.
be implemented by each subclass. If the +template+ option is specified and non-empty, the
Initializes a new instance of the calling class and then calls the #convert method that must

Convert the Kramdown document +doc+ to the output format implemented by a subclass.
def self.convert(doc)
  result = new(doc).convert(doc.tree)
  result = apply_template(doc, result) if !doc.options[:template].empty?
  result
end

def self.get_template(template)

Return the template specified by +template+.
def self.get_template(template)
  format_ext = '.' + self.name.split(/::/).last.downcase
  shipped = File.join(::Kramdown.data_dir, template + format_ext)
  if File.exist?(template)
    File.read(template)
  elsif File.exist?(template + format_ext)
    File.read(template + format_ext)
  elsif File.exist?(shipped)
    File.read(shipped)
  else
    raise "The specified template file #{template} does not exist"
  end
end

def generate_id(str)

Generate an unique alpha-numeric ID from the the string +str+ for use as header ID.
def generate_id(str)
  gen_id = str.gsub(/[^a-zA-Z0-9 -]/, '').gsub(/^[^a-zA-Z]*/, '').gsub(' ', '-').downcase
  gen_id = 'section' if gen_id.length == 0
  @used_ids ||= {}
  if @used_ids.has_key?(gen_id)
    gen_id += '-' + (@used_ids[gen_id] += 1).to_s
  else
    @used_ids[gen_id] = 0
  end
  @doc.options[:auto_id_prefix] + gen_id
end

def initialize(doc)

Initialize the converter with the given Kramdown document +doc+.
def initialize(doc)
  @doc = doc
  @doc.conversion_infos.clear
end