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