Class: Kramdown::Converter::Html

### Home Classes Methods

### In Files

- lib/kramdown/converter/html.rb

### Parent

Kramdown::Converter::Base

### Methods

- ::new
- #add_syntax_highlighter_to_class_attr
- #convert
- #convert_a
- #convert_abbreviation
- #convert_blank
- #convert_blockquote
- #convert_br
- #convert_codeblock
- #convert_codespan
- #convert_comment
- #convert_dd
- #convert_dl
- #convert_dt
- #convert_em
- #convert_entity
- #convert_footnote
- #convert_header
- #convert_hr
- #convert_html_element
- #convert_img
- #convert_li
- #convert_math
- #convert_ol
- #convert_p
- #convert_raw
- #convert_root
- #convert_smart_quote
- #convert_strong
- #convert_table
- #convert_tbody
- #convert_td
- #convert_text
- #convert_tfoot
- #convert_thead
- #convert_tr
- #convert_typographic_sym
- #convert_ul
- #convert_xml_comment
- #convert_xml_pi
- #footnote_content
- #format_as_block_html
- #format_as_indented_block_html
- #format_as_span_html
- #generate_toc_tree
- #inner
- #obfuscate
- #remove_footnotes

### Included Modules

- Kramdown::Utils::Html
- Kramdown::Parser::Html::Constants

### 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::Converter::Html

Converts a Kramdown::Document to HTML.

You can customize the HTML converter by sub-classing it and overriding the `convert_NAME` methods. Each such method takes the following parameters:

`el`

  The element of type `NAME` to be converted.

`indent`

  A number representing the current amount of spaces for indent (only used for block-level elements).

The return value of such a method has to be a string containing the element `el` formatted as HTML element.

### Constants

DISPATCHER

  The mapping of element type to conversion method.

FOOTNOTE_BACKLINK_FMT

### Attributes

indent[RW]

The amount of indentation used when nesting HTML tags.

### Public Class Methods

new(root, options) click to toggle source

Initialize the HTML converter with the given Kramdown document `doc`.

    # File lib/kramdown/converter/html.rb, line 37
    def initialize(root, options)
      super
      @footnote_counter = @footnote_start = @options[:footnote_nr]
      @footnotes = []
      @footnotes_by_name = {}
      @footnote_location = nil
      @toc = []
      @toc_code = nil
      @indent = 2
      @stack = []
    end

### Public Instance Methods

add_syntax_highlighter_to_class_attr(attr) click to toggle source

Add the syntax highlighter name to the 'class' attribute of the given attribute hash.

    # File lib/kramdown/converter/html.rb, line 362
    def add_syntax_highlighter_to_class_attr(attr)
      (attr['class'] = (attr['class'] || '') + " highlighter-#{@options[:syntax_highlighter]}").lstrip!
    end

convert(el, indent = -@indent) click to toggle source

Dispatch the conversion of the element `el` to a `convert_TYPE` method using the `type` of the element.

    # File lib/kramdown/converter/html.rb, line 54
    def convert(el, indent = -@indent)
      send(DISPATCHER[el.type], el, indent)
    end

