1 \section{\module{htmllib
} ---
2 A parser for HTML documents
}
4 \declaremodule{standard
}{htmllib
}
5 \modulesynopsis{A parser for HTML documents.
}
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
26 \withsubitem{(in module sgmllib)
}{\ttindex{SGMLParser
}}
28 The following is a summary of the interface defined by
29 \class{sgmllib.SGMLParser
}:
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:
44 parser.feed(open('myfile.html').read())
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.
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.
73 \seemodule{htmlentitydefs
}{Definition of replacement text for HTML
75 \seemodule{sgmllib
}{Base class for
\class{HTMLParser
}.
}
79 \subsection{HTMLParser Objects
\label{html-parser-objects
}}
81 In addition to tag methods, the
\class{HTMLParser
} class provides some
82 additional methods and instance variables for use within tag methods.
84 \begin{memberdesc
}{formatter
}
85 This is the formatter instance associated with the parser.
88 \begin{memberdesc
}{nofill
}
89 Boolean flag which should be true when whitespace should not be
90 collapsed, or false when it should be. In general, this should only
91 be true when character data is to be treated as ``preformatted'' text,
92 as within a
\code{<PRE>
} element. The default value is false. This
93 affects the operation of
\method{handle_data()
} and
\method{save_end()
}.
97 \begin{methoddesc
}{anchor_bgn
}{href, name, type
}
98 This method is called at the start of an anchor region. The arguments
99 correspond to the attributes of the
\code{<A>
} tag with the same
100 names. The default implementation maintains a list of hyperlinks
101 (defined by the
\code{HREF
} attribute for
\code{<A>
} tags) within the
102 document. The list of hyperlinks is available as the data attribute
106 \begin{methoddesc
}{anchor_end
}{}
107 This method is called at the end of an anchor region. The default
108 implementation adds a textual footnote marker using an index into the
109 list of hyperlinks created by
\method{anchor_bgn()
}.
112 \begin{methoddesc
}{handle_image
}{source, alt
\optional{, ismap
\optional{, align
\optional{, width
\optional{, height
}}}}}
113 This method is called to handle images. The default implementation
114 simply passes the
\var{alt
} value to the
\method{handle_data()
}
118 \begin{methoddesc
}{save_bgn
}{}
119 Begins saving character data in a buffer instead of sending it to the
120 formatter object. Retrieve the stored data via
\method{save_end()
}.
121 Use of the
\method{save_bgn()
} /
\method{save_end()
} pair may not be
125 \begin{methoddesc
}{save_end
}{}
126 Ends buffering character data and returns all data saved since the
127 preceding call to
\method{save_bgn()
}. If the
\member{nofill
} flag is
128 false, whitespace is collapsed to single spaces. A call to this
129 method without a preceding call to
\method{save_bgn()
} will raise a
130 \exception{TypeError
} exception.
135 \section{\module{htmlentitydefs
} ---
136 Definitions of HTML general entities
}
138 \declaremodule{standard
}{htmlentitydefs
}
139 \modulesynopsis{Definitions of HTML general entities.
}
140 \sectionauthor{Fred L. Drake, Jr.
}{fdrake@acm.org
}
142 This module defines a single dictionary,
\code{entitydefs
}, which is
143 used by the
\refmodule{htmllib
} module to provide the
144 \member{entitydefs
} member of the
\class{HTMLParser
} class. The
145 definition provided here contains all the entities defined by HTML
2.0
146 that can be handled using simple textual substitution in the Latin-
1
147 character set (ISO-
8859-
1).
150 \begin{datadesc
}{entitydefs
}
151 A dictionary mapping HTML
2.0 entity definitions to their
152 replacement text in ISO Latin-
1.