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_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.



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/kramdown/parser/html.rb', line 541

def parse
  @stack, @tree = [], @root
  @src = 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|
    parse_raw_html(c, &tag_handler) if !closed
  end
  parse_raw_html(@tree, &tag_handler)

  ElementConverter.convert(@tree)
end