Module: Kramdown::Parser::Html::Parser

Includes:
Constants
Included in:
Kramdown::Parser::Html, Kramdown
Defined in:
lib/kramdown/parser/html.rb

Overview

Contains the parsing methods. This module can be mixed into any parser to get HTML parsing functionality. The only thing that must be provided by the class are instance variable parsing.

Constant Summary collapse

HTML_RAW_START =

:nodoc:

/(?=<(#{REXML::Parsers::BaseParser::UNAME_STR}|\/|!--|\?))/

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 Method Summary collapse

Instance Method Details

#handle_html_start_tagObject

Process the HTML start tag that has already be scanned/checked via @src.

Does the common processing steps and then yields to the caller for further processing (first parameter is the created element, the second parameter is true if the HTML element is already closed, ie. contains no body).



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/kramdown/parser/html.rb', line 88

def handle_html_start_tag # :yields: el, closed
  name = @src[1].downcase
  closed = !@src[4].nil?
  attrs = Utils::OrderedHash.new
  @src[2].scan(HTML_ATTRIBUTE_RE).each {|attr,sep,val| attrs[attr] = val}

  el = Element.new(:html_element, name, attrs, :category => :block)
  @tree.children << el

  if !closed && HTML_ELEMENTS_WITHOUT_BODY.include?(el.value)
    warning("The HTML tag '#{el.value}' cannot have any content - auto-closing it")
    closed = true
  end
  if name == 'script' || name == 'style'
    handle_raw_html_tag(name)
    yield(el, true)
  else
    yield(el, closed)
  end
end

#handle_raw_html_tag(name) ⇒ Object

Handle the raw HTML tag at the current position.



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/kramdown/parser/html.rb', line 110

def handle_raw_html_tag(name)
  curpos = @src.pos
  if @src.scan_until(/(?=<\/#{name}\s*>)/mi)
    add_text(extract_string(curpos...@src.pos, @src), @tree.children.last, :raw)
    @src.scan(HTML_TAG_CLOSE_RE)
  else
    add_text(@src.rest, @tree.children.last, :raw)
    @src.terminate
    warning("Found no end tag for '#{name}' - auto-closing it")
  end
end

#parse_raw_html(el, &block) ⇒ Object

Parse raw HTML from the current source position, storing the found elements in el. Parsing continues until one of the following criteria are fulfilled:

  • The end of the document is reached.

  • The matching end tag for the element el is found (only used if el is an HTML element).

When an HTML start tag is found, processing is deferred to #handle_html_start_tag, providing the block given to this method.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/kramdown/parser/html.rb', line 133

def parse_raw_html(el, &block)
  @stack.push(@tree)
  @tree = el

  done = false
  while !@src.eos? && !done
    if result = @src.scan_until(HTML_RAW_START)
      add_text(result, @tree, :text)
      if result = @src.scan(HTML_COMMENT_RE)
        @tree.children << Element.new(:xml_comment, result, nil, :category => :block)
      elsif result = @src.scan(HTML_INSTRUCTION_RE)
        @tree.children << Element.new(:xml_pi, result, nil, :category => :block)
      elsif @src.scan(HTML_TAG_RE)
        handle_html_start_tag(&block)
      elsif @src.scan(HTML_TAG_CLOSE_RE)
        if @tree.value == @src[1].downcase
          done = true
        else
          warning("Found invalidly used HTML closing tag for '#{@src[1].downcase}' - ignoring it")
        end
      else
        add_text(@src.getch, @tree, :text)
      end
    else
      add_text(@src.rest, @tree, :text)
      @src.terminate
      warning("Found no end tag for '#{@tree.value}' - auto-closing it") if @tree.type == :html_element
      done = true
    end
  end

  @tree = @stack.pop
end