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