17.4 The HTML ModuleThe HTML modules provide an interface to parse HTML documents. After you parse the document, you can print or display it according to the markup tags, or you can extract specific information such as hyperlinks. The HTML::Parser module provides the base class for the usable HTML modules. It provides methods for reading in HTML text from either a string or a file and then separating out the syntactic structures and data. As a base class, Parser does virtually nothing on its own. The other modules call it internally and override its empty methods for their own purposes. However, the HTML::Parser class is useful to you if you want to write your own classes for parsing and formatting HTML. HTML::TreeBuilder is a class that parses HTML into a syntax tree. In a syntax tree, each element of the HTML, such as container elements with beginning and end tags, is stored relative to other elements. This preserves the nested structure and behavior of HTML and its hierarchy. A syntax tree of the TreeBuilder class is formed of connected nodes that represent each element of the HTML document. These nodes are saved as objects from the HTML::Element class. An HTML::Element object stores all the information from an HTML tag: the start tag, end tag, attributes, plain text, and pointers to any nested elements. The remaining classes of the HTML modules use the syntax trees and its nodes of element objects to output useful information from the HTML documents. The format classes, such as HTML::FormatText and HTML::FormatPS, allow you to produce text and PostScript from HTML. The HTML::LinkExtor class extracts all of the links from a document. Additional modules provide means for replacing HTML character entities and implementing HTML tags as subroutines. 17.4.1 HTML::Parser
This module implements the base class for the other HTML modules.
A parser object is created with the The constructor takes no arguments.$p = HTML::Parser->new();
The parser object takes methods that read in HTML either from a string or
a file. The string-reading method can take data as several smaller chunks
if the HTML is too big. Each chunk of HTML will be appended to the object,
and the
When the The following list shows the internal methods contained in HTML::Parser:package HTML::MyParser; require HTML::Parser; @ISA=qw(HTML::MyParser); sub start { your subroutine defined here } 17.4.2 HTML::ElementThe HTML::Element module provides methods for dealing with nodes in an HTML syntax tree. You can get or set the contents of each node, traverse the tree, and delete a node. HTML::Element objects are used to represent elements of HTML. These elements include start and end tags, attributes, contained plain text, and other nested elements. The constructor for this class requires the name of the tag for its first argument. You may optionally specify initial attributes and values as hash elements in the constructor. For example: The new element is created for the anchor tag,$h = HTML::Element->new('a', 'href' => 'http://www.oreilly.com');
<a>
, which links to
the URL through its
href
attribute.
The following methods are provided for objects of the HTML::Element class:
17.4.3 HTML::TreeBuilderThe HTML::TreeBuilder class provides a parser that creates an HTML syntax tree. Each node of the tree is an HTML::Element object. This class inherits both HTML::Parser and HTML::Elements, so methods from both of those classes can be used on its objects. The methods provided by HTML::TreeBuilder control how the parsing is performed. Values for these methods are set by providing a boolean value for their arguments. Here are the methods: 17.4.4 HTML::FormatPS
The HTML::FormatPS module converts an HTML parse tree into PostScript.
The formatter object is created with the You can now give parsed HTML to the formatter and produce PostScript output for printing. HTML::FormatPS does not handle table or form elements at this time.$formatter = new HTML::FormatPS('papersize' => 'Letter');
The method for this class is The following list describes the attributes that can be set in the constructor:use HTML::FormatPS; $html = HTML::TreeBuilder->parse_file(somefile); $formatter = new HTML::FormatPS; print $formatter->format($html);
17.4.5 HTML::FormatTextThe HTML::FormatText takes a parsed HTML file and outputs a plain text version of it. None of the character attributes will be usable, i.e., bold or italic fonts, font sizes, etc.
This module is similar to FormatPS in that the constructor takes
attributes for formatting, and the The constructor can take two parameters:$formatter = new HTML::FormatText (leftmargin => 10, rightmargin => 80);
leftmargin
and
rightmargin
.
The value for the margins is given in column numbers. The aliases
lm
and
rm
can also be used.
The print $formatter->format($html); Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|