Class: Kramdown::Parser::Html

Inherits:
Base
  • Object
show all
Includes:
Parser
Defined in:
lib/kramdown/parser/html.rb

Overview

Used for parsing a HTML document.

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

Defined Under Namespace

Modules: Constants, Parser Classes: ElementConverter

Constant Summary

Constants included from Parser

Parser::HTML_RAW_START

Constants included from Constants

Constants::HTML_ATTRIBUTE_RE, Constants::HTML_BLOCK_ELEMENTS, Constants::HTML_COMMENT_RE, Constants::HTML_CONTENT_MODEL, Constants::HTML_CONTENT_MODEL_BLOCK, Constants::HTML_CONTENT_MODEL_RAW, Constants::HTML_CONTENT_MODEL_SPAN, Constants::HTML_DOCTYPE_RE, Constants::HTML_ELEMENTS_WITHOUT_BODY, Constants::HTML_ENTITY_RE, Constants::HTML_INSTRUCTION_RE, Constants::HTML_SPAN_ELEMENTS, Constants::HTML_TAG_CLOSE_RE, Constants::HTML_TAG_RE

Instance Attribute Summary

Attributes inherited from Base

#options, #root, #source, #warnings

Instance Method Summary collapse

Methods included from Parser

#handle_html_start_tag, #handle_raw_html_tag, #parse_html_attributes, #parse_raw_html

Methods inherited from Base

#adapt_source, #add_text, #extract_string, #initialize, parse, #warning

Constructor Details

This class inherits a constructor from Kramdown::Parser::Base

Instance Method Details

#parseObject

Parse the source string provided on initialization as HTML document.



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/kramdown/parser/html.rb', line 560

def parse
  @stack, @tree = [], @root
  @src = Kramdown::Utils::StringScanner.new(adapt_source(source))

  while true
    if result = @src.scan(/\s*#{HTML_INSTRUCTION_RE}/)
      @tree.children << Element.new(:xml_pi, result.strip, nil, :category => :block)
    elsif result = @src.scan(/\s*#{HTML_DOCTYPE_RE}/)
      # ignore the doctype
    elsif result = @src.scan(/\s*#{HTML_COMMENT_RE}/)
      @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