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