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