class Kramdown::Parser::Html

Used for parsing an HTML document.

The parsing code is in the Parser module that can also be used by other parsers.

Public Instance Methods

parse() click to toggle source

Parse the source string provided on initialization as HTML document.

# File lib/kramdown/parser/html.rb, line 585
def parse
  @stack, @tree = [], @root
  @src = Kramdown::Utils::StringScanner.new(adapt_source(source))

  while true
    if (result = @src.scan(/\s*#{HTML_INSTRUCTION_RE}/o))
      @tree.children << Element.new(:xml_pi, result.strip, nil, category: :block)
    elsif (result = @src.scan(/\s*#{HTML_DOCTYPE_RE}/o))
      # ignore the doctype
    elsif (result = @src.scan(/\s*#{HTML_COMMENT_RE}/o))
      @tree.children << Element.new(:xml_comment, result.strip, nil, category: :block)
    else
      break
    end
  end

  tag_handler = lambda do |c, closed, handle_body|
    parse_raw_html(c, &tag_handler) if !closed && handle_body
  end
  parse_raw_html(@tree, &tag_handler)

  ElementConverter.convert(@tree)
end