Convenience methods for parsing XML/HTML.

This commit is contained in:
Yorick Peterse 2014-09-03 09:30:56 +02:00
parent efc3827865
commit 71f2b42074
3 changed files with 49 additions and 0 deletions

View File

@ -2,6 +2,9 @@ require 'ast'
require 'set'
require 'stringio'
require_relative 'oga/version'
require_relative 'oga/oga'
# Load these first so that the native extensions don't have to define the
# Oga::XML namespace.
require_relative 'oga/xml/lexer'

27
lib/oga/oga.rb Normal file
View File

@ -0,0 +1,27 @@
module Oga
##
# Parses the given XML document.
#
# @example
# document = Oga.parse_xml('<root>Hello</root>')
#
# @param [String|IO] xml The XML input to parse.
# @return [Oga::XML::Document]
#
def self.parse_xml(xml)
return XML::Parser.new(xml).parse
end
##
# Parses the given HTML document.
#
# @example
# document = Oga.parse_html('<html>...</html>')
#
# @param [String|IO] html The HTML input to parse.
# @return [Oga::XML::Document]
#
def self.parse_html(html)
return HTML::Parser.new(html).parse
end
end # Oga

19
spec/oga/oga_spec.rb Normal file
View File

@ -0,0 +1,19 @@
require 'spec_helper'
describe Oga do
context 'parse_xml' do
example 'parse an XML document' do
document = described_class.parse_xml('<root>foo</root>')
document.is_a?(Oga::XML::Document).should == true
end
end
context 'parse_html' do
example 'parse an HTML document' do
document = described_class.parse_xml('<html><body></body></html>')
document.is_a?(Oga::XML::Document).should == true
end
end
end