Skip to content

Instantly share code, notes, and snippets.

@dmgarland
Created November 18, 2011 10:00
Show Gist options
  • Save dmgarland/1376053 to your computer and use it in GitHub Desktop.
Save dmgarland/1376053 to your computer and use it in GitHub Desktop.
HTML Push Parser with Nokogiri?
module Nokogiri
module XML
module SAX
class PushParser
# The Nokogiri::XML::SAX::Document on which the PushParser will be
# operating
attr_accessor :document
###
# Create a new PushParser with +doc+ as the SAX Document, providing
# an optional +file_name+ and +encoding+
def initialize(doc = XML::SAX::Document.new, file_name = nil, encoding = 'UTF-8')
@document = doc
@encoding = encoding
# Doesn't seem to treat the input as HTML
@sax_parser = HTML::SAX::Parser.new(doc)
## Create our push parser context
initialize_native(@sax_parser, file_name)
end
###
# Write a +chunk+ of XML to the PushParser. Any callback methods
# that can be called will be called immediately.
def write chunk, last_chunk = false
native_write(chunk, last_chunk)
end
alias :<< :write
###
# Finish the parsing. This method is only necessary for
# Nokogiri::XML::SAX::Document#end_document to be called.
def finish
write '', true
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment