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