convert_a(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 235
    def convert_a(el, indent)
      res = inner(el, indent)
      attr = el.attr.dup
      if attr['href'].start_with?('mailto:')
        mail_addr = attr['href'][7..-1]
        attr['href'] = obfuscate('mailto') << ":" << obfuscate(mail_addr)
        res = obfuscate(res) if res == mail_addr
      end
      format_as_span_html(el.type, attr, res)
    end

convert_abbreviation(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 321
    def convert_abbreviation(el, indent)
      title = @root.options[:abbrev_defs][el.value]
      format_as_span_html("abbr", {:title => (title.empty? ? nil : title)}, el.value)
    end

convert_blank(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 74
    def convert_blank(el, indent)
      "\n"
    end

convert_blockquote(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 118
    def convert_blockquote(el, indent)
      format_as_indented_block_html(el.type, el.attr, inner(el, indent), indent)
    end

convert_br(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 231
    def convert_br(el, indent)
      "<br />"
    end

convert_codeblock(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 90
    def convert_codeblock(el, indent)
      attr = el.attr.dup
      lang = extract_code_language!(attr)
      highlighted_code = highlight_code(el.value, lang, :block)

      if highlighted_code
        add_syntax_highlighter_to_class_attr(attr)
        "#{' '*indent}<div#{html_attributes(attr)}>#{highlighted_code}#{' '*indent}</div>\n"
      else
        result = escape_html(el.value)
        result.chomp!
        if el.attr['class'].to_s =~ /\bshow-whitespaces\b/
          result.gsub!(/(?:(^[ \t]+)|([ \t]+$)|([ \t]+))/) do |m|
            suffix = ($1 ? '-l' : ($2 ? '-r' : ''))
            m.scan(/./).map do |c|
              case c
              when "\t" then "<span class=\"ws-tab#{suffix}\">\t</span>"
              when " " then "<span class=\"ws-space#{suffix}\">&#8901;</span>"
              end
            end.join('')
          end
        end
        code_attr = {}
        code_attr['class'] = "language-#{lang}" if lang
        "#{' '*indent}<pre#{html_attributes(attr)}><code#{html_attributes(code_attr)}>#{result}\n</code></pre>\n"
      end
    end

convert_codespan(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 250
    def convert_codespan(el, indent)
      attr = el.attr.dup
      lang = extract_code_language(attr)
      result = highlight_code(el.value, lang, :span)
      if result
        add_syntax_highlighter_to_class_attr(attr)
      else
        result = escape_html(el.value)
      end

      format_as_span_html('code', attr, result)
    end

convert_comment(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 223
    def convert_comment(el, indent)
      if el.options[:category] == :block
        "#{' '*indent}<!-- #{el.value} -->\n"
      else
        "<!-- #{el.value} -->"
      end
    end

convert_dd(el, indent) click to toggle source
Alias for: convert_li
convert_dl(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 148
    def convert_dl(el, indent)
      format_as_indented_block_html(el.type, el.attr, inner(el, indent), indent)
    end

convert_dt(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 164
    def convert_dt(el, indent)
      format_as_block_html(el.type, el.attr, inner(el, indent), indent)
    end

convert_em(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 285
    def convert_em(el, indent)
      format_as_span_html(el.type, el.attr, inner(el, indent))
    end

Also aliased as: convert_strong
convert_entity(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 290
    def convert_entity(el, indent)
      entity_to_str(el.value, el.options[:original])
    end

convert_footnote(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 263
    def convert_footnote(el, indent)
      repeat = ''
      if (footnote = @footnotes_by_name[el.options[:name]])
        number = footnote[2]
        repeat = ":#{footnote[3] += 1}"
      else
        number = @footnote_counter
        @footnote_counter += 1
        @footnotes << [el.options[:name], el.value, number, 0]
        @footnotes_by_name[el.options[:name]] = @footnotes.last
      end
      "<sup id=\"fnref:#{el.options[:name]}#{repeat}\"><a href=\"#fn:#{el.options[:name]}\" class=\"footnote\">#{number}</a></sup>"
    end

convert_header(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 122
    def convert_header(el, indent)
      attr = el.attr.dup
      if @options[:auto_ids] && !attr['id']
        attr['id'] = generate_id(el.options[:raw_text])
      end
      @toc << [el.options[:level], attr['id'], el.children] if attr['id'] && in_toc?(el)
      level = output_header_level(el.options[:level])
      format_as_block_html("h#{level}", attr, inner(el, indent), indent)
    end

convert_hr(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 132
    def convert_hr(el, indent)
      "#{' '*indent}<hr#{html_attributes(el.attr)} />\n"
    end

convert_html_element(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 168
    def convert_html_element(el, indent)
      res = inner(el, indent)
      if el.options[:category] == :span
        "<#{el.value}#{html_attributes(el.attr)}" << (res.empty? && HTML_ELEMENTS_WITHOUT_BODY.include?(el.value) ? " />" : ">#{res}</#{el.value}>")
      else
        output = ''
        output << ' '*indent if @stack.last.type != :html_element || @stack.last.options[:content_model] != :raw
        output << "<#{el.value}#{html_attributes(el.attr)}"
        if el.options[:is_closed] && el.options[:content_model] == :raw
          output << " />"
        elsif !res.empty? && el.options[:content_model] != :block
          output << ">#{res}</#{el.value}>"
        elsif !res.empty?
          output << ">\n#{res.chomp}\n"  << ' '*indent << "</#{el.value}>"
        elsif HTML_ELEMENTS_WITHOUT_BODY.include?(el.value)
          output << " />"
        else
          output << "></#{el.value}>"
        end
        output << "\n" if @stack.last.type != :html_element || @stack.last.options[:content_model] != :raw
        output
      end
    end

convert_img(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 246
    def convert_img(el, indent)
      "<img#{html_attributes(el.attr)} />"
    end

convert_li(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 152
    def convert_li(el, indent)
      output = ' '*indent << "<#{el.type}" << html_attributes(el.attr) << ">"
      res = inner(el, indent)
      if el.children.empty? || (el.children.first.type == :p && el.children.first.options[:transparent])
        output << res << (res =~ /\n\Z/ ? ' '*indent : '')
      else
        output << "\n" << res << ' '*indent
      end
      output << "</#{el.type}>\n"
    end

Also aliased as: convert_dd
convert_math(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 311
    def convert_math(el, indent)
      if (result = format_math(el, :indent => indent))
        result
      elsif el.options[:category] == :block
        format_as_block_html('pre', el.attr, "$$\n#{el.value}\n$$", indent)
      else
        format_as_span_html('span', el.attr, "$#{el.value}$")
      end
    end

convert_ol(el, indent) click to toggle source
Alias for: convert_ul
convert_p(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 82
    def convert_p(el, indent)
      if el.options[:transparent]
        inner(el, indent)
      else
        format_as_block_html(el.type, el.attr, inner(el, indent), indent)
      end
    end

convert_raw(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 277
    def convert_raw(el, indent)
      if !el.options[:type] || el.options[:type].empty? || el.options[:type].include?('html')
        el.value + (el.options[:category] == :block ? "\n" : '')
      else
        ''
      end
    end

convert_root(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 326
    def convert_root(el, indent)
      result = inner(el, indent)
      if @footnote_location
        result.sub!(/#{@footnote_location}/, footnote_content)
      else
        result << footnote_content
      end
      if @toc_code
        toc_tree = generate_toc_tree(@toc, @toc_code[0], @toc_code[1] || {})
        text = if toc_tree.children.size > 0
                 convert(toc_tree, 0)
               else
                 ''
               end
        result.sub!(/#{@toc_code.last}/, text)
      end
      result
    end

convert_smart_quote(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 307
    def convert_smart_quote(el, indent)
      entity_to_str(smart_quote_entity(el))
    end

convert_strong(el, indent) click to toggle source
Alias for: convert_em
convert_table(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 201
    def convert_table(el, indent)
      format_as_indented_block_html(el.type, el.attr, inner(el, indent), indent)
    end

Also aliased as: convert_thead, convert_tbody, convert_tfoot, convert_tr
convert_tbody(el, indent) click to toggle source
Alias for: convert_table
convert_td(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 211
    def convert_td(el, indent)
      res = inner(el, indent)
      type = (@stack[-2].type == :thead ? :th : :td)
      attr = el.attr
      alignment = @stack[-3].options[:alignment][@stack.last.children.index(el)]
      if alignment != :default
        attr = el.attr.dup
        attr['style'] = (attr.has_key?('style') ? "#{attr['style']}; ": '') << "text-align: #{alignment}"
      end
      format_as_block_html(type, attr, res.empty? ? entity_to_str(ENTITY_NBSP) : res, indent)
    end

convert_text(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 78
    def convert_text(el, indent)
      escape_html(el.value, :text)
    end

convert_tfoot(el, indent) click to toggle source
Alias for: convert_table
convert_thead(el, indent) click to toggle source
Alias for: convert_table
convert_tr(el, indent) click to toggle source
Alias for: convert_table
convert_typographic_sym(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 303
    def convert_typographic_sym(el, indent)
      TYPOGRAPHIC_SYMS[el.value].map {|e| entity_to_str(e)}.join('')
    end

convert_ul(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 136
    def convert_ul(el, indent)
      if !@toc_code && (el.options[:ial][:refs].include?('toc') rescue nil)
        @toc_code = [el.type, el.attr, (0..128).to_a.map{|a| rand(36).to_s(36)}.join]
        @toc_code.last
      elsif !@footnote_location && el.options[:ial] && (el.options[:ial][:refs] || []).include?('footnotes')
        @footnote_location = (0..128).to_a.map{|a| rand(36).to_s(36)}.join
      else
        format_as_indented_block_html(el.type, el.attr, inner(el, indent), indent)
      end
    end

Also aliased as: convert_ol
convert_xml_comment(el, indent) click to toggle source

    # File lib/kramdown/converter/html.rb, line 192
    def convert_xml_comment(el, indent)
      if el.options[:category] == :block && (@stack.last.type != :html_element || @stack.last.options[:content_model] != :raw)
        ' '*indent << el.value << "\n"
      else
        el.value
      end
    end

Also aliased as: convert_xml_pi
convert_xml_pi(el, indent) click to toggle source
Alias for: convert_xml_comment
footnote_content() click to toggle source

Return a HTML ordered list with the footnote content for the used footnotes.

    # File lib/kramdown/converter/html.rb, line 423
    def footnote_content
      ol = Element.new(:ol)
      ol.attr['start'] = @footnote_start if @footnote_start != 1
      i = 0
      while i < @footnotes.length
        name, data, _, repeat = *@footnotes[i]
        li = Element.new(:li, nil, {'id' => "fn:#{name}"})
        li.children = Marshal.load(Marshal.dump(data.children))

        if li.children.last.type == :p
          para = li.children.last
          insert_space = true
        else
          li.children << (para = Element.new(:p))
          insert_space = false
        end

        para.children << Element.new(:raw, FOOTNOTE_BACKLINK_FMT % [insert_space ? ' ' : '', name, "&#8617;"])
        (1..repeat).each do |index|
          para.children << Element.new(:raw, FOOTNOTE_BACKLINK_FMT % [" ", "#{name}:#{index}", "&#8617;<sup>#{index+1}</sup>"])
        end

        ol.children << Element.new(:raw, convert(li, 4))
        i += 1
      end
      (ol.children.empty? ? '' : format_as_indented_block_html('div', {:class => "footnotes"}, convert(ol, 2), 0))
    end

format_as_block_html(name, attr, body, indent) click to toggle source

Format the given element as block HTML.

    # File lib/kramdown/converter/html.rb, line 351
    def format_as_block_html(name, attr, body, indent)
      "#{' '*indent}<#{name}#{html_attributes(attr)}>#{body}</#{name}>\n"
    end

format_as_indented_block_html(name, attr, body, indent) click to toggle source

Format the given element as block HTML with a newline after the start tag and indentation before the end tag.

    # File lib/kramdown/converter/html.rb, line 357
    def format_as_indented_block_html(name, attr, body, indent)
      "#{' '*indent}<#{name}#{html_attributes(attr)}>\n#{body}#{' '*indent}</#{name}>\n"
    end

format_as_span_html(name, attr, body) click to toggle source

Format the given element as span HTML.

    # File lib/kramdown/converter/html.rb, line 346
    def format_as_span_html(name, attr, body)
      "<#{name}#{html_attributes(attr)}>#{body}</#{name}>"
    end

inner(el, indent) click to toggle source

Return the converted content of the children of `el` as a string. The parameter `indent` has to be the amount of indentation used for the element `el`.

Pushes `el` onto the @stack before converting the child elements and pops it from the stack afterwards.

    # File lib/kramdown/converter/html.rb, line 63
    def inner(el, indent)
      result = ''
      indent += @indent
      @stack.push(el)
      el.children.each do |inner_el|
        result << send(DISPATCHER[inner_el.type], inner_el, indent)
      end
      @stack.pop
      result
    end

obfuscate(text) click to toggle source

Obfuscate the `text` by using HTML entities.

    # File lib/kramdown/converter/html.rb, line 411
    def obfuscate(text)
      result = ""
      text.each_byte do |b|
        result << (b > 128 ? b.chr : "&#%03d;" % b)
      end
      result.force_encoding(text.encoding) if result.respond_to?(:force_encoding)
      result
    end

remove_footnotes(elements) click to toggle source

Remove all footnotes from the given elements.

    # File lib/kramdown/converter/html.rb, line 403
    def remove_footnotes(elements)
      elements.delete_if do |c|
        remove_footnotes(c.children)
        c.type == :footnote
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.