class HTMLParser
Constants
- ATTR_RE
- CLOSE_TAG_RE
- OPEN_TAG_RE
- TEXT_RE
Public Class Methods
new(html)
click to toggle source
# File lib/html-renderer/html_parser.rb, line 50 def initialize(html) @s = StringScanner.new(html) # @s = html end
Public Instance Methods
as_tree()
click to toggle source
# File lib/html-renderer/html_parser.rb, line 68 def as_tree tree.map { |e| e.recursive_inspect } end
each_tag() { |:close_tag, captures.first| ... }
click to toggle source
# File lib/html-renderer/html_parser.rb, line 55 def each_tag until @s.eos? if @s.scan(CLOSE_TAG_RE) yield [:close_tag, @s.captures.first] elsif @s.scan(OPEN_TAG_RE) tag = Tag.from_str(@s.captures.first) yield [:open_tag, tag] elsif @s.scan(TEXT_RE) yield [:text, @s.matched] end end end
tree()
click to toggle source
# File lib/html-renderer/html_parser.rb, line 72 def tree stack = Stack.new stack.push Tag.new("root") each_tag do |type, elem| case type when :text text = elem.strip stack.top.children << text unless text.empty? when :open_tag stack.top.children << elem stack.push elem when :close_tag stack.pop else raise "wat" end end stack end