blob: b05c4702b4e8a901d24d60904813b43c5113b02d [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Standard Module \sectcode{htmllib}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-htmllib}
Guido van Rossuma12ef941995-02-27 17:53:25 +00003\stmodindex{htmllib}
Guido van Rossum86751151995-02-28 17:14:32 +00004\index{HTML}
5\index{hypertext}
6
Fred Drake19479911998-02-13 06:58:54 +00007\setindexsubitem{(in module htmllib)}
Guido van Rossum86751151995-02-28 17:14:32 +00008
Fred Drake58d7f691996-10-08 21:52:23 +00009This module defines a class which can serve as a base for parsing text
10files formatted in the HyperText Mark-up Language (HTML). The class
11is not directly concerned with I/O --- it must be provided with input
12in string form via a method, and makes calls to methods of a
13``formatter'' object in order to produce output. The
Fred Drake526467c1998-02-10 21:42:27 +000014\class{HTMLParser} class is designed to be used as a base class for
Fred Drake58d7f691996-10-08 21:52:23 +000015other classes in order to add functionality, and allows most of its
16methods to be extended or overridden. In turn, this class is derived
Fred Drake526467c1998-02-10 21:42:27 +000017from and extends the \class{SGMLParser} class defined in module
18\module{sgmllib}\refstmodindex{sgmllib}. The \class{HTMLParser}
19implementation supports the HTML 2.0 language as described in
20\rfc{1866}. Two implementations of formatter objects are provided in
21the \module{formatter}\refstmodindex{formatter} module; refer to the
22documentation for that module for information on the formatter
23interface.
Guido van Rossum86751151995-02-28 17:14:32 +000024\index{SGML}
Guido van Rossum86751151995-02-28 17:14:32 +000025\ttindex{SGMLParser}
26\index{formatter}
27
28The following is a summary of the interface defined by
Fred Drake526467c1998-02-10 21:42:27 +000029\class{sgmllib.SGMLParser}:
Guido van Rossum86751151995-02-28 17:14:32 +000030
31\begin{itemize}
32
33\item
Fred Drake526467c1998-02-10 21:42:27 +000034The interface to feed data to an instance is through the \method{feed()}
Guido van Rossum86751151995-02-28 17:14:32 +000035method, which takes a string argument. This can be called with as
Fred Drake526467c1998-02-10 21:42:27 +000036little 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
Fred Drake58d7f691996-10-08 21:52:23 +000038contains complete HTML tags, these are processed immediately;
39incomplete elements are saved in a buffer. To force processing of all
Fred Drake526467c1998-02-10 21:42:27 +000040unprocessed data, call the \method{close()} method.
Guido van Rossum86751151995-02-28 17:14:32 +000041
Fred Drake58d7f691996-10-08 21:52:23 +000042For example, to parse the entire contents of a file, use:
Fred Drake19479911998-02-13 06:58:54 +000043\begin{verbatim}
Fred Drake58d7f691996-10-08 21:52:23 +000044parser.feed(open('myfile.html').read())
45parser.close()
Fred Drake19479911998-02-13 06:58:54 +000046\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000047%
Guido van Rossum86751151995-02-28 17:14:32 +000048\item
49The interface to define semantics for HTML tags is very simple: derive
50a class and define methods called \code{start_\var{tag}()},
51\code{end_\var{tag}()}, or \code{do_\var{tag}()}. The parser will
52call these at appropriate moments: \code{start_\var{tag}} or
Fred Drake526467c1998-02-10 21:42:27 +000053\code{do_\var{tag}()} is called when an opening tag of the form
54\code{<\var{tag} ...>} is encountered; \code{end_\var{tag}()} is called
Guido van Rossum86751151995-02-28 17:14:32 +000055when a closing tag of the form \code{<\var{tag}>} is encountered. If
56an opening tag requires a corresponding closing tag, like \code{<H1>}
Fred Drake526467c1998-02-10 21:42:27 +000057... \code{</H1>}, the class should define the \code{start_\var{tag}()}
Guido van Rossum86751151995-02-28 17:14:32 +000058method; if a tag requires no closing tag, like \code{<P>}, the class
Fred Drake526467c1998-02-10 21:42:27 +000059should define the \code{do_\var{tag}()} method.
Guido van Rossum86751151995-02-28 17:14:32 +000060
61\end{itemize}
62
Fred Drake58d7f691996-10-08 21:52:23 +000063The module defines a single class:
Guido van Rossum86751151995-02-28 17:14:32 +000064
Fred Drake58d7f691996-10-08 21:52:23 +000065\begin{funcdesc}{HTMLParser}{formatter}
66This is the basic HTML parser class. It supports all entity names
Fred Drakec5891241998-02-09 19:16:20 +000067required by the HTML 2.0 specification (\rfc{1866}). It also defines
Fred Drake58d7f691996-10-08 21:52:23 +000068handlers for all HTML 2.0 and many HTML 3.0 and 3.2 elements.
Guido van Rossum86751151995-02-28 17:14:32 +000069\end{funcdesc}
70
Fred Drake526467c1998-02-10 21:42:27 +000071In addition to tag methods, the \class{HTMLParser} class provides some
Fred Drake58d7f691996-10-08 21:52:23 +000072additional methods and instance variables for use within tag methods.
73
Fred Drake19479911998-02-13 06:58:54 +000074\setindexsubitem{(HTMLParser attribute)}
Fred Drake8f925951996-10-09 16:13:22 +000075
Fred Drake58d7f691996-10-08 21:52:23 +000076\begin{datadesc}{formatter}
77This is the formatter instance associated with the parser.
78\end{datadesc}
79
80\begin{datadesc}{nofill}
81Boolean 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 Drake58d7f691996-10-08 21:52:23 +000086\end{datadesc}
87
Fred Drake19479911998-02-13 06:58:54 +000088\setindexsubitem{(HTMLParser method)}
Fred Drake526467c1998-02-10 21:42:27 +000089
Fred Drake58d7f691996-10-08 21:52:23 +000090\begin{funcdesc}{anchor_bgn}{href\, name\, type}
91This method is called at the start of an anchor region. The arguments
92correspond to the attributes of the \code{<A>} tag with the same
93names. The default implementation maintains a list of hyperlinks
Fred Drake526467c1998-02-10 21:42:27 +000094(defined by the \code{href} attribute) within the document. The list
Fred Drake58d7f691996-10-08 21:52:23 +000095of hyperlinks is available as the data attribute \code{anchorlist}.
Guido van Rossum86751151995-02-28 17:14:32 +000096\end{funcdesc}
97
Fred Drake58d7f691996-10-08 21:52:23 +000098\begin{funcdesc}{anchor_end}{}
99This method is called at the end of an anchor region. The default
100implementation adds a textual footnote marker using an index into the
Fred Drake526467c1998-02-10 21:42:27 +0000101list of hyperlinks created by \method{anchor_bgn()}.
Guido van Rossum86751151995-02-28 17:14:32 +0000102\end{funcdesc}
103
Fred Drake58d7f691996-10-08 21:52:23 +0000104\begin{funcdesc}{handle_image}{source\, alt\optional{\, ismap\optional{\, align\optional{\, width\optional{\, height}}}}}
105This method is called to handle images. The default implementation
Fred Drake526467c1998-02-10 21:42:27 +0000106simply passes the \var{alt} value to the \method{handle_data()}
Fred Drake58d7f691996-10-08 21:52:23 +0000107method.
Guido van Rossum86751151995-02-28 17:14:32 +0000108\end{funcdesc}
109
Fred Drake58d7f691996-10-08 21:52:23 +0000110\begin{funcdesc}{save_bgn}{}
111Begins saving character data in a buffer instead of sending it to the
Fred Drake526467c1998-02-10 21:42:27 +0000112formatter object. Retrieve the stored data via \method{save_end()}.
113Use of the \method{save_bgn()} / \method{save_end()} pair may not be
Fred Drake58d7f691996-10-08 21:52:23 +0000114nested.
Guido van Rossum86751151995-02-28 17:14:32 +0000115\end{funcdesc}
116
Fred Drake58d7f691996-10-08 21:52:23 +0000117\begin{funcdesc}{save_end}{}
118Ends buffering character data and returns all data saved since the
Fred Drake526467c1998-02-10 21:42:27 +0000119preceeding call to \method{save_bgn()}. If the \code{nofill} flag is
120false, whitespace is collapsed to single spaces. A call to this
121method without a preceeding call to \method{save_bgn()} will raise a
122\exception{TypeError} exception.
Guido van Rossum86751151995-02-28 17:14:32 +0000123\end{funcdesc}