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