Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{sgmllib}} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame^] | 2 | \declaremodule{standard}{sgmllib} |
| 3 | |
| 4 | \modulesynopsis{Only as much of an SGML parser as needed to parse HTML.} |
| 5 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 6 | \index{SGML} |
| 7 | |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 8 | This module defines a class \class{SGMLParser} which serves as the |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 9 | basis for parsing text files formatted in SGML (Standard Generalized |
| 10 | Mark-up Language). In fact, it does not provide a full SGML parser |
Fred Drake | 8f92595 | 1996-10-09 16:13:22 +0000 | [diff] [blame] | 11 | --- it only parses SGML insofar as it is used by HTML, and the module |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 12 | only exists as a base for the \module{htmllib}\refstmodindex{htmllib} |
| 13 | module. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 14 | |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 15 | |
| 16 | \begin{classdesc}{SGMLParser}{} |
| 17 | The \class{SGMLParser} class is instantiated without arguments. |
| 18 | The parser is hardcoded to recognize the following |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 19 | constructs: |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 20 | |
| 21 | \begin{itemize} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 22 | \item |
| 23 | Opening and closing tags of the form |
Fred Drake | b441eb8 | 1998-02-13 14:37:12 +0000 | [diff] [blame] | 24 | \samp{<\var{tag} \var{attr}="\var{value}" ...>} and |
| 25 | \samp{</\var{tag}>}, respectively. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 26 | |
| 27 | \item |
Fred Drake | b441eb8 | 1998-02-13 14:37:12 +0000 | [diff] [blame] | 28 | Numeric character references of the form \samp{\&\#\var{name};}. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 29 | |
| 30 | \item |
Fred Drake | b441eb8 | 1998-02-13 14:37:12 +0000 | [diff] [blame] | 31 | Entity references of the form \samp{\&\var{name};}. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 32 | |
| 33 | \item |
Fred Drake | b441eb8 | 1998-02-13 14:37:12 +0000 | [diff] [blame] | 34 | SGML comments of the form \samp{<!--\var{text}-->}. Note that |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 35 | spaces, tabs, and newlines are allowed between the trailing |
Fred Drake | b441eb8 | 1998-02-13 14:37:12 +0000 | [diff] [blame] | 36 | \samp{>} and the immediately preceeding \samp{--}. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 37 | |
| 38 | \end{itemize} |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 39 | \end{classdesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 40 | |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 41 | \class{SGMLParser} instances have the following interface methods: |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 42 | |
Fred Drake | 8f92595 | 1996-10-09 16:13:22 +0000 | [diff] [blame] | 43 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 44 | \begin{methoddesc}{reset}{} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 45 | Reset the instance. Loses all unprocessed data. This is called |
| 46 | implicitly at instantiation time. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 47 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 48 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 49 | \begin{methoddesc}{setnomoretags}{} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 50 | Stop processing tags. Treat all following input as literal input |
| 51 | (CDATA). (This is only provided so the HTML tag \code{<PLAINTEXT>} |
| 52 | can be implemented.) |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 53 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 54 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 55 | \begin{methoddesc}{setliteral}{} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 56 | Enter literal mode (CDATA mode). |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 57 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 58 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 59 | \begin{methoddesc}{feed}{data} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 60 | Feed some text to the parser. It is processed insofar as it consists |
| 61 | of complete elements; incomplete data is buffered until more data is |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 62 | fed or \method{close()} is called. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 63 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 64 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 65 | \begin{methoddesc}{close}{} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 66 | Force processing of all buffered data as if it were followed by an |
| 67 | end-of-file mark. This method may be redefined by a derived class to |
| 68 | define additional processing at the end of the input, but the |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 69 | redefined version should always call \method{close()}. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 70 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 71 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 72 | \begin{methoddesc}{handle_starttag}{tag, method, attributes} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 73 | This method is called to handle start tags for which either a |
| 74 | \code{start_\var{tag}()} or \code{do_\var{tag}()} method has been |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 75 | defined. The \var{tag} argument is the name of the tag converted to |
| 76 | lower case, and the \var{method} argument is the bound method which |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 77 | should be used to support semantic interpretation of the start tag. |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 78 | The \var{attributes} argument is a list of \code{(\var{name}, \var{value})} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 79 | pairs containing the attributes found inside the tag's \code{<>} |
| 80 | brackets. The \var{name} has been translated to lower case and double |
| 81 | quotes and backslashes in the \var{value} have been interpreted. For |
| 82 | instance, for the tag \code{<A HREF="http://www.cwi.nl/">}, this |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 83 | method would be called as \samp{unknown_starttag('a', [('href', |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 84 | 'http://www.cwi.nl/')])}. The base implementation simply calls |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 85 | \var{method} with \var{attributes} as the only argument. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 86 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 87 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 88 | \begin{methoddesc}{handle_endtag}{tag, method} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 89 | This method is called to handle endtags for which an |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 90 | \code{end_\var{tag}()} method has been defined. The \var{tag} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 91 | argument is the name of the tag converted to lower case, and the |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 92 | \var{method} argument is the bound method which should be used to |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 93 | support semantic interpretation of the end tag. If no |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 94 | \code{end_\var{tag}()} method is defined for the closing element, |
| 95 | this handler is not called. The base implementation simply calls |
| 96 | \var{method}. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 97 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 98 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 99 | \begin{methoddesc}{handle_data}{data} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 100 | This method is called to process arbitrary data. It is intended to be |
| 101 | overridden by a derived class; the base class implementation does |
| 102 | nothing. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 103 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 104 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 105 | \begin{methoddesc}{handle_charref}{ref} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 106 | This method is called to process a character reference of the form |
Fred Drake | b441eb8 | 1998-02-13 14:37:12 +0000 | [diff] [blame] | 107 | \samp{\&\#\var{ref};}. In the base implementation, \var{ref} must |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 108 | be a decimal number in the |
| 109 | range 0-255. It translates the character to \ASCII{} and calls the |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 110 | method \method{handle_data()} with the character as argument. If |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 111 | \var{ref} is invalid or out of range, the method |
| 112 | \code{unknown_charref(\var{ref})} is called to handle the error. A |
| 113 | subclass must override this method to provide support for named |
| 114 | character entities. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 115 | \end{methoddesc} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 116 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 117 | \begin{methoddesc}{handle_entityref}{ref} |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 118 | This method is called to process a general entity reference of the |
| 119 | form \samp{\&\var{ref};} where \var{ref} is an general entity |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 120 | reference. It looks for \var{ref} in the instance (or class) |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 121 | variable \member{entitydefs} which should be a mapping from entity |
| 122 | names to corresponding translations. |
| 123 | If a translation is found, it calls the method \method{handle_data()} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 124 | with the translation; otherwise, it calls the method |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 125 | \code{unknown_entityref(\var{ref})}. The default \member{entitydefs} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 126 | defines translations for \code{\&}, \code{\&apos}, \code{\>}, |
| 127 | \code{\<}, and \code{\"}. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 128 | \end{methoddesc} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 129 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 130 | \begin{methoddesc}{handle_comment}{comment} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 131 | This method is called when a comment is encountered. The |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 132 | \var{comment} argument is a string containing the text between the |
Fred Drake | b441eb8 | 1998-02-13 14:37:12 +0000 | [diff] [blame] | 133 | \samp{<!--} and \samp{-->} delimiters, but not the delimiters |
| 134 | themselves. For example, the comment \samp{<!--text-->} will |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 135 | cause this method to be called with the argument \code{'text'}. The |
| 136 | default method does nothing. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 137 | \end{methoddesc} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 138 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 139 | \begin{methoddesc}{report_unbalanced}{tag} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 140 | This method is called when an end tag is found which does not |
| 141 | correspond to any open element. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 142 | \end{methoddesc} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 143 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 144 | \begin{methoddesc}{unknown_starttag}{tag, attributes} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 145 | This method is called to process an unknown start tag. It is intended |
| 146 | to be overridden by a derived class; the base class implementation |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 147 | does nothing. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 148 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 149 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 150 | \begin{methoddesc}{unknown_endtag}{tag} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 151 | This method is called to process an unknown end tag. It is intended |
| 152 | to be overridden by a derived class; the base class implementation |
| 153 | does nothing. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 154 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 155 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 156 | \begin{methoddesc}{unknown_charref}{ref} |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 157 | This method is called to process unresolvable numeric character |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 158 | references. Refer to \method{handle_charref()} to determine what is |
| 159 | handled by default. It is intended to be overridden by a derived |
| 160 | class; the base class implementation does nothing. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 161 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 162 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 163 | \begin{methoddesc}{unknown_entityref}{ref} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 164 | This method is called to process an unknown entity reference. It is |
| 165 | intended to be overridden by a derived class; the base class |
| 166 | implementation does nothing. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 167 | \end{methoddesc} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 168 | |
| 169 | Apart from overriding or extending the methods listed above, derived |
| 170 | classes may also define methods of the following form to define |
| 171 | processing of specific tags. Tag names in the input stream are case |
| 172 | independent; the \var{tag} occurring in method names must be in lower |
| 173 | case: |
| 174 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 175 | \begin{methoddescni}{start_\var{tag}}{attributes} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 176 | This method is called to process an opening tag \var{tag}. It has |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 177 | preference over \code{do_\var{tag}()}. The \var{attributes} |
| 178 | argument has the same meaning as described for |
| 179 | \method{handle_starttag()} above. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 180 | \end{methoddescni} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 181 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 182 | \begin{methoddescni}{do_\var{tag}}{attributes} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 183 | This method is called to process an opening tag \var{tag} that does |
| 184 | not come with a matching closing tag. The \var{attributes} argument |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 185 | has the same meaning as described for \method{handle_starttag()} above. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 186 | \end{methoddescni} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 187 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 188 | \begin{methoddescni}{end_\var{tag}}{} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 189 | This method is called to process a closing tag \var{tag}. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 190 | \end{methoddescni} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 191 | |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 192 | Note that the parser maintains a stack of open elements for which no |
| 193 | end tag has been found yet. Only tags processed by |
| 194 | \code{start_\var{tag}()} are pushed on this stack. Definition of an |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 195 | \code{end_\var{tag}()} method is optional for these tags. For tags |
Fred Drake | 2dde74c | 1998-03-12 14:42:23 +0000 | [diff] [blame] | 196 | processed by \code{do_\var{tag}()} or by \method{unknown_tag()}, no |
Fred Drake | 42439ad | 1996-10-08 21:51:49 +0000 | [diff] [blame] | 197 | \code{end_\var{tag}()} method must be defined; if defined, it will not |
| 198 | be used. If both \code{start_\var{tag}()} and \code{do_\var{tag}()} |
| 199 | methods exist for a tag, the \code{start_\var{tag}()} method takes |
| 200 | precedence. |