Class: Scrapetor::SAX::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/scrapetor/sax.rb

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ Parser

Returns a new instance of Parser.



38
39
40
# File 'lib/scrapetor/sax.rb', line 38

def initialize(handler)
  @handler = handler
end

Instance Method Details

#parse(html) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/scrapetor/sax.rb', line 42

def parse(html)
  Tokenizer.new(html).each_event do |event|
    type, *args = event
    case type
    when :doc_start   then @handler.start_document
    when :doc_end     then @handler.end_document
    when :start       then @handler.start_element(args[0], args[1])
    when :end         then @handler.end_element(args[0])
    when :text        then @handler.characters(args[0])
    when :comment     then @handler.comment(args[0])
    when :doctype     then @handler.doctype(args[0])
    when :cdata       then @handler.cdata_block(args[0])
    end
  end
  self
end

#parse_file(path) ⇒ Object



59
60
61
# File 'lib/scrapetor/sax.rb', line 59

def parse_file(path)
  parse(File.read(path))
end

#parse_io(io) ⇒ Object



63
64
65
# File 'lib/scrapetor/sax.rb', line 63

def parse_io(io)
  parse(io.read)
end