sgmllib -- Python library reference
Next: rfc822
Prev: htmllib
Up: Internet and WWW
Top: Top
11.9. Standard Module sgmllib
This module defines a class SGMLParser
which serves as the
basis for parsing text files formatted in SGML (Standard Generalized
Mark-up Language). In fact, it does not provide a full SGML parser
--- it only parses SGML insofar as it is used by HTML, and the module only
exists as a basis for the htmllib
module.
In particular, the parser is hardcoded to recognize the following
elements:
- Opening and closing tags of the form
``
<tag attr="value" ...>
'' and
``</tag>
'', respectively.
- Character references of the form ``
&#name;
''.
- Entity references of the form ``
&name;
''.
- SGML comments of the form ``
<!--text>
''.
The SGMLParser
class must be instantiated without arguments.
It has the following interface methods:
- reset () -- function of module sgmllib
-
Reset the instance. Loses all unprocessed data. This is called
implicitly at instantiation time.
- setnomoretags () -- function of module sgmllib
-
Stop processing tags. Treat all following input as literal input
(CDATA). (This is only provided so the HTML tag
<PLAINTEXT>
can be implemented.)
- setliteral () -- function of module sgmllib
-
Enter literal mode (CDATA mode).
- feed (data) -- function of module sgmllib
-
Feed some text to the parser. It is processed insofar as it consists
of complete elements; incomplete data is buffered until more data is
fed or
close()
is called.
- close () -- function of module sgmllib
-
Force processing of all buffered data as if it were followed by an
end-of-file mark. This method may be redefined by a derived class to
define additional processing at the end of the input, but the
redefined version should always call
SGMLParser.close()
.
- handle_charref (ref) -- function of module sgmllib
-
This method is called to process a character reference of the form
``
&#ref;
'' where ref is a decimal number in the
range 0-255. It translates the character to ASCII and calls the
method handle_data()
with the character as argument. If
ref is invalid or out of range, the method
unknown_charref(ref)
is called instead.
- handle_entityref (ref) -- function of module sgmllib
-
This method is called to process an entity reference of the form
``
&ref;
'' where ref is an alphabetic entity
reference. It looks for ref in the instance (or class)
variable entitydefs
which should give the entity's translation.
If a translation is found, it calls the method handle_data()
with the translation; otherwise, it calls the method
unknown_entityref(ref)
.
- handle_data (data) -- function of module sgmllib
-
This method is called to process arbitrary data. It is intended to be
overridden by a derived class; the base class implementation does
nothing.
- unknown_starttag (tag, attributes) -- function of module sgmllib
-
This method is called to process an unknown start tag. It is intended
to be overridden by a derived class; the base class implementation
does nothing. The attributes argument is a list of
(name, value) pairs containing the attributes found inside
the tag's
<>
brackets. The name has been translated to
lower case and double quotes and backslashes in the value have
been interpreted. For instance, for the tag
<A HREF="http://www.cwi.nl/">
, this method would be
called as unknown_starttag('a', [('href', 'http://www.cwi.nl/')])
.
- unknown_endtag (tag) -- function of module sgmllib
-
This method is called to process an unknown end tag. It is intended
to be overridden by a derived class; the base class implementation
does nothing.
- unknown_charref (ref) -- function of module sgmllib
-
This method is called to process an unknown character reference. It
is intended to be overridden by a derived class; the base class
implementation does nothing.
- unknown_entityref (ref) -- function of module sgmllib
-
This method is called to process an unknown entity reference. It is
intended to be overridden by a derived class; the base class
implementation does nothing.
Apart from overriding or extending the methods listed above, derived
classes may also define methods of the following form to define
processing of specific tags. Tag names in the input stream are case
independent; the tag occurring in method names must be in lower
case:
- start_tag (attributes) -- function of module sgmllib
-
This method is called to process an opening tag tag. It has
preference over
do_tag()
. The attributes argument
has the same meaning as described for unknown_tag()
above.
- do_tag (attributes) -- function of module sgmllib
-
This method is called to process an opening tag tag that does
not come with a matching closing tag. The attributes argument
has the same meaning as described for
unknown_tag()
above.
- end_tag () -- function of module sgmllib
-
This method is called to process a closing tag tag.
Note that the parser maintains a stack of opening tags for which no
matching closing tag has been found yet. Only tags processed by
start_tag()
are pushed on this stack. Definition of a
end_tag()
method is optional for these tags. For tags
processed by do_tag()
or by unknown_tag()
, no
end_tag()
method must be defined.
Next: rfc822
Prev: htmllib
Up: Internet and WWW
Top: Top