Class: Nokogiri::HTML4::SAX::Parser

Inherits:
XML::SAX::Parser show all
Defined in:
lib/nokogiri/html4/sax/parser.rb

Overview

This class lets you perform SAX style parsing on HTML with HTML error correction.

Here is a basic usage example:

class MyDoc < Nokogiri::XML::SAX::Document
  def start_element name, attributes = []
    puts "found a #{name}"
  end
end

parser = Nokogiri::HTML4::SAX::Parser.new(MyDoc.new)
parser.parse(File.read(ARGV[0], mode: 'rb'))

For more information on SAX parsers, see Nokogiri::XML::SAX

Since:

  • v1.12.0

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Nokogiri::XML::SAX::Parser

Instance Method Details

#parse_file(filename, encoding = "UTF-8") {|ctx| ... } ⇒ Object

Parse a file with filename

Yields:

  • (ctx)

Raises:

  • (ArgumentError)

Since:

  • v1.12.0



49
50
51
52
53
54
55
56
# File 'lib/nokogiri/html4/sax/parser.rb', line 49

def parse_file(filename, encoding = "UTF-8")
  raise ArgumentError unless filename
  raise Errno::ENOENT unless File.exist?(filename)
  raise Errno::EISDIR if File.directory?(filename)
  ctx = ParserContext.file(filename, encoding)
  yield ctx if block_given?
  ctx.parse_with(self)
end

#parse_io(io, encoding = "UTF-8") {|ctx| ... } ⇒ Object

Parse given io

Yields:

  • (ctx)

Since:

  • v1.12.0



39
40
41
42
43
44
45
# File 'lib/nokogiri/html4/sax/parser.rb', line 39

def parse_io(io, encoding = "UTF-8")
  check_encoding(encoding)
  @encoding = encoding
  ctx = ParserContext.io(io, ENCODINGS[encoding])
  yield ctx if block_given?
  ctx.parse_with(self)
end

#parse_memory(data, encoding = "UTF-8") {|ctx| ... } ⇒ Object

Parse html stored in data using encoding

Yields:

  • (ctx)

Raises:

  • (ArgumentError)

Since:

  • v1.12.0



29
30
31
32
33
34
35
# File 'lib/nokogiri/html4/sax/parser.rb', line 29

def parse_memory(data, encoding = "UTF-8")
  raise ArgumentError unless data
  return unless data.length > 0
  ctx = ParserContext.memory(data, encoding)
  yield ctx if block_given?
  ctx.parse_with(self)
end