Class: Kramdown::Converter::Base

### Home Classes Methods

### In Files

- lib/kramdown/converter/base.rb

### Parent

Object

### Methods

- ::convert
- #convert
- #extract_code_language
- #extract_code_language!
- #generate_id
- #in_toc?
- #output_header_level
- #smart_quote_entity
- #warning

### Class/Module Index

Quicksearch

- Kramdown
- Kramdown::Converter
- Kramdown::Converter::Base
- Kramdown::Converter::Html
- Kramdown::Converter::Kramdown
- Kramdown::Converter::Latex
- Kramdown::Converter::RemoveHtmlTags
- Kramdown::Converter::Toc
- Kramdown::Document
- Kramdown::Element
- Kramdown::Error
- Kramdown::Options
- Kramdown::Options::Boolean
- Kramdown::Parser
- Kramdown::Parser::Base
- 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::Entities
- Kramdown::Utils::Entities::Entity
- Kramdown::Utils::Html
- Kramdown::Utils::OrderedHash

No matching classes.

# Kramdown::Converter::Base

## Base class for converters

This class serves as base class for all converters. It provides methods that can/should be used by all converters (like generate_id) as well as common functionality that is automatically applied to the result (for example, embedding the output into a template).

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

## Implementing a converter

Implementing a new converter is rather easy: just derive a new class from this class and put it in the Kramdown::Converter module (the latter is only needed if auto-detection should work properly). Then you need to implement the convert method which has to contain the conversion code for converting an element and has to return the conversion result.

The actual transformation of the document tree can be done in any way. However, writing one method per element type is a straight forward way to do it - this is how the Html and Latex converters do the transformation.

Have a look at the Base::convert method for additional information!

### Attributes

data[R]

Can be used by a converter for storing arbitrary information during the conversion process.

options[R]

The hash with the conversion options.

root[R]

The root element that is converted.

warnings[R]

The warnings array.

### Public Class Methods

convert(tree, options = {}) click to toggle source

Convert the element tree `tree` and return the resulting conversion object (normally a string) and an array with warning messages. The parameter `options` specifies the conversion options that should be used.

Initializes a new instance of the calling class and then calls the convert method with `tree` as parameter. If the `template` option is specified and non-empty, the result is rendered into the specified template. The template resolution is done in the following way:

1. Look in the current working directory for the template.

2. Append `.convertername` (e.g. `.html`) to the template name and look for the resulting file in the current working directory.

3. Append `.convertername` to the template name and look for it in the kramdown data directory.

    # File lib/kramdown/converter/base.rb, line 88
    def self.convert(tree, options = {})
      converter = new(tree, ::Kramdown::Options.merge(options.merge(tree.options[:options] || {})))
      result = converter.convert(tree)
      result = apply_template(converter, result) if !converter.options[:template].empty?
      [result, converter.warnings]
    end

### Public Instance Methods

convert(el) click to toggle source

Convert the element `el` and return the resulting object.

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

    # File lib/kramdown/converter/base.rb, line 98
    def convert(el)
      raise NotImplementedError
    end

extract_code_language(attr) click to toggle source

Extract the code block/span language from the attributes.

    # File lib/kramdown/converter/base.rb, line 145
    def extract_code_language(attr)
      if attr['class'] && attr['class'] =~ /\blanguage-\w+\b/
        attr['class'].scan(/\blanguage-(\w+)\b/).first.first
      end
    end

extract_code_language!(attr) click to toggle source

See extract_code_language

Warning: This version will modify the given attributes if a language is present.

    # File lib/kramdown/converter/base.rb, line 154
    def extract_code_language!(attr)
      lang = extract_code_language(attr)
      attr['class'] = attr['class'].sub(/\blanguage-\w+\b/, '').strip if lang
      attr.delete('class') if lang && attr['class'].empty?
      lang
    end

generate_id(str) click to toggle source

Generate an unique alpha-numeric ID from the the string `str` for use as a header ID.

Uses the option `auto_id_prefix`: the value of this option is prepended to every generated ID.

    # File lib/kramdown/converter/base.rb, line 165
    def generate_id(str)
      gen_id = str.gsub(/^[^a-zA-Z]+/, '')
      gen_id.tr!('^a-zA-Z0-9 -', '')
      gen_id.tr!(' ', '-')
      gen_id.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
      @options[:auto_id_prefix] + gen_id
    end

output_header_level(level) click to toggle source

Return the output header level given a level.

Uses the `header_offset` option for adjusting the header level.

    # File lib/kramdown/converter/base.rb, line 140
    def output_header_level(level)
      [[level + @options[:header_offset], 6].min, 1].max
    end

smart_quote_entity(el) click to toggle source

Return the entity that represents the given smart_quote element.

    # File lib/kramdown/converter/base.rb, line 183
    def smart_quote_entity(el)
      res = @options[:smart_quotes][SMART_QUOTE_INDICES[el.value]]
      ::Kramdown::Utils::Entities.entity(res)
    end

warning(text) click to toggle source

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

    # File lib/kramdown/converter/base.rb, line 127
    def warning(text)
      @warnings << text
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.