NAME HTML::HTML5::Parser - parse HTML reliably SYNOPSIS use HTML::HTML5::Parser; my $parser = HTML::HTML5::Parser->new; my $doc = $parser->parse_string(<<'EOT');
Foo bar.
BazQuux. EOT my $fdoc = $parser->parse_file( $html_file_name ); my $fhdoc = $parser->parse_fh( $html_file_handle ); DESCRIPTION This library is substantially the same as the non-CPAN module Whatpm::HTML. Changes include: * Provides an XML::LibXML-like DOM interface. If you usually use XML::LibXML's DOM parser, this should be a drop-in solution for tag soup HTML. * Constructs an XML::LibXML::Document as the result of parsing. * Via bundling and modifications, removed external dependencies on non-CPAN packages. Constructor `new` $parser = HTML::HTML5::Parser->new; # or $parser = HTML::HTML5::Parser->new(no_cache => 1); The constructor does nothing interesting besides take one flag argument, `no_cache => 1`, to disable the global element metadata cache. Disabling the cache is handy for conserving memory if you parse a large number of documents, however, class methods such as `/source_line` will not work, and must be run from an instance of this parser. XML::LibXML-Compatible Methods `parse_file`, `parse_html_file` $doc = $parser->parse_file( $html_file_name [,\%opts] ); This function parses an HTML document from a file or network; $html_file_name can be either a filename or an URL. Options include 'encoding' to indicate file encoding (e.g. 'utf-8') and 'user_agent' which should be a blessed `LWP::UserAgent` (or HTTP::Tiny) object to be used when retrieving URLs. If requesting a URL and the response Content-Type header indicates an XML-based media type (such as XHTML), XML::LibXML::Parser will be used automatically (instead of the tag soup parser). The XML parser can be told to use a DTD catalogue by setting the option 'xml_catalogue' to the filename of the catalogue. HTML (tag soup) parsing can be forced using the option 'force_html', even when an XML media type is returned. If an options hashref was passed, parse_file will set $options->{'parser_used'} to the name of the class used to parse the URL, to allow the calling code to double-check which parser was used afterwards. If an options hashref was passed, parse_file will set $options->{'response'} to the HTTP::Response object obtained by retrieving the URI. `parse_fh`, `parse_html_fh` $doc = $parser->parse_fh( $io_fh [,\%opts] ); `parse_fh()` parses a IOREF or a subclass of `IO::Handle`. Options include 'encoding' to indicate file encoding (e.g. 'utf-8'). `parse_string`, `parse_html_string` $doc = $parser->parse_string( $html_string [,\%opts] ); This function is similar to `parse_fh()`, but it parses an HTML document that is available as a single string in memory. Options include 'encoding' to indicate file encoding (e.g. 'utf-8'). `load_xml`, `load_html` Wrappers for the parse_* functions. These should be roughly compatible with the equivalently named functions in XML::LibXML. Note that `load_xml` first attempts to parse as real XML, falling back to HTML5 parsing; `load_html` just goes straight for HTML5. `parse_balanced_chunk` $fragment = $parser->parse_balanced_chunk( $string [,\%opts] ); This method is roughly equivalent to XML::LibXML's method of the same name, but unlike XML::LibXML, and despite its name it does not require the chunk to be "balanced". This method is somewhat black magic, but should work, and do the proper thing in most cases. Of course, the proper thing might not be what you'd expect! I'll try to keep this explanation as brief as possible... Consider the following string: Hello World What is the proper way to parse that? If it were found in a document like this:
Hello |
Hello |
` element", the `Hello` bit did not strictly end up within the ` | ` element (not even within the
` |