blob: bf57ea90ef6623af9ea1fa05fc54fab96a9377ab [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Standard Module \sectcode{htmllib}}
Guido van Rossuma12ef941995-02-27 17:53:25 +00002\stmodindex{htmllib}
Guido van Rossum86751151995-02-28 17:14:32 +00003\index{HTML}
4\index{hypertext}
5
6\renewcommand{\indexsubitem}{(in module htmllib)}
7
Fred Drake58d7f691996-10-08 21:52:23 +00008This module defines a class which can serve as a base for parsing text
9files formatted in the HyperText Mark-up Language (HTML). The class
10is not directly concerned with I/O --- it must be provided with input
11in string form via a method, and makes calls to methods of a
12``formatter'' object in order to produce output. The
13\code{HTMLParser} class is designed to be used as a base class for
14other classes in order to add functionality, and allows most of its
15methods to be extended or overridden. In turn, this class is derived
16from and extends the \code{SGMLParser} class defined in module
17\code{sgmllib}. Two implementations of formatter objects are
18provided in the \code{formatter} module; refer to the documentation
19for that module for information on the formatter interface.
Guido van Rossum86751151995-02-28 17:14:32 +000020\index{SGML}
21\stmodindex{sgmllib}
22\ttindex{SGMLParser}
23\index{formatter}
Fred Drake58d7f691996-10-08 21:52:23 +000024\stmodindex{formatter}
Guido van Rossum86751151995-02-28 17:14:32 +000025
26The following is a summary of the interface defined by
27\code{sgmllib.SGMLParser}:
28
29\begin{itemize}
30
31\item
32The interface to feed data to an instance is through the \code{feed()}
33method, which takes a string argument. This can be called with as
Fred Drake58d7f691996-10-08 21:52:23 +000034little or as much text at a time as desired; \code{p.feed(a);
35p.feed(b)} has the same effect as \code{p.feed(a+b)}. When the data
36contains complete HTML tags, these are processed immediately;
37incomplete elements are saved in a buffer. To force processing of all
38unprocessed data, call the \code{close()} method.
Guido van Rossum86751151995-02-28 17:14:32 +000039
Fred Drake58d7f691996-10-08 21:52:23 +000040For example, to parse the entire contents of a file, use:
41\begin{verbatim}
42parser.feed(open('myfile.html').read())
43parser.close()
44\end{verbatim}
Guido van Rossum86751151995-02-28 17:14:32 +000045
46\item
47The interface to define semantics for HTML tags is very simple: derive
48a class and define methods called \code{start_\var{tag}()},
49\code{end_\var{tag}()}, or \code{do_\var{tag}()}. The parser will
50call these at appropriate moments: \code{start_\var{tag}} or
51\code{do_\var{tag}} is called when an opening tag of the form
52\code{<\var{tag} ...>} is encountered; \code{end_\var{tag}} is called
53when a closing tag of the form \code{<\var{tag}>} is encountered. If
54an opening tag requires a corresponding closing tag, like \code{<H1>}
55... \code{</H1>}, the class should define the \code{start_\var{tag}}
56method; if a tag requires no closing tag, like \code{<P>}, the class
57should define the \code{do_\var{tag}} method.
58
59\end{itemize}
60
Fred Drake58d7f691996-10-08 21:52:23 +000061The module defines a single class:
Guido van Rossum86751151995-02-28 17:14:32 +000062
Fred Drake58d7f691996-10-08 21:52:23 +000063\begin{funcdesc}{HTMLParser}{formatter}
64This is the basic HTML parser class. It supports all entity names
65required by the HTML 2.0 specification (RFC 1866). It also defines
66handlers for all HTML 2.0 and many HTML 3.0 and 3.2 elements.
Guido van Rossum86751151995-02-28 17:14:32 +000067\end{funcdesc}
68
Fred Drake58d7f691996-10-08 21:52:23 +000069In addition to tag methods, the \code{HTMLParser} class provides some
70additional methods and instance variables for use within tag methods.
71
Fred Drake8f925951996-10-09 16:13:22 +000072\renewcommand{\indexsubitem}{({\tt HTMLParser} method)}
73
Fred Drake58d7f691996-10-08 21:52:23 +000074\begin{datadesc}{formatter}
75This is the formatter instance associated with the parser.
76\end{datadesc}
77
78\begin{datadesc}{nofill}
79Boolean flag which should be true when whitespace should not be
80collapsed, or false when it should be. In general, this should only
81be true when character data is to be treated as ``preformatted'' text,
82as within a \code{<PRE>} element. The default value is false. This
83affects the operation of \code{handle_data()} and \code{save_end()}.
84\end{datadesc}
85
86\begin{funcdesc}{anchor_bgn}{href\, name\, type}
87This method is called at the start of an anchor region. The arguments
88correspond to the attributes of the \code{<A>} tag with the same
89names. The default implementation maintains a list of hyperlinks
90(defined by the \code{href} argument) within the document. The list
91of hyperlinks is available as the data attribute \code{anchorlist}.
Guido van Rossum86751151995-02-28 17:14:32 +000092\end{funcdesc}
93
Fred Drake58d7f691996-10-08 21:52:23 +000094\begin{funcdesc}{anchor_end}{}
95This method is called at the end of an anchor region. The default
96implementation adds a textual footnote marker using an index into the
97list of hyperlinks created by \code{anchor_bgn()}.
Guido van Rossum86751151995-02-28 17:14:32 +000098\end{funcdesc}
99
Fred Drake58d7f691996-10-08 21:52:23 +0000100\begin{funcdesc}{handle_image}{source\, alt\optional{\, ismap\optional{\, align\optional{\, width\optional{\, height}}}}}
101This method is called to handle images. The default implementation
102simply passes the \code{alt} value to the \code{handle_data()}
103method.
Guido van Rossum86751151995-02-28 17:14:32 +0000104\end{funcdesc}
105
Fred Drake58d7f691996-10-08 21:52:23 +0000106\begin{funcdesc}{save_bgn}{}
107Begins saving character data in a buffer instead of sending it to the
108formatter object. Retrieve the stored data via \code{save_end()}
109Use of the \code{save_bgn()} / \code{save_end()} pair may not be
110nested.
Guido van Rossum86751151995-02-28 17:14:32 +0000111\end{funcdesc}
112
Fred Drake58d7f691996-10-08 21:52:23 +0000113\begin{funcdesc}{save_end}{}
114Ends buffering character data and returns all data saved since the
115preceeding call to \code{save_bgn()}. If \code{nofill} flag is false,
116whitespace is collapsed to single spaces. A call to this method
117without a preceeding call to \code{save_bgn()} will raise a
118\code{TypeError} exception.
Guido van Rossum86751151995-02-28 17:14:32 +0000119\end{funcdesc}