source: vendor/python/2.5/Doc/lib/libhtmllib.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 7.1 KB
Line 
1\section{\module{htmllib} ---
2 A parser for HTML documents}
3
4\declaremodule{standard}{htmllib}
5\modulesynopsis{A parser for HTML documents.}
6
7\index{HTML}
8\index{hypertext}
9
10
11This 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
16\class{HTMLParser} class is designed to be used as a base class for
17other classes in order to add functionality, and allows most of its
18methods to be extended or overridden. In turn, this class is derived
19from and extends the \class{SGMLParser} class defined in module
20\refmodule{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 \refmodule{formatter}\refstmodindex{formatter}\ module; refer to the
24documentation for that module for information on the formatter
25interface.
26\withsubitem{(in module sgmllib)}{\ttindex{SGMLParser}}
27
28The following is a summary of the interface defined by
29\class{sgmllib.SGMLParser}:
30
31\begin{itemize}
32
33\item
34The interface to feed data to an instance is through the \method{feed()}
35method, which takes a string argument. This can be called with as
36little or as much text at a time as desired; \samp{p.feed(a);
37p.feed(b)} has the same effect as \samp{p.feed(a+b)}. When the data
38contains complete HTML markup constructs, these are processed immediately;
39incomplete constructs are saved in a buffer. To force processing of all
40unprocessed data, call the \method{close()} method.
41
42For example, to parse the entire contents of a file, use:
43\begin{verbatim}
44parser.feed(open('myfile.html').read())
45parser.close()
46\end{verbatim}
47
48\item
49The interface to define semantics for HTML tags is very simple: derive
50a class and define methods called \method{start_\var{tag}()},
51\method{end_\var{tag}()}, or \method{do_\var{tag}()}. The parser will
52call these at appropriate moments: \method{start_\var{tag}} or
53\method{do_\var{tag}()} is called when an opening tag of the form
54\code{<\var{tag} ...>} is encountered; \method{end_\var{tag}()} is called
55when a closing tag of the form \code{<\var{tag}>} is encountered. If
56an opening tag requires a corresponding closing tag, like \code{<H1>}
57... \code{</H1>}, the class should define the \method{start_\var{tag}()}
58method; if a tag requires no closing tag, like \code{<P>}, the class
59should define the \method{do_\var{tag}()} method.
60
61\end{itemize}
62
63The module defines a parser class and an exception:
64
65\begin{classdesc}{HTMLParser}{formatter}
66This is the basic HTML parser class. It supports all entity names
67required by the XHTML 1.0 Recommendation (\url{http://www.w3.org/TR/xhtml1}).
68It also defines handlers for all HTML 2.0 and many HTML 3.0 and 3.2 elements.
69\end{classdesc}
70
71\begin{excdesc}{HTMLParseError}
72Exception raised by the \class{HTMLParser} class when it encounters an
73error while parsing.
74\versionadded{2.4}
75\end{excdesc}
76
77
78\begin{seealso}
79 \seemodule{formatter}{Interface definition for transforming an
80 abstract flow of formatting events into
81 specific output events on writer objects.}
82 \seemodule{HTMLParser}{Alternate HTML parser that offers a slightly
83 lower-level view of the input, but is
84 designed to work with XHTML, and does not
85 implement some of the SGML syntax not used in
86 ``HTML as deployed'' and which isn't legal
87 for XHTML.}
88 \seemodule{htmlentitydefs}{Definition of replacement text for XHTML 1.0
89 entities.}
90 \seemodule{sgmllib}{Base class for \class{HTMLParser}.}
91\end{seealso}
92
93
94\subsection{HTMLParser Objects \label{html-parser-objects}}
95
96In addition to tag methods, the \class{HTMLParser} class provides some
97additional methods and instance variables for use within tag methods.
98
99\begin{memberdesc}{formatter}
100This is the formatter instance associated with the parser.
101\end{memberdesc}
102
103\begin{memberdesc}{nofill}
104Boolean flag which should be true when whitespace should not be
105collapsed, or false when it should be. In general, this should only
106be true when character data is to be treated as ``preformatted'' text,
107as within a \code{<PRE>} element. The default value is false. This
108affects the operation of \method{handle_data()} and \method{save_end()}.
109\end{memberdesc}
110
111
112\begin{methoddesc}{anchor_bgn}{href, name, type}
113This method is called at the start of an anchor region. The arguments
114correspond to the attributes of the \code{<A>} tag with the same
115names. The default implementation maintains a list of hyperlinks
116(defined by the \code{HREF} attribute for \code{<A>} tags) within the
117document. The list of hyperlinks is available as the data attribute
118\member{anchorlist}.
119\end{methoddesc}
120
121\begin{methoddesc}{anchor_end}{}
122This method is called at the end of an anchor region. The default
123implementation adds a textual footnote marker using an index into the
124list of hyperlinks created by \method{anchor_bgn()}.
125\end{methoddesc}
126
127\begin{methoddesc}{handle_image}{source, alt\optional{, ismap\optional{,
128 align\optional{, width\optional{, height}}}}}
129This method is called to handle images. The default implementation
130simply passes the \var{alt} value to the \method{handle_data()}
131method.
132\end{methoddesc}
133
134\begin{methoddesc}{save_bgn}{}
135Begins saving character data in a buffer instead of sending it to the
136formatter object. Retrieve the stored data via \method{save_end()}.
137Use of the \method{save_bgn()} / \method{save_end()} pair may not be
138nested.
139\end{methoddesc}
140
141\begin{methoddesc}{save_end}{}
142Ends buffering character data and returns all data saved since the
143preceding call to \method{save_bgn()}. If the \member{nofill} flag is
144false, whitespace is collapsed to single spaces. A call to this
145method without a preceding call to \method{save_bgn()} will raise a
146\exception{TypeError} exception.
147\end{methoddesc}
148
149
150
151\section{\module{htmlentitydefs} ---
152 Definitions of HTML general entities}
153
154\declaremodule{standard}{htmlentitydefs}
155\modulesynopsis{Definitions of HTML general entities.}
156\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
157
158This module defines three dictionaries, \code{name2codepoint},
159\code{codepoint2name}, and \code{entitydefs}. \code{entitydefs} is
160used by the \refmodule{htmllib} module to provide the
161\member{entitydefs} member of the \class{HTMLParser} class. The
162definition provided here contains all the entities defined by XHTML 1.0
163that can be handled using simple textual substitution in the Latin-1
164character set (ISO-8859-1).
165
166
167\begin{datadesc}{entitydefs}
168 A dictionary mapping XHTML 1.0 entity definitions to their
169 replacement text in ISO Latin-1.
170
171\end{datadesc}
172
173\begin{datadesc}{name2codepoint}
174 A dictionary that maps HTML entity names to the Unicode codepoints.
175 \versionadded{2.3}
176\end{datadesc}
177
178\begin{datadesc}{codepoint2name}
179 A dictionary that maps Unicode codepoints to HTML entity names.
180 \versionadded{2.3}
181\end{datadesc}
Note: See TracBrowser for help on using the repository browser.