Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """A parser for SGML, using the derived class as a static DTD.""" |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 2 | |
| 3 | # XXX This only supports those SGML features used by HTML. |
| 4 | |
| 5 | # XXX There should be a way to distinguish between PCDATA (parsed |
| 6 | # character data -- the normal case), RCDATA (replaceable character |
| 7 | # data -- only char and entity references and end tags are special) |
Fred Drake | fb38c76 | 2001-07-16 18:30:35 +0000 | [diff] [blame] | 8 | # and CDATA (character data -- only end tags are special). RCDATA is |
| 9 | # not supported at all. |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 10 | |
| 11 | |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 12 | import markupbase |
Guido van Rossum | 1fef181 | 1997-10-23 19:09:21 +0000 | [diff] [blame] | 13 | import re |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 14 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 15 | __all__ = ["SGMLParser"] |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 16 | |
| 17 | # Regular expressions used for parsing |
| 18 | |
Guido van Rossum | 1fef181 | 1997-10-23 19:09:21 +0000 | [diff] [blame] | 19 | interesting = re.compile('[&<]') |
| 20 | incomplete = re.compile('&([a-zA-Z][a-zA-Z0-9]*|#[0-9]*)?|' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 21 | '<([a-zA-Z][^<>]*|' |
| 22 | '/([a-zA-Z][^<>]*)?|' |
| 23 | '![^<>]*)?') |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 1ad0071 | 1998-05-28 22:48:53 +0000 | [diff] [blame] | 25 | entityref = re.compile('&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]') |
Guido van Rossum | 1fef181 | 1997-10-23 19:09:21 +0000 | [diff] [blame] | 26 | charref = re.compile('&#([0-9]+)[^0-9]') |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 1fef181 | 1997-10-23 19:09:21 +0000 | [diff] [blame] | 28 | starttagopen = re.compile('<[>a-zA-Z]') |
Guido van Rossum | 5fdf852 | 1998-08-24 20:59:13 +0000 | [diff] [blame] | 29 | shorttagopen = re.compile('<[a-zA-Z][-.a-zA-Z0-9]*/') |
| 30 | shorttag = re.compile('<([a-zA-Z][-.a-zA-Z0-9]*)/([^/]*)/') |
Guido van Rossum | 1ad0071 | 1998-05-28 22:48:53 +0000 | [diff] [blame] | 31 | piclose = re.compile('>') |
Guido van Rossum | 1fef181 | 1997-10-23 19:09:21 +0000 | [diff] [blame] | 32 | endbracket = re.compile('[<>]') |
Fred Drake | dc19163 | 2001-07-05 18:21:57 +0000 | [diff] [blame] | 33 | tagfind = re.compile('[a-zA-Z][-_.a-zA-Z0-9]*') |
Guido van Rossum | 1fef181 | 1997-10-23 19:09:21 +0000 | [diff] [blame] | 34 | attrfind = re.compile( |
Fred Drake | 8600b47 | 2001-07-14 05:50:33 +0000 | [diff] [blame] | 35 | r'\s*([a-zA-Z_][-:.a-zA-Z_0-9]*)(\s*=\s*' |
Fred Drake | 0834d77 | 2003-03-14 16:21:57 +0000 | [diff] [blame] | 36 | r'(\'[^\']*\'|"[^"]*"|[-a-zA-Z0-9./,:;+*%?!&$\(\)_#=~\'"]*))?') |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 37 | |
Fred Drake | 6695737 | 2001-03-16 20:04:57 +0000 | [diff] [blame] | 38 | |
| 39 | class SGMLParseError(RuntimeError): |
| 40 | """Exception raised for all parse errors.""" |
| 41 | pass |
| 42 | |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 43 | |
| 44 | # SGML parser base class -- find tags and call handler functions. |
| 45 | # Usage: p = SGMLParser(); p.feed(data); ...; p.close(). |
| 46 | # The dtd is defined by deriving a class which defines methods |
| 47 | # with special names to handle tags: start_foo and end_foo to handle |
| 48 | # <foo> and </foo>, respectively, or do_foo to handle <foo> by itself. |
| 49 | # (Tags are converted to lower case for this purpose.) The data |
| 50 | # between tags is passed to the parser by calling self.handle_data() |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 51 | # with some data as argument (the data may be split up in arbitrary |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 52 | # chunks). Entity references are passed by calling |
| 53 | # self.handle_entityref() with the entity reference as argument. |
| 54 | |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 55 | class SGMLParser(markupbase.ParserBase): |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 57 | def __init__(self, verbose=0): |
Fred Drake | 08f8dd6 | 2001-07-19 20:08:04 +0000 | [diff] [blame] | 58 | """Initialize and reset this instance.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 59 | self.verbose = verbose |
| 60 | self.reset() |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 62 | def reset(self): |
Fred Drake | 08f8dd6 | 2001-07-19 20:08:04 +0000 | [diff] [blame] | 63 | """Reset this instance. Loses all unprocessed data.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 64 | self.rawdata = '' |
| 65 | self.stack = [] |
| 66 | self.lasttag = '???' |
| 67 | self.nomoretags = 0 |
| 68 | self.literal = 0 |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 69 | markupbase.ParserBase.reset(self) |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 71 | def setnomoretags(self): |
Fred Drake | 390e9db | 2001-07-19 20:57:23 +0000 | [diff] [blame] | 72 | """Enter literal mode (CDATA) till EOF. |
| 73 | |
| 74 | Intended for derived classes only. |
| 75 | """ |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 76 | self.nomoretags = self.literal = 1 |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 78 | def setliteral(self, *args): |
Fred Drake | 390e9db | 2001-07-19 20:57:23 +0000 | [diff] [blame] | 79 | """Enter literal mode (CDATA). |
| 80 | |
| 81 | Intended for derived classes only. |
| 82 | """ |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 83 | self.literal = 1 |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 85 | def feed(self, data): |
Fred Drake | 390e9db | 2001-07-19 20:57:23 +0000 | [diff] [blame] | 86 | """Feed some data to the parser. |
| 87 | |
| 88 | Call this as often as you want, with as little or as much text |
| 89 | as you want (may include '\n'). (This just saves the text, |
| 90 | all the processing is done by goahead().) |
| 91 | """ |
| 92 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 93 | self.rawdata = self.rawdata + data |
| 94 | self.goahead(0) |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 96 | def close(self): |
Fred Drake | 08f8dd6 | 2001-07-19 20:08:04 +0000 | [diff] [blame] | 97 | """Handle the remaining data.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 98 | self.goahead(1) |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 99 | |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 100 | def error(self, message): |
| 101 | raise SGMLParseError(message) |
| 102 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 103 | # Internal -- handle data as far as reasonable. May leave state |
| 104 | # and data to be processed by a subsequent call. If 'end' is |
| 105 | # true, force handling all data as if followed by EOF marker. |
| 106 | def goahead(self, end): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 107 | rawdata = self.rawdata |
| 108 | i = 0 |
| 109 | n = len(rawdata) |
| 110 | while i < n: |
| 111 | if self.nomoretags: |
| 112 | self.handle_data(rawdata[i:n]) |
| 113 | i = n |
| 114 | break |
| 115 | match = interesting.search(rawdata, i) |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 116 | if match: j = match.start() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 117 | else: j = n |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 118 | if i < j: |
| 119 | self.handle_data(rawdata[i:j]) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 120 | i = j |
| 121 | if i == n: break |
| 122 | if rawdata[i] == '<': |
| 123 | if starttagopen.match(rawdata, i): |
| 124 | if self.literal: |
| 125 | self.handle_data(rawdata[i]) |
| 126 | i = i+1 |
| 127 | continue |
| 128 | k = self.parse_starttag(i) |
| 129 | if k < 0: break |
| 130 | i = k |
| 131 | continue |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 132 | if rawdata.startswith("</", i): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 133 | k = self.parse_endtag(i) |
| 134 | if k < 0: break |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 135 | i = k |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 136 | self.literal = 0 |
| 137 | continue |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 138 | if self.literal: |
| 139 | if n > (i + 1): |
| 140 | self.handle_data("<") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 141 | i = i+1 |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 142 | else: |
| 143 | # incomplete |
| 144 | break |
| 145 | continue |
| 146 | if rawdata.startswith("<!--", i): |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 147 | # Strictly speaking, a comment is --.*-- |
| 148 | # within a declaration tag <!...>. |
| 149 | # This should be removed, |
| 150 | # and comments handled only in parse_declaration. |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 151 | k = self.parse_comment(i) |
| 152 | if k < 0: break |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 153 | i = k |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 154 | continue |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 155 | if rawdata.startswith("<?", i): |
Guido van Rossum | 1ad0071 | 1998-05-28 22:48:53 +0000 | [diff] [blame] | 156 | k = self.parse_pi(i) |
| 157 | if k < 0: break |
| 158 | i = i+k |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 159 | continue |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 160 | if rawdata.startswith("<!", i): |
Fred Drake | 6695737 | 2001-03-16 20:04:57 +0000 | [diff] [blame] | 161 | # This is some sort of declaration; in "HTML as |
| 162 | # deployed," this should only be the document type |
| 163 | # declaration ("<!DOCTYPE html...>"). |
| 164 | k = self.parse_declaration(i) |
| 165 | if k < 0: break |
| 166 | i = k |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 167 | continue |
| 168 | elif rawdata[i] == '&': |
Fred Drake | fb38c76 | 2001-07-16 18:30:35 +0000 | [diff] [blame] | 169 | if self.literal: |
| 170 | self.handle_data(rawdata[i]) |
| 171 | i = i+1 |
| 172 | continue |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 173 | match = charref.match(rawdata, i) |
| 174 | if match: |
| 175 | name = match.group(1) |
| 176 | self.handle_charref(name) |
| 177 | i = match.end(0) |
| 178 | if rawdata[i-1] != ';': i = i-1 |
| 179 | continue |
| 180 | match = entityref.match(rawdata, i) |
| 181 | if match: |
| 182 | name = match.group(1) |
| 183 | self.handle_entityref(name) |
| 184 | i = match.end(0) |
| 185 | if rawdata[i-1] != ';': i = i-1 |
| 186 | continue |
| 187 | else: |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 188 | self.error('neither < nor & ??') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 189 | # We get here only if incomplete matches but |
| 190 | # nothing else |
| 191 | match = incomplete.match(rawdata, i) |
| 192 | if not match: |
| 193 | self.handle_data(rawdata[i]) |
| 194 | i = i+1 |
| 195 | continue |
| 196 | j = match.end(0) |
| 197 | if j == n: |
| 198 | break # Really incomplete |
| 199 | self.handle_data(rawdata[i:j]) |
| 200 | i = j |
| 201 | # end while |
| 202 | if end and i < n: |
| 203 | self.handle_data(rawdata[i:n]) |
| 204 | i = n |
| 205 | self.rawdata = rawdata[i:] |
| 206 | # XXX if end: check for empty stack |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 207 | |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 208 | # Extensions for the DOCTYPE scanner: |
| 209 | _decl_otherchars = '=' |
Fred Drake | 6695737 | 2001-03-16 20:04:57 +0000 | [diff] [blame] | 210 | |
Guido van Rossum | 1ad0071 | 1998-05-28 22:48:53 +0000 | [diff] [blame] | 211 | # Internal -- parse processing instr, return length or -1 if not terminated |
| 212 | def parse_pi(self, i): |
| 213 | rawdata = self.rawdata |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 214 | if rawdata[i:i+2] != '<?': |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 215 | self.error('unexpected call to parse_pi()') |
Guido van Rossum | 1ad0071 | 1998-05-28 22:48:53 +0000 | [diff] [blame] | 216 | match = piclose.search(rawdata, i+2) |
| 217 | if not match: |
| 218 | return -1 |
| 219 | j = match.start(0) |
| 220 | self.handle_pi(rawdata[i+2: j]) |
| 221 | j = match.end(0) |
| 222 | return j-i |
Fred Drake | b46696c | 2000-06-29 18:50:59 +0000 | [diff] [blame] | 223 | |
| 224 | __starttag_text = None |
| 225 | def get_starttag_text(self): |
| 226 | return self.__starttag_text |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 227 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 228 | # Internal -- handle starttag, return length or -1 if not terminated |
| 229 | def parse_starttag(self, i): |
Fred Drake | b46696c | 2000-06-29 18:50:59 +0000 | [diff] [blame] | 230 | self.__starttag_text = None |
| 231 | start_pos = i |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 232 | rawdata = self.rawdata |
| 233 | if shorttagopen.match(rawdata, i): |
| 234 | # SGML shorthand: <tag/data/ == <tag>data</tag> |
| 235 | # XXX Can data contain &... (entity or char refs)? |
| 236 | # XXX Can data contain < or > (tag characters)? |
| 237 | # XXX Can there be whitespace before the first /? |
| 238 | match = shorttag.match(rawdata, i) |
| 239 | if not match: |
| 240 | return -1 |
| 241 | tag, data = match.group(1, 2) |
Fred Drake | b46696c | 2000-06-29 18:50:59 +0000 | [diff] [blame] | 242 | self.__starttag_text = '<%s/' % tag |
Eric S. Raymond | 1b645e8 | 2001-02-09 07:49:30 +0000 | [diff] [blame] | 243 | tag = tag.lower() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 244 | k = match.end(0) |
Fred Drake | b46696c | 2000-06-29 18:50:59 +0000 | [diff] [blame] | 245 | self.finish_shorttag(tag, data) |
| 246 | self.__starttag_text = rawdata[start_pos:match.end(1) + 1] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 247 | return k |
| 248 | # XXX The following should skip matching quotes (' or ") |
| 249 | match = endbracket.search(rawdata, i+1) |
| 250 | if not match: |
| 251 | return -1 |
| 252 | j = match.start(0) |
| 253 | # Now parse the data between i+1 and j into a tag and attrs |
| 254 | attrs = [] |
| 255 | if rawdata[i:i+2] == '<>': |
| 256 | # SGML shorthand: <> == <last open tag seen> |
| 257 | k = j |
| 258 | tag = self.lasttag |
| 259 | else: |
| 260 | match = tagfind.match(rawdata, i+1) |
| 261 | if not match: |
Fred Drake | a3bae33 | 2001-09-24 20:15:51 +0000 | [diff] [blame] | 262 | self.error('unexpected call to parse_starttag') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 263 | k = match.end(0) |
Eric S. Raymond | 1b645e8 | 2001-02-09 07:49:30 +0000 | [diff] [blame] | 264 | tag = rawdata[i+1:k].lower() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 265 | self.lasttag = tag |
| 266 | while k < j: |
| 267 | match = attrfind.match(rawdata, k) |
| 268 | if not match: break |
| 269 | attrname, rest, attrvalue = match.group(1, 2, 3) |
| 270 | if not rest: |
| 271 | attrvalue = attrname |
| 272 | elif attrvalue[:1] == '\'' == attrvalue[-1:] or \ |
| 273 | attrvalue[:1] == '"' == attrvalue[-1:]: |
| 274 | attrvalue = attrvalue[1:-1] |
Eric S. Raymond | 1b645e8 | 2001-02-09 07:49:30 +0000 | [diff] [blame] | 275 | attrs.append((attrname.lower(), attrvalue)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 276 | k = match.end(0) |
| 277 | if rawdata[j] == '>': |
| 278 | j = j+1 |
Fred Drake | b46696c | 2000-06-29 18:50:59 +0000 | [diff] [blame] | 279 | self.__starttag_text = rawdata[start_pos:j] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 280 | self.finish_starttag(tag, attrs) |
| 281 | return j |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 282 | |
| 283 | # Internal -- parse endtag |
| 284 | def parse_endtag(self, i): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 285 | rawdata = self.rawdata |
| 286 | match = endbracket.search(rawdata, i+1) |
| 287 | if not match: |
| 288 | return -1 |
| 289 | j = match.start(0) |
Eric S. Raymond | 1b645e8 | 2001-02-09 07:49:30 +0000 | [diff] [blame] | 290 | tag = rawdata[i+2:j].strip().lower() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 291 | if rawdata[j] == '>': |
| 292 | j = j+1 |
| 293 | self.finish_endtag(tag) |
| 294 | return j |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 295 | |
| 296 | # Internal -- finish parsing of <tag/data/ (same as <tag>data</tag>) |
| 297 | def finish_shorttag(self, tag, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 298 | self.finish_starttag(tag, []) |
| 299 | self.handle_data(data) |
| 300 | self.finish_endtag(tag) |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 301 | |
| 302 | # Internal -- finish processing of start tag |
| 303 | # Return -1 for unknown tag, 0 for open-only tag, 1 for balanced tag |
| 304 | def finish_starttag(self, tag, attrs): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 305 | try: |
| 306 | method = getattr(self, 'start_' + tag) |
| 307 | except AttributeError: |
| 308 | try: |
| 309 | method = getattr(self, 'do_' + tag) |
| 310 | except AttributeError: |
| 311 | self.unknown_starttag(tag, attrs) |
| 312 | return -1 |
| 313 | else: |
| 314 | self.handle_starttag(tag, method, attrs) |
| 315 | return 0 |
| 316 | else: |
| 317 | self.stack.append(tag) |
| 318 | self.handle_starttag(tag, method, attrs) |
| 319 | return 1 |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 320 | |
| 321 | # Internal -- finish processing of end tag |
| 322 | def finish_endtag(self, tag): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 323 | if not tag: |
| 324 | found = len(self.stack) - 1 |
| 325 | if found < 0: |
| 326 | self.unknown_endtag(tag) |
| 327 | return |
| 328 | else: |
| 329 | if tag not in self.stack: |
| 330 | try: |
| 331 | method = getattr(self, 'end_' + tag) |
| 332 | except AttributeError: |
| 333 | self.unknown_endtag(tag) |
Guido van Rossum | b84ef9b | 1998-07-07 22:46:11 +0000 | [diff] [blame] | 334 | else: |
| 335 | self.report_unbalanced(tag) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 336 | return |
| 337 | found = len(self.stack) |
| 338 | for i in range(found): |
| 339 | if self.stack[i] == tag: found = i |
| 340 | while len(self.stack) > found: |
| 341 | tag = self.stack[-1] |
| 342 | try: |
| 343 | method = getattr(self, 'end_' + tag) |
| 344 | except AttributeError: |
| 345 | method = None |
| 346 | if method: |
| 347 | self.handle_endtag(tag, method) |
| 348 | else: |
| 349 | self.unknown_endtag(tag) |
| 350 | del self.stack[-1] |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 351 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 352 | # Overridable -- handle start tag |
| 353 | def handle_starttag(self, tag, method, attrs): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 354 | method(attrs) |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 355 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 356 | # Overridable -- handle end tag |
| 357 | def handle_endtag(self, tag, method): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 358 | method() |
Guido van Rossum | efe5ac4 | 1995-06-22 18:56:36 +0000 | [diff] [blame] | 359 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 360 | # Example -- report an unbalanced </...> tag. |
| 361 | def report_unbalanced(self, tag): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 362 | if self.verbose: |
| 363 | print '*** Unbalanced </' + tag + '>' |
| 364 | print '*** Stack:', self.stack |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 365 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 366 | def handle_charref(self, name): |
Fred Drake | 08f8dd6 | 2001-07-19 20:08:04 +0000 | [diff] [blame] | 367 | """Handle character reference, no need to override.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 368 | try: |
Eric S. Raymond | 1b645e8 | 2001-02-09 07:49:30 +0000 | [diff] [blame] | 369 | n = int(name) |
Eric S. Raymond | 18af564 | 2001-02-09 10:12:19 +0000 | [diff] [blame] | 370 | except ValueError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 371 | self.unknown_charref(name) |
| 372 | return |
| 373 | if not 0 <= n <= 255: |
| 374 | self.unknown_charref(name) |
| 375 | return |
| 376 | self.handle_data(chr(n)) |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 377 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 378 | # Definition of entities -- derived classes may override |
| 379 | entitydefs = \ |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 380 | {'lt': '<', 'gt': '>', 'amp': '&', 'quot': '"', 'apos': '\''} |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 381 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 382 | def handle_entityref(self, name): |
Fred Drake | 08f8dd6 | 2001-07-19 20:08:04 +0000 | [diff] [blame] | 383 | """Handle entity references. |
| 384 | |
| 385 | There should be no need to override this method; it can be |
| 386 | tailored by setting up the self.entitydefs mapping appropriately. |
| 387 | """ |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 388 | table = self.entitydefs |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 389 | if name in table: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 390 | self.handle_data(table[name]) |
| 391 | else: |
| 392 | self.unknown_entityref(name) |
| 393 | return |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 394 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 395 | # Example -- handle data, should be overridden |
| 396 | def handle_data(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 397 | pass |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 398 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 399 | # Example -- handle comment, could be overridden |
| 400 | def handle_comment(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 401 | pass |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 402 | |
Fred Drake | 6695737 | 2001-03-16 20:04:57 +0000 | [diff] [blame] | 403 | # Example -- handle declaration, could be overridden |
| 404 | def handle_decl(self, decl): |
| 405 | pass |
| 406 | |
Guido van Rossum | 1ad0071 | 1998-05-28 22:48:53 +0000 | [diff] [blame] | 407 | # Example -- handle processing instruction, could be overridden |
| 408 | def handle_pi(self, data): |
| 409 | pass |
| 410 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 411 | # To be overridden -- handlers for unknown objects |
| 412 | def unknown_starttag(self, tag, attrs): pass |
| 413 | def unknown_endtag(self, tag): pass |
| 414 | def unknown_charref(self, ref): pass |
| 415 | def unknown_entityref(self, ref): pass |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 416 | |
| 417 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 418 | class TestSGMLParser(SGMLParser): |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 420 | def __init__(self, verbose=0): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 421 | self.testdata = "" |
| 422 | SGMLParser.__init__(self, verbose) |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 423 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 424 | def handle_data(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 425 | self.testdata = self.testdata + data |
| 426 | if len(`self.testdata`) >= 70: |
| 427 | self.flush() |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 428 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 429 | def flush(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 430 | data = self.testdata |
| 431 | if data: |
| 432 | self.testdata = "" |
| 433 | print 'data:', `data` |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 434 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 435 | def handle_comment(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 436 | self.flush() |
| 437 | r = `data` |
| 438 | if len(r) > 68: |
| 439 | r = r[:32] + '...' + r[-32:] |
| 440 | print 'comment:', r |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 441 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 442 | def unknown_starttag(self, tag, attrs): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 443 | self.flush() |
| 444 | if not attrs: |
| 445 | print 'start tag: <' + tag + '>' |
| 446 | else: |
| 447 | print 'start tag: <' + tag, |
| 448 | for name, value in attrs: |
| 449 | print name + '=' + '"' + value + '"', |
| 450 | print '>' |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 451 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 452 | def unknown_endtag(self, tag): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 453 | self.flush() |
| 454 | print 'end tag: </' + tag + '>' |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 455 | |
| 456 | def unknown_entityref(self, ref): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 457 | self.flush() |
| 458 | print '*** unknown entity ref: &' + ref + ';' |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 459 | |
| 460 | def unknown_charref(self, ref): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 461 | self.flush() |
| 462 | print '*** unknown char ref: &#' + ref + ';' |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 463 | |
Martin v. Löwis | 3163a3b | 2003-03-30 14:25:40 +0000 | [diff] [blame] | 464 | def unknown_decl(self, data): |
| 465 | self.flush() |
| 466 | print '*** unknown decl: [' + data + ']' |
| 467 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 468 | def close(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 469 | SGMLParser.close(self) |
| 470 | self.flush() |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 471 | |
| 472 | |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 473 | def test(args = None): |
| 474 | import sys |
| 475 | |
Raymond Hettinger | f13eb55 | 2002-06-02 00:40:05 +0000 | [diff] [blame] | 476 | if args is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 477 | args = sys.argv[1:] |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 478 | |
| 479 | if args and args[0] == '-s': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 480 | args = args[1:] |
| 481 | klass = SGMLParser |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 482 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 483 | klass = TestSGMLParser |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 484 | |
| 485 | if args: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 486 | file = args[0] |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 487 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 488 | file = 'test.html' |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 489 | |
| 490 | if file == '-': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 491 | f = sys.stdin |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 492 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 493 | try: |
| 494 | f = open(file, 'r') |
| 495 | except IOError, msg: |
| 496 | print file, ":", msg |
| 497 | sys.exit(1) |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 498 | |
| 499 | data = f.read() |
| 500 | if f is not sys.stdin: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 501 | f.close() |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 502 | |
| 503 | x = klass() |
| 504 | for c in data: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 505 | x.feed(c) |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 506 | x.close() |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 507 | |
| 508 | |
Guido van Rossum | 1dba24e | 1995-03-04 22:28:49 +0000 | [diff] [blame] | 509 | if __name__ == '__main__': |
Guido van Rossum | 4876651 | 1996-03-28 18:45:04 +0000 | [diff] [blame] | 510 | test() |