Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/libhtmlparser.tex @ 39940:dfd809dcf680 2.5
Bug #1446043: correctly raise a LookupError if an encoding name given
to encodings.search_function() contains a dot.
(backport from rev. 52075)
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sat, 30 Sep 2006 11:22:35 +0000 |
| parents | 6e0b053c6cf7 |
| children | 963c0e442832 |
| rev | line source |
|---|---|
|
18005
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
1 \section{\module{HTMLParser} --- |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
2 Simple HTML and XHTML parser} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
3 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
4 \declaremodule{standard}{HTMLParser} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
5 \modulesynopsis{A simple parser that can handle HTML and XHTML.} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
6 |
|
33438
a243296786ce
document the exceptions raised by sgmllib, htmllib, and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
31277
diff
changeset
|
7 \versionadded{2.2} |
|
a243296786ce
document the exceptions raised by sgmllib, htmllib, and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
31277
diff
changeset
|
8 |
|
18005
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
9 This module defines a class \class{HTMLParser} which serves as the |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
10 basis for parsing text files formatted in HTML\index{HTML} (HyperText |
|
18396
7de5c376232c
Added more information on the differences between the htmllib and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
18005
diff
changeset
|
11 Mark-up Language) and XHTML.\index{XHTML} Unlike the parser in |
|
7de5c376232c
Added more information on the differences between the htmllib and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
18005
diff
changeset
|
12 \refmodule{htmllib}, this parser is not based on the SGML parser in |
|
7de5c376232c
Added more information on the differences between the htmllib and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
18005
diff
changeset
|
13 \refmodule{sgmllib}. |
|
18005
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
14 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
15 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 \begin{classdesc}{HTMLParser}{} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
17 The \class{HTMLParser} class is instantiated without arguments. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
18 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
19 An HTMLParser instance is fed HTML data and calls handler functions |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
20 when tags begin and end. The \class{HTMLParser} class is meant to be |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
21 overridden by the user to provide a desired behavior. |
|
18396
7de5c376232c
Added more information on the differences between the htmllib and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
18005
diff
changeset
|
22 |
|
7de5c376232c
Added more information on the differences between the htmllib and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
18005
diff
changeset
|
23 Unlike the parser in \refmodule{htmllib}, this parser does not check |
|
7de5c376232c
Added more information on the differences between the htmllib and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
18005
diff
changeset
|
24 that end tags match start tags or call the end-tag handler for |
|
7de5c376232c
Added more information on the differences between the htmllib and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
18005
diff
changeset
|
25 elements which are closed implicitly by closing an outer element. |
|
18005
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
26 \end{classdesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
27 |
|
33438
a243296786ce
document the exceptions raised by sgmllib, htmllib, and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
31277
diff
changeset
|
28 An exception is defined as well: |
|
a243296786ce
document the exceptions raised by sgmllib, htmllib, and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
31277
diff
changeset
|
29 |
|
a243296786ce
document the exceptions raised by sgmllib, htmllib, and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
31277
diff
changeset
|
30 \begin{excdesc}{HTMLParseError} |
|
a243296786ce
document the exceptions raised by sgmllib, htmllib, and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
31277
diff
changeset
|
31 Exception raised by the \class{HTMLParser} class when it encounters an |
|
a243296786ce
document the exceptions raised by sgmllib, htmllib, and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
31277
diff
changeset
|
32 error while parsing. This exception provides three attributes: |
|
a243296786ce
document the exceptions raised by sgmllib, htmllib, and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
31277
diff
changeset
|
33 \member{msg} is a brief message explaining the error, \member{lineno} |
|
a243296786ce
document the exceptions raised by sgmllib, htmllib, and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
31277
diff
changeset
|
34 is the number of the line on which the broken construct was detected, |
|
a243296786ce
document the exceptions raised by sgmllib, htmllib, and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
31277
diff
changeset
|
35 and \member{offset} is the number of characters into the line at which |
|
a243296786ce
document the exceptions raised by sgmllib, htmllib, and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
31277
diff
changeset
|
36 the construct starts. |
|
a243296786ce
document the exceptions raised by sgmllib, htmllib, and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
31277
diff
changeset
|
37 \end{excdesc} |
|
a243296786ce
document the exceptions raised by sgmllib, htmllib, and HTMLParser
Fred Drake <fdrake@acm.org>
parents:
31277
diff
changeset
|
38 |
|
18005
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
39 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
40 \class{HTMLParser} instances have the following methods: |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
41 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
42 \begin{methoddesc}{reset}{} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
43 Reset the instance. Loses all unprocessed data. This is called |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
44 implicitly at instantiation time. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
45 \end{methoddesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
46 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
47 \begin{methoddesc}{feed}{data} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
48 Feed some text to the parser. It is processed insofar as it consists |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
49 of complete elements; incomplete data is buffered until more data is |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
50 fed or \method{close()} is called. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
51 \end{methoddesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
52 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
53 \begin{methoddesc}{close}{} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
54 Force processing of all buffered data as if it were followed by an |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
55 end-of-file mark. This method may be redefined by a derived class to |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
56 define additional processing at the end of the input, but the |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 redefined version should always call the \class{HTMLParser} base class |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
58 method \method{close()}. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
59 \end{methoddesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
60 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
61 \begin{methoddesc}{getpos}{} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
62 Return current line number and offset. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
63 \end{methoddesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
64 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 \begin{methoddesc}{get_starttag_text}{} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
66 Return the text of the most recently opened start tag. This should |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
67 not normally be needed for structured processing, but may be useful in |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
68 dealing with HTML ``as deployed'' or for re-generating input with |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 minimal changes (whitespace between attributes can be preserved, |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
70 etc.). |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
71 \end{methoddesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
72 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
73 \begin{methoddesc}{handle_starttag}{tag, attrs} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
74 This method is called to handle the start of a tag. It is intended to |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
75 be overridden by a derived class; the base class implementation does |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
76 nothing. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
77 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
78 The \var{tag} argument is the name of the tag converted to |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
79 lower case. The \var{attrs} argument is a list of \code{(\var{name}, |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
80 \var{value})} pairs containing the attributes found inside the tag's |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
81 \code{<>} brackets. The \var{name} will be translated to lower case |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
82 and double quotes and backslashes in the \var{value} have been |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
83 interpreted. For instance, for the tag \code{<A |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
84 HREF="http://www.cwi.nl/">}, this method would be called as |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
85 \samp{handle_starttag('a', [('href', 'http://www.cwi.nl/')])}. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
86 \end{methoddesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
87 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
88 \begin{methoddesc}{handle_startendtag}{tag, attrs} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
89 Similar to \method{handle_starttag()}, but called when the parser |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
90 encounters an XHTML-style empty tag (\code{<a .../>}). This method |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
91 may be overridden by subclasses which require this particular lexical |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
92 information; the default implementation simple calls |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
93 \method{handle_starttag()} and \method{handle_endtag()}. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
94 \end{methoddesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
95 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
96 \begin{methoddesc}{handle_endtag}{tag} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
97 This method is called to handle the end tag of an element. It is |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
98 intended to be overridden by a derived class; the base class |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
99 implementation does nothing. The \var{tag} argument is the name of |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
100 the tag converted to lower case. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
101 \end{methoddesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
102 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
103 \begin{methoddesc}{handle_data}{data} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
104 This method is called to process arbitrary data. It is intended to be |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
105 overridden by a derived class; the base class implementation does |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
106 nothing. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
107 \end{methoddesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
108 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
109 \begin{methoddesc}{handle_charref}{name} This method is called to |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
110 process a character reference of the form \samp{\&\#\var{ref};}. It |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
111 is intended to be overridden by a derived class; the base class |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
112 implementation does nothing. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
113 \end{methoddesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
114 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
115 \begin{methoddesc}{handle_entityref}{name} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
116 This method is called to process a general entity reference of the |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
117 form \samp{\&\var{name};} where \var{name} is an general entity |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
118 reference. It is intended to be overridden by a derived class; the |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
119 base class implementation does nothing. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
120 \end{methoddesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
121 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
122 \begin{methoddesc}{handle_comment}{data} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
123 This method is called when a comment is encountered. The |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
124 \var{comment} argument is a string containing the text between the |
|
31277
6c3c3b771abc
- remove crufty markup that's no longer needed to make the
Fred Drake <fdrake@acm.org>
parents:
31150
diff
changeset
|
125 \samp{--} and \samp{--} delimiters, but not the delimiters |
|
6c3c3b771abc
- remove crufty markup that's no longer needed to make the
Fred Drake <fdrake@acm.org>
parents:
31150
diff
changeset
|
126 themselves. For example, the comment \samp{<!--text-->} will |
|
31150
94c1bd7a55ae
Fix double hyphen markup.
Raymond Hettinger <python@rcn.com>
parents:
30206
diff
changeset
|
127 cause this method to be called with the argument \code{'text'}. It is |
|
18005
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
128 intended to be overridden by a derived class; the base class |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
129 implementation does nothing. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
130 \end{methoddesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
131 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
132 \begin{methoddesc}{handle_decl}{decl} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
133 Method called when an SGML declaration is read by the parser. The |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
134 \var{decl} parameter will be the entire contents of the declaration |
|
38091
6e0b053c6cf7
avoid ugly markup based on the unfortunate conversions of ">>" and "<<" to
Fred Drake <fdrake@acm.org>
parents:
33438
diff
changeset
|
135 inside the \code{<!}...\code{>} markup. It is intended to be overridden |
|
18005
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
136 by a derived class; the base class implementation does nothing. |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
137 \end{methoddesc} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
138 |
|
28662
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
139 \begin{methoddesc}{handle_pi}{data} |
|
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
140 Method called when a processing instruction is encountered. The |
|
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
141 \var{data} parameter will contain the entire processing instruction. |
|
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
142 For example, for the processing instruction \code{<?proc color='red'>}, |
|
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
143 this method would be called as \code{handle_pi("proc color='red'")}. It |
|
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
144 is intended to be overridden by a derived class; the base class |
|
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
145 implementation does nothing. |
|
18005
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
146 |
|
28662
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
147 \note{The \class{HTMLParser} class uses the SGML syntactic rules for |
|
31277
6c3c3b771abc
- remove crufty markup that's no longer needed to make the
Fred Drake <fdrake@acm.org>
parents:
31150
diff
changeset
|
148 processing instructions. An XHTML processing instruction using the |
|
28662
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
149 trailing \character{?} will cause the \character{?} to be included in |
|
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
150 \var{data}.} |
|
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
151 \end{methoddesc} |
|
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
152 |
|
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
153 |
|
ee6df2c668ea
Added documentation for the handle_pi() method, based on SF patch #662464.
Fred Drake <fdrake@acm.org>
parents:
18396
diff
changeset
|
154 \subsection{Example HTML Parser Application \label{htmlparser-example}} |
|
18005
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
155 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
156 As a basic example, below is a very basic HTML parser that uses the |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
157 \class{HTMLParser} class to print out tags as they are encountered: |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
158 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
159 \begin{verbatim} |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
160 from HTMLParser import HTMLParser |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
161 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
162 class MyHTMLParser(HTMLParser): |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
163 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
164 def handle_starttag(self, tag, attrs): |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
165 print "Encountered the beginning of a %s tag" % tag |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
166 |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
167 def handle_endtag(self, tag): |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
168 print "Encountered the end of a %s tag" % tag |
|
baf2137e9676
Michel Pelletier <michel@digicool.com>:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
169 \end{verbatim} |
