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