blob: 899e76ede3595e0878201f8facf89bbe7b301728 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{htmllib}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{htmllib}
3
4\modulesynopsis{A parser for HTML documents.}
5
Guido van Rossum86751151995-02-28 17:14:32 +00006\index{HTML}
7\index{hypertext}
8
Guido van Rossum86751151995-02-28 17:14:32 +00009
Fred Drake58d7f691996-10-08 21:52:23 +000010This module defines a class which can serve as a base for parsing text
11files formatted in the HyperText Mark-up Language (HTML). The class
12is not directly concerned with I/O --- it must be provided with input
13in string form via a method, and makes calls to methods of a
14``formatter'' object in order to produce output. The
Fred Drake526467c1998-02-10 21:42:27 +000015\class{HTMLParser} class is designed to be used as a base class for
Fred Drake58d7f691996-10-08 21:52:23 +000016other classes in order to add functionality, and allows most of its
17methods to be extended or overridden. In turn, this class is derived
Fred Drake526467c1998-02-10 21:42:27 +000018from and extends the \class{SGMLParser} class defined in module
19\module{sgmllib}\refstmodindex{sgmllib}. The \class{HTMLParser}
20implementation supports the HTML 2.0 language as described in
21\rfc{1866}. Two implementations of formatter objects are provided in
22the \module{formatter}\refstmodindex{formatter} module; refer to the
23documentation for that module for information on the formatter
24interface.
Guido van Rossum86751151995-02-28 17:14:32 +000025\index{SGML}
Fred Drake51375ae1998-03-12 14:39:09 +000026\withsubitem{(in module sgmllib)}{\ttindex{SGMLParser}}
Guido van Rossum86751151995-02-28 17:14:32 +000027\index{formatter}
28
29The following is a summary of the interface defined by
Fred Drake526467c1998-02-10 21:42:27 +000030\class{sgmllib.SGMLParser}:
Guido van Rossum86751151995-02-28 17:14:32 +000031
32\begin{itemize}
33
34\item
Fred Drake526467c1998-02-10 21:42:27 +000035The interface to feed data to an instance is through the \method{feed()}
Guido van Rossum86751151995-02-28 17:14:32 +000036method, which takes a string argument. This can be called with as
Fred Drake526467c1998-02-10 21:42:27 +000037little or as much text at a time as desired; \samp{p.feed(a);
38p.feed(b)} has the same effect as \samp{p.feed(a+b)}. When the data
Fred Drake58d7f691996-10-08 21:52:23 +000039contains complete HTML tags, these are processed immediately;
40incomplete elements are saved in a buffer. To force processing of all
Fred Drake526467c1998-02-10 21:42:27 +000041unprocessed data, call the \method{close()} method.
Guido van Rossum86751151995-02-28 17:14:32 +000042
Fred Drake58d7f691996-10-08 21:52:23 +000043For example, to parse the entire contents of a file, use:
Fred Drake19479911998-02-13 06:58:54 +000044\begin{verbatim}
Fred Drake58d7f691996-10-08 21:52:23 +000045parser.feed(open('myfile.html').read())
46parser.close()
Fred Drake19479911998-02-13 06:58:54 +000047\end{verbatim}
Fred Drake51375ae1998-03-12 14:39:09 +000048
Guido van Rossum86751151995-02-28 17:14:32 +000049\item
50The interface to define semantics for HTML tags is very simple: derive
51a class and define methods called \code{start_\var{tag}()},
52\code{end_\var{tag}()}, or \code{do_\var{tag}()}. The parser will
53call these at appropriate moments: \code{start_\var{tag}} or
Fred Drake526467c1998-02-10 21:42:27 +000054\code{do_\var{tag}()} is called when an opening tag of the form
55\code{<\var{tag} ...>} is encountered; \code{end_\var{tag}()} is called
Guido van Rossum86751151995-02-28 17:14:32 +000056when a closing tag of the form \code{<\var{tag}>} is encountered. If
57an opening tag requires a corresponding closing tag, like \code{<H1>}
Fred Drake526467c1998-02-10 21:42:27 +000058... \code{</H1>}, the class should define the \code{start_\var{tag}()}
Guido van Rossum86751151995-02-28 17:14:32 +000059method; if a tag requires no closing tag, like \code{<P>}, the class
Fred Drake526467c1998-02-10 21:42:27 +000060should define the \code{do_\var{tag}()} method.
Guido van Rossum86751151995-02-28 17:14:32 +000061
62\end{itemize}
63
Fred Drake58d7f691996-10-08 21:52:23 +000064The module defines a single class:
Guido van Rossum86751151995-02-28 17:14:32 +000065
Fred Drake51375ae1998-03-12 14:39:09 +000066\begin{classdesc}{HTMLParser}{formatter}
Fred Drake58d7f691996-10-08 21:52:23 +000067This is the basic HTML parser class. It supports all entity names
Fred Drakec5891241998-02-09 19:16:20 +000068required by the HTML 2.0 specification (\rfc{1866}). It also defines
Fred Drake58d7f691996-10-08 21:52:23 +000069handlers for all HTML 2.0 and many HTML 3.0 and 3.2 elements.
Fred Drake51375ae1998-03-12 14:39:09 +000070\end{classdesc}
Guido van Rossum86751151995-02-28 17:14:32 +000071
Fred Drake526467c1998-02-10 21:42:27 +000072In addition to tag methods, the \class{HTMLParser} class provides some
Fred Drake58d7f691996-10-08 21:52:23 +000073additional methods and instance variables for use within tag methods.
74
Fred Drake8fe533e1998-03-27 05:27:08 +000075\begin{memberdesc}{formatter}
Fred Drake58d7f691996-10-08 21:52:23 +000076This is the formatter instance associated with the parser.
Fred Drake8fe533e1998-03-27 05:27:08 +000077\end{memberdesc}
Fred Drake58d7f691996-10-08 21:52:23 +000078
Fred Drake8fe533e1998-03-27 05:27:08 +000079\begin{memberdesc}{nofill}
Fred Drake58d7f691996-10-08 21:52:23 +000080Boolean flag which should be true when whitespace should not be
81collapsed, or false when it should be. In general, this should only
82be true when character data is to be treated as ``preformatted'' text,
83as within a \code{<PRE>} element. The default value is false. This
Fred Drake526467c1998-02-10 21:42:27 +000084affects the operation of \method{handle_data()} and \method{save_end()}.
Fred Drake8fe533e1998-03-27 05:27:08 +000085\end{memberdesc}
Fred Drake58d7f691996-10-08 21:52:23 +000086
Fred Drake526467c1998-02-10 21:42:27 +000087
Fred Drake8fe533e1998-03-27 05:27:08 +000088\begin{methoddesc}{anchor_bgn}{href, name, type}
Fred Drake58d7f691996-10-08 21:52:23 +000089This method is called at the start of an anchor region. The arguments
90correspond to the attributes of the \code{<A>} tag with the same
91names. The default implementation maintains a list of hyperlinks
Fred Drake526467c1998-02-10 21:42:27 +000092(defined by the \code{href} attribute) within the document. The list
Fred Drake58d7f691996-10-08 21:52:23 +000093of hyperlinks is available as the data attribute \code{anchorlist}.
Fred Drake8fe533e1998-03-27 05:27:08 +000094\end{methoddesc}
Guido van Rossum86751151995-02-28 17:14:32 +000095
Fred Drake8fe533e1998-03-27 05:27:08 +000096\begin{methoddesc}{anchor_end}{}
Fred Drake58d7f691996-10-08 21:52:23 +000097This method is called at the end of an anchor region. The default
98implementation adds a textual footnote marker using an index into the
Fred Drake526467c1998-02-10 21:42:27 +000099list of hyperlinks created by \method{anchor_bgn()}.
Fred Drake8fe533e1998-03-27 05:27:08 +0000100\end{methoddesc}
Guido van Rossum86751151995-02-28 17:14:32 +0000101
Fred Drake8fe533e1998-03-27 05:27:08 +0000102\begin{methoddesc}{handle_image}{source, alt\optional{, ismap\optional{, align\optional{, width\optional{, height}}}}}
Fred Drake58d7f691996-10-08 21:52:23 +0000103This method is called to handle images. The default implementation
Fred Drake526467c1998-02-10 21:42:27 +0000104simply passes the \var{alt} value to the \method{handle_data()}
Fred Drake58d7f691996-10-08 21:52:23 +0000105method.
Fred Drake8fe533e1998-03-27 05:27:08 +0000106\end{methoddesc}
Guido van Rossum86751151995-02-28 17:14:32 +0000107
Fred Drake8fe533e1998-03-27 05:27:08 +0000108\begin{methoddesc}{save_bgn}{}
Fred Drake58d7f691996-10-08 21:52:23 +0000109Begins saving character data in a buffer instead of sending it to the
Fred Drake526467c1998-02-10 21:42:27 +0000110formatter object. Retrieve the stored data via \method{save_end()}.
111Use of the \method{save_bgn()} / \method{save_end()} pair may not be
Fred Drake58d7f691996-10-08 21:52:23 +0000112nested.
Fred Drake8fe533e1998-03-27 05:27:08 +0000113\end{methoddesc}
Guido van Rossum86751151995-02-28 17:14:32 +0000114
Fred Drake8fe533e1998-03-27 05:27:08 +0000115\begin{methoddesc}{save_end}{}
Fred Drake58d7f691996-10-08 21:52:23 +0000116Ends buffering character data and returns all data saved since the
Fred Drake526467c1998-02-10 21:42:27 +0000117preceeding call to \method{save_bgn()}. If the \code{nofill} flag is
118false, whitespace is collapsed to single spaces. A call to this
119method without a preceeding call to \method{save_bgn()} will raise a
120\exception{TypeError} exception.
Fred Drake8fe533e1998-03-27 05:27:08 +0000121\end{methoddesc}