.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "HTML::HTML5::Parser 3" .TH HTML::HTML5::Parser 3 2023-01-20 "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME HTML::HTML5::Parser \- parse HTML reliably .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use HTML::HTML5::Parser; \& \& my $parser = HTML::HTML5::Parser\->new; \& my $doc = $parser\->parse_string(<<\*(AqEOT\*(Aq); \& \&
Foo bar. \&
BazQuux. \& EOT \& \& my $fdoc = $parser\->parse_file( $html_file_name ); \& my $fhdoc = $parser\->parse_fh( $html_file_handle ); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This library is substantially the same as the non-CPAN module Whatpm::HTML. Changes include: .IP \(bu 8 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. .IP \(bu 8 Constructs an XML::LibXML::Document as the result of parsing. .IP \(bu 8 Via bundling and modifications, removed external dependencies on non-CPAN packages. .SS Constructor .IX Subsection "Constructor" .ie n .IP """new""" 8 .el .IP \f(CWnew\fR 8 .IX Item "new" .Vb 3 \& $parser = HTML::HTML5::Parser\->new; \& # or \& $parser = HTML::HTML5::Parser\->new(no_cache => 1); .Ve .Sp The constructor does nothing interesting besides take one flag argument, \f(CW\*(C`no_cache => 1\*(C'\fR, 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 \&\f(CW\*(C`/source_line\*(C'\fR will not work, and must be run from an instance of this parser. .SS "XML::LibXML\-Compatible Methods" .IX Subsection "XML::LibXML-Compatible Methods" .ie n .IP """parse_file"", ""parse_html_file""" 4 .el .IP "\f(CWparse_file\fR, \f(CWparse_html_file\fR" 4 .IX Item "parse_file, parse_html_file" .Vb 1 \& $doc = $parser\->parse_file( $html_file_name [,\e%opts] ); .Ve .Sp This function parses an HTML document from a file or network; \&\f(CW$html_file_name\fR can be either a filename or an URL. .Sp Options include 'encoding' to indicate file encoding (e.g. \&'utf\-8') and 'user_agent' which should be a blessed \f(CW\*(C`LWP::UserAgent\*(C'\fR (or HTTP::Tiny) object to be used when retrieving URLs. .Sp 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. .Sp 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 \f(CW$options\fR\->{'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. .Sp If an options hashref was passed, parse_file will set \f(CW$options\fR\->{'response'} to the HTTP::Response object obtained by retrieving the URI. .ie n .IP """parse_fh"", ""parse_html_fh""" 4 .el .IP "\f(CWparse_fh\fR, \f(CWparse_html_fh\fR" 4 .IX Item "parse_fh, parse_html_fh" .Vb 1 \& $doc = $parser\->parse_fh( $io_fh [,\e%opts] ); .Ve .Sp \&\f(CWparse_fh()\fR parses a IOREF or a subclass of \f(CW\*(C`IO::Handle\*(C'\fR. .Sp Options include 'encoding' to indicate file encoding (e.g. \&'utf\-8'). .ie n .IP """parse_string"", ""parse_html_string""" 4 .el .IP "\f(CWparse_string\fR, \f(CWparse_html_string\fR" 4 .IX Item "parse_string, parse_html_string" .Vb 1 \& $doc = $parser\->parse_string( $html_string [,\e%opts] ); .Ve .Sp This function is similar to \f(CWparse_fh()\fR, but it parses an HTML document that is available as a single string in memory. .Sp Options include 'encoding' to indicate file encoding (e.g. \&'utf\-8'). .ie n .IP """load_xml"", ""load_html""" 4 .el .IP "\f(CWload_xml\fR, \f(CWload_html\fR" 4 .IX Item "load_xml, load_html" Wrappers for the parse_* functions. These should be roughly compatible with the equivalently named functions in XML::LibXML. .Sp Note that \f(CW\*(C`load_xml\*(C'\fR first attempts to parse as real XML, falling back to HTML5 parsing; \f(CW\*(C`load_html\*(C'\fR just goes straight for HTML5. .ie n .IP """parse_balanced_chunk""" 4 .el .IP \f(CWparse_balanced_chunk\fR 4 .IX Item "parse_balanced_chunk" .Vb 1 \& $fragment = $parser\->parse_balanced_chunk( $string [,\e%opts] ); .Ve .Sp 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... .Sp Consider the following string: .Sp .Vb 1 \& Hello World .Ve .Sp What is the proper way to parse that? If it were found in a document like this: .Sp .Vb 8 \& \&
\& Hello |
\& Hello |
\*(C'\fR element", the \f(CW\*(C`Hello\*(C'\fR bit did not strictly end up within the \f(CW\*(C` | \*(C'\fR element (not
even within the \f(CW\*(C` |