| 1 | n/a | """HTML 2.0 parser. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | See the HTML 2.0 specification: |
|---|
| 4 | n/a | http://www.w3.org/hypertext/WWW/MarkUp/html-spec/html-spec_toc.html |
|---|
| 5 | 1 | """ |
|---|
| 6 | n/a | |
|---|
| 7 | 1 | from warnings import warnpy3k |
|---|
| 8 | 1 | warnpy3k("the htmllib module has been removed in Python 3.0", |
|---|
| 9 | 1 | stacklevel=2) |
|---|
| 10 | 1 | del warnpy3k |
|---|
| 11 | n/a | |
|---|
| 12 | 1 | import sgmllib |
|---|
| 13 | n/a | |
|---|
| 14 | 1 | from formatter import AS_IS |
|---|
| 15 | n/a | |
|---|
| 16 | 1 | __all__ = ["HTMLParser", "HTMLParseError"] |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | 2 | class HTMLParseError(sgmllib.SGMLParseError): |
|---|
| 20 | 1 | """Error raised when an HTML document can't be parsed.""" |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | |
|---|
| 23 | 2 | class HTMLParser(sgmllib.SGMLParser): |
|---|
| 24 | n/a | """This is the basic HTML parser class. |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | It supports all entity names required by the XHTML 1.0 Recommendation. |
|---|
| 27 | n/a | It also defines handlers for all HTML 2.0 and many HTML 3.0 and 3.2 |
|---|
| 28 | n/a | elements. |
|---|
| 29 | n/a | |
|---|
| 30 | 1 | """ |
|---|
| 31 | n/a | |
|---|
| 32 | 1 | from htmlentitydefs import entitydefs |
|---|
| 33 | n/a | |
|---|
| 34 | 1 | def __init__(self, formatter, verbose=0): |
|---|
| 35 | n/a | """Creates an instance of the HTMLParser class. |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | The formatter parameter is the formatter instance associated with |
|---|
| 38 | n/a | the parser. |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | """ |
|---|
| 41 | 2 | sgmllib.SGMLParser.__init__(self, verbose) |
|---|
| 42 | 2 | self.formatter = formatter |
|---|
| 43 | n/a | |
|---|
| 44 | 1 | def error(self, message): |
|---|
| 45 | 0 | raise HTMLParseError(message) |
|---|
| 46 | n/a | |
|---|
| 47 | 1 | def reset(self): |
|---|
| 48 | 2 | sgmllib.SGMLParser.reset(self) |
|---|
| 49 | 2 | self.savedata = None |
|---|
| 50 | 2 | self.isindex = 0 |
|---|
| 51 | 2 | self.title = None |
|---|
| 52 | 2 | self.base = None |
|---|
| 53 | 2 | self.anchor = None |
|---|
| 54 | 2 | self.anchorlist = [] |
|---|
| 55 | 2 | self.nofill = 0 |
|---|
| 56 | 2 | self.list_stack = [] |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | # ------ Methods used internally; some may be overridden |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | # --- Formatter interface, taking care of 'savedata' mode; |
|---|
| 61 | n/a | # shouldn't need to be overridden |
|---|
| 62 | n/a | |
|---|
| 63 | 1 | def handle_data(self, data): |
|---|
| 64 | 12 | if self.savedata is not None: |
|---|
| 65 | 0 | self.savedata = self.savedata + data |
|---|
| 66 | n/a | else: |
|---|
| 67 | 12 | if self.nofill: |
|---|
| 68 | 0 | self.formatter.add_literal_data(data) |
|---|
| 69 | n/a | else: |
|---|
| 70 | 12 | self.formatter.add_flowing_data(data) |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | # --- Hooks to save data; shouldn't need to be overridden |
|---|
| 73 | n/a | |
|---|
| 74 | 1 | def save_bgn(self): |
|---|
| 75 | n/a | """Begins saving character data in a buffer instead of sending it |
|---|
| 76 | n/a | to the formatter object. |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | Retrieve the stored data via the save_end() method. Use of the |
|---|
| 79 | n/a | save_bgn() / save_end() pair may not be nested. |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | """ |
|---|
| 82 | 0 | self.savedata = '' |
|---|
| 83 | n/a | |
|---|
| 84 | 1 | def save_end(self): |
|---|
| 85 | n/a | """Ends buffering character data and returns all data saved since |
|---|
| 86 | n/a | the preceding call to the save_bgn() method. |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | If the nofill flag is false, whitespace is collapsed to single |
|---|
| 89 | n/a | spaces. A call to this method without a preceding call to the |
|---|
| 90 | n/a | save_bgn() method will raise a TypeError exception. |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | """ |
|---|
| 93 | 0 | data = self.savedata |
|---|
| 94 | 0 | self.savedata = None |
|---|
| 95 | 0 | if not self.nofill: |
|---|
| 96 | 0 | data = ' '.join(data.split()) |
|---|
| 97 | 0 | return data |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | # --- Hooks for anchors; should probably be overridden |
|---|
| 100 | n/a | |
|---|
| 101 | 1 | def anchor_bgn(self, href, name, type): |
|---|
| 102 | n/a | """This method is called at the start of an anchor region. |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | The arguments correspond to the attributes of the <A> tag with |
|---|
| 105 | n/a | the same names. The default implementation maintains a list of |
|---|
| 106 | n/a | hyperlinks (defined by the HREF attribute for <A> tags) within |
|---|
| 107 | n/a | the document. The list of hyperlinks is available as the data |
|---|
| 108 | n/a | attribute anchorlist. |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | """ |
|---|
| 111 | 0 | self.anchor = href |
|---|
| 112 | 0 | if self.anchor: |
|---|
| 113 | 0 | self.anchorlist.append(href) |
|---|
| 114 | n/a | |
|---|
| 115 | 1 | def anchor_end(self): |
|---|
| 116 | n/a | """This method is called at the end of an anchor region. |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | The default implementation adds a textual footnote marker using an |
|---|
| 119 | n/a | index into the list of hyperlinks created by the anchor_bgn()method. |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | """ |
|---|
| 122 | 3 | if self.anchor: |
|---|
| 123 | 0 | self.handle_data("[%d]" % len(self.anchorlist)) |
|---|
| 124 | 0 | self.anchor = None |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | # --- Hook for images; should probably be overridden |
|---|
| 127 | n/a | |
|---|
| 128 | 1 | def handle_image(self, src, alt, *args): |
|---|
| 129 | n/a | """This method is called to handle images. |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | The default implementation simply passes the alt value to the |
|---|
| 132 | n/a | handle_data() method. |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | """ |
|---|
| 135 | 0 | self.handle_data(alt) |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | # --------- Top level elememts |
|---|
| 138 | n/a | |
|---|
| 139 | 2 | def start_html(self, attrs): pass |
|---|
| 140 | 2 | def end_html(self): pass |
|---|
| 141 | n/a | |
|---|
| 142 | 1 | def start_head(self, attrs): pass |
|---|
| 143 | 1 | def end_head(self): pass |
|---|
| 144 | n/a | |
|---|
| 145 | 2 | def start_body(self, attrs): pass |
|---|
| 146 | 2 | def end_body(self): pass |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | # ------ Head elements |
|---|
| 149 | n/a | |
|---|
| 150 | 1 | def start_title(self, attrs): |
|---|
| 151 | 0 | self.save_bgn() |
|---|
| 152 | n/a | |
|---|
| 153 | 1 | def end_title(self): |
|---|
| 154 | 0 | self.title = self.save_end() |
|---|
| 155 | n/a | |
|---|
| 156 | 1 | def do_base(self, attrs): |
|---|
| 157 | 0 | for a, v in attrs: |
|---|
| 158 | 0 | if a == 'href': |
|---|
| 159 | 0 | self.base = v |
|---|
| 160 | n/a | |
|---|
| 161 | 1 | def do_isindex(self, attrs): |
|---|
| 162 | 0 | self.isindex = 1 |
|---|
| 163 | n/a | |
|---|
| 164 | 1 | def do_link(self, attrs): |
|---|
| 165 | 0 | pass |
|---|
| 166 | n/a | |
|---|
| 167 | 1 | def do_meta(self, attrs): |
|---|
| 168 | 0 | pass |
|---|
| 169 | n/a | |
|---|
| 170 | 1 | def do_nextid(self, attrs): # Deprecated |
|---|
| 171 | 0 | pass |
|---|
| 172 | n/a | |
|---|
| 173 | n/a | # ------ Body elements |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | # --- Headings |
|---|
| 176 | n/a | |
|---|
| 177 | 1 | def start_h1(self, attrs): |
|---|
| 178 | 0 | self.formatter.end_paragraph(1) |
|---|
| 179 | 0 | self.formatter.push_font(('h1', 0, 1, 0)) |
|---|
| 180 | n/a | |
|---|
| 181 | 1 | def end_h1(self): |
|---|
| 182 | 0 | self.formatter.end_paragraph(1) |
|---|
| 183 | 0 | self.formatter.pop_font() |
|---|
| 184 | n/a | |
|---|
| 185 | 1 | def start_h2(self, attrs): |
|---|
| 186 | 0 | self.formatter.end_paragraph(1) |
|---|
| 187 | 0 | self.formatter.push_font(('h2', 0, 1, 0)) |
|---|
| 188 | n/a | |
|---|
| 189 | 1 | def end_h2(self): |
|---|
| 190 | 0 | self.formatter.end_paragraph(1) |
|---|
| 191 | 0 | self.formatter.pop_font() |
|---|
| 192 | n/a | |
|---|
| 193 | 1 | def start_h3(self, attrs): |
|---|
| 194 | 0 | self.formatter.end_paragraph(1) |
|---|
| 195 | 0 | self.formatter.push_font(('h3', 0, 1, 0)) |
|---|
| 196 | n/a | |
|---|
| 197 | 1 | def end_h3(self): |
|---|
| 198 | 0 | self.formatter.end_paragraph(1) |
|---|
| 199 | 0 | self.formatter.pop_font() |
|---|
| 200 | n/a | |
|---|
| 201 | 1 | def start_h4(self, attrs): |
|---|
| 202 | 0 | self.formatter.end_paragraph(1) |
|---|
| 203 | 0 | self.formatter.push_font(('h4', 0, 1, 0)) |
|---|
| 204 | n/a | |
|---|
| 205 | 1 | def end_h4(self): |
|---|
| 206 | 0 | self.formatter.end_paragraph(1) |
|---|
| 207 | 0 | self.formatter.pop_font() |
|---|
| 208 | n/a | |
|---|
| 209 | 1 | def start_h5(self, attrs): |
|---|
| 210 | 0 | self.formatter.end_paragraph(1) |
|---|
| 211 | 0 | self.formatter.push_font(('h5', 0, 1, 0)) |
|---|
| 212 | n/a | |
|---|
| 213 | 1 | def end_h5(self): |
|---|
| 214 | 0 | self.formatter.end_paragraph(1) |
|---|
| 215 | 0 | self.formatter.pop_font() |
|---|
| 216 | n/a | |
|---|
| 217 | 1 | def start_h6(self, attrs): |
|---|
| 218 | 0 | self.formatter.end_paragraph(1) |
|---|
| 219 | 0 | self.formatter.push_font(('h6', 0, 1, 0)) |
|---|
| 220 | n/a | |
|---|
| 221 | 1 | def end_h6(self): |
|---|
| 222 | 0 | self.formatter.end_paragraph(1) |
|---|
| 223 | 0 | self.formatter.pop_font() |
|---|
| 224 | n/a | |
|---|
| 225 | n/a | # --- Block Structuring Elements |
|---|
| 226 | n/a | |
|---|
| 227 | 1 | def do_p(self, attrs): |
|---|
| 228 | 0 | self.formatter.end_paragraph(1) |
|---|
| 229 | n/a | |
|---|
| 230 | 1 | def start_pre(self, attrs): |
|---|
| 231 | 0 | self.formatter.end_paragraph(1) |
|---|
| 232 | 0 | self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1)) |
|---|
| 233 | 0 | self.nofill = self.nofill + 1 |
|---|
| 234 | n/a | |
|---|
| 235 | 1 | def end_pre(self): |
|---|
| 236 | 0 | self.formatter.end_paragraph(1) |
|---|
| 237 | 0 | self.formatter.pop_font() |
|---|
| 238 | 0 | self.nofill = max(0, self.nofill - 1) |
|---|
| 239 | n/a | |
|---|
| 240 | 1 | def start_xmp(self, attrs): |
|---|
| 241 | 0 | self.start_pre(attrs) |
|---|
| 242 | 0 | self.setliteral('xmp') # Tell SGML parser |
|---|
| 243 | n/a | |
|---|
| 244 | 1 | def end_xmp(self): |
|---|
| 245 | 0 | self.end_pre() |
|---|
| 246 | n/a | |
|---|
| 247 | 1 | def start_listing(self, attrs): |
|---|
| 248 | 0 | self.start_pre(attrs) |
|---|
| 249 | 0 | self.setliteral('listing') # Tell SGML parser |
|---|
| 250 | n/a | |
|---|
| 251 | 1 | def end_listing(self): |
|---|
| 252 | 0 | self.end_pre() |
|---|
| 253 | n/a | |
|---|
| 254 | 1 | def start_address(self, attrs): |
|---|
| 255 | 0 | self.formatter.end_paragraph(0) |
|---|
| 256 | 0 | self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS)) |
|---|
| 257 | n/a | |
|---|
| 258 | 1 | def end_address(self): |
|---|
| 259 | 0 | self.formatter.end_paragraph(0) |
|---|
| 260 | 0 | self.formatter.pop_font() |
|---|
| 261 | n/a | |
|---|
| 262 | 1 | def start_blockquote(self, attrs): |
|---|
| 263 | 0 | self.formatter.end_paragraph(1) |
|---|
| 264 | 0 | self.formatter.push_margin('blockquote') |
|---|
| 265 | n/a | |
|---|
| 266 | 1 | def end_blockquote(self): |
|---|
| 267 | 0 | self.formatter.end_paragraph(1) |
|---|
| 268 | 0 | self.formatter.pop_margin() |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | # --- List Elements |
|---|
| 271 | n/a | |
|---|
| 272 | 1 | def start_ul(self, attrs): |
|---|
| 273 | 0 | self.formatter.end_paragraph(not self.list_stack) |
|---|
| 274 | 0 | self.formatter.push_margin('ul') |
|---|
| 275 | 0 | self.list_stack.append(['ul', '*', 0]) |
|---|
| 276 | n/a | |
|---|
| 277 | 1 | def end_ul(self): |
|---|
| 278 | 0 | if self.list_stack: del self.list_stack[-1] |
|---|
| 279 | 0 | self.formatter.end_paragraph(not self.list_stack) |
|---|
| 280 | 0 | self.formatter.pop_margin() |
|---|
| 281 | n/a | |
|---|
| 282 | 1 | def do_li(self, attrs): |
|---|
| 283 | 0 | self.formatter.end_paragraph(0) |
|---|
| 284 | 0 | if self.list_stack: |
|---|
| 285 | 0 | [dummy, label, counter] = top = self.list_stack[-1] |
|---|
| 286 | 0 | top[2] = counter = counter+1 |
|---|
| 287 | n/a | else: |
|---|
| 288 | 0 | label, counter = '*', 0 |
|---|
| 289 | 0 | self.formatter.add_label_data(label, counter) |
|---|
| 290 | n/a | |
|---|
| 291 | 1 | def start_ol(self, attrs): |
|---|
| 292 | 0 | self.formatter.end_paragraph(not self.list_stack) |
|---|
| 293 | 0 | self.formatter.push_margin('ol') |
|---|
| 294 | 0 | label = '1.' |
|---|
| 295 | 0 | for a, v in attrs: |
|---|
| 296 | 0 | if a == 'type': |
|---|
| 297 | 0 | if len(v) == 1: v = v + '.' |
|---|
| 298 | 0 | label = v |
|---|
| 299 | 0 | self.list_stack.append(['ol', label, 0]) |
|---|
| 300 | n/a | |
|---|
| 301 | 1 | def end_ol(self): |
|---|
| 302 | 0 | if self.list_stack: del self.list_stack[-1] |
|---|
| 303 | 0 | self.formatter.end_paragraph(not self.list_stack) |
|---|
| 304 | 0 | self.formatter.pop_margin() |
|---|
| 305 | n/a | |
|---|
| 306 | 1 | def start_menu(self, attrs): |
|---|
| 307 | 0 | self.start_ul(attrs) |
|---|
| 308 | n/a | |
|---|
| 309 | 1 | def end_menu(self): |
|---|
| 310 | 0 | self.end_ul() |
|---|
| 311 | n/a | |
|---|
| 312 | 1 | def start_dir(self, attrs): |
|---|
| 313 | 0 | self.start_ul(attrs) |
|---|
| 314 | n/a | |
|---|
| 315 | 1 | def end_dir(self): |
|---|
| 316 | 0 | self.end_ul() |
|---|
| 317 | n/a | |
|---|
| 318 | 1 | def start_dl(self, attrs): |
|---|
| 319 | 0 | self.formatter.end_paragraph(1) |
|---|
| 320 | 0 | self.list_stack.append(['dl', '', 0]) |
|---|
| 321 | n/a | |
|---|
| 322 | 1 | def end_dl(self): |
|---|
| 323 | 0 | self.ddpop(1) |
|---|
| 324 | 0 | if self.list_stack: del self.list_stack[-1] |
|---|
| 325 | n/a | |
|---|
| 326 | 1 | def do_dt(self, attrs): |
|---|
| 327 | 0 | self.ddpop() |
|---|
| 328 | n/a | |
|---|
| 329 | 1 | def do_dd(self, attrs): |
|---|
| 330 | 0 | self.ddpop() |
|---|
| 331 | 0 | self.formatter.push_margin('dd') |
|---|
| 332 | 0 | self.list_stack.append(['dd', '', 0]) |
|---|
| 333 | n/a | |
|---|
| 334 | 1 | def ddpop(self, bl=0): |
|---|
| 335 | 0 | self.formatter.end_paragraph(bl) |
|---|
| 336 | 0 | if self.list_stack: |
|---|
| 337 | 0 | if self.list_stack[-1][0] == 'dd': |
|---|
| 338 | 0 | del self.list_stack[-1] |
|---|
| 339 | 0 | self.formatter.pop_margin() |
|---|
| 340 | n/a | |
|---|
| 341 | n/a | # --- Phrase Markup |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | # Idiomatic Elements |
|---|
| 344 | n/a | |
|---|
| 345 | 1 | def start_cite(self, attrs): self.start_i(attrs) |
|---|
| 346 | 1 | def end_cite(self): self.end_i() |
|---|
| 347 | n/a | |
|---|
| 348 | 1 | def start_code(self, attrs): self.start_tt(attrs) |
|---|
| 349 | 1 | def end_code(self): self.end_tt() |
|---|
| 350 | n/a | |
|---|
| 351 | 1 | def start_em(self, attrs): self.start_i(attrs) |
|---|
| 352 | 1 | def end_em(self): self.end_i() |
|---|
| 353 | n/a | |
|---|
| 354 | 1 | def start_kbd(self, attrs): self.start_tt(attrs) |
|---|
| 355 | 1 | def end_kbd(self): self.end_tt() |
|---|
| 356 | n/a | |
|---|
| 357 | 1 | def start_samp(self, attrs): self.start_tt(attrs) |
|---|
| 358 | 1 | def end_samp(self): self.end_tt() |
|---|
| 359 | n/a | |
|---|
| 360 | 1 | def start_strong(self, attrs): self.start_b(attrs) |
|---|
| 361 | 1 | def end_strong(self): self.end_b() |
|---|
| 362 | n/a | |
|---|
| 363 | 1 | def start_var(self, attrs): self.start_i(attrs) |
|---|
| 364 | 1 | def end_var(self): self.end_i() |
|---|
| 365 | n/a | |
|---|
| 366 | n/a | # Typographic Elements |
|---|
| 367 | n/a | |
|---|
| 368 | 1 | def start_i(self, attrs): |
|---|
| 369 | 0 | self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS)) |
|---|
| 370 | 1 | def end_i(self): |
|---|
| 371 | 0 | self.formatter.pop_font() |
|---|
| 372 | n/a | |
|---|
| 373 | 1 | def start_b(self, attrs): |
|---|
| 374 | 0 | self.formatter.push_font((AS_IS, AS_IS, 1, AS_IS)) |
|---|
| 375 | 1 | def end_b(self): |
|---|
| 376 | 0 | self.formatter.pop_font() |
|---|
| 377 | n/a | |
|---|
| 378 | 1 | def start_tt(self, attrs): |
|---|
| 379 | 0 | self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1)) |
|---|
| 380 | 1 | def end_tt(self): |
|---|
| 381 | 0 | self.formatter.pop_font() |
|---|
| 382 | n/a | |
|---|
| 383 | 1 | def start_a(self, attrs): |
|---|
| 384 | 3 | href = '' |
|---|
| 385 | 3 | name = '' |
|---|
| 386 | 3 | type = '' |
|---|
| 387 | 7 | for attrname, value in attrs: |
|---|
| 388 | 4 | value = value.strip() |
|---|
| 389 | 4 | if attrname == 'href': |
|---|
| 390 | 2 | href = value |
|---|
| 391 | 4 | if attrname == 'name': |
|---|
| 392 | 2 | name = value |
|---|
| 393 | 4 | if attrname == 'type': |
|---|
| 394 | 0 | type = value.lower() |
|---|
| 395 | 3 | self.anchor_bgn(href, name, type) |
|---|
| 396 | n/a | |
|---|
| 397 | 1 | def end_a(self): |
|---|
| 398 | 3 | self.anchor_end() |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | # --- Line Break |
|---|
| 401 | n/a | |
|---|
| 402 | 1 | def do_br(self, attrs): |
|---|
| 403 | 0 | self.formatter.add_line_break() |
|---|
| 404 | n/a | |
|---|
| 405 | n/a | # --- Horizontal Rule |
|---|
| 406 | n/a | |
|---|
| 407 | 1 | def do_hr(self, attrs): |
|---|
| 408 | 0 | self.formatter.add_hor_rule() |
|---|
| 409 | n/a | |
|---|
| 410 | n/a | # --- Image |
|---|
| 411 | n/a | |
|---|
| 412 | 1 | def do_img(self, attrs): |
|---|
| 413 | 0 | align = '' |
|---|
| 414 | 0 | alt = '(image)' |
|---|
| 415 | 0 | ismap = '' |
|---|
| 416 | 0 | src = '' |
|---|
| 417 | 0 | width = 0 |
|---|
| 418 | 0 | height = 0 |
|---|
| 419 | 0 | for attrname, value in attrs: |
|---|
| 420 | 0 | if attrname == 'align': |
|---|
| 421 | 0 | align = value |
|---|
| 422 | 0 | if attrname == 'alt': |
|---|
| 423 | 0 | alt = value |
|---|
| 424 | 0 | if attrname == 'ismap': |
|---|
| 425 | 0 | ismap = value |
|---|
| 426 | 0 | if attrname == 'src': |
|---|
| 427 | 0 | src = value |
|---|
| 428 | 0 | if attrname == 'width': |
|---|
| 429 | 0 | try: width = int(value) |
|---|
| 430 | 0 | except ValueError: pass |
|---|
| 431 | 0 | if attrname == 'height': |
|---|
| 432 | 0 | try: height = int(value) |
|---|
| 433 | 0 | except ValueError: pass |
|---|
| 434 | 0 | self.handle_image(src, alt, ismap, align, width, height) |
|---|
| 435 | n/a | |
|---|
| 436 | n/a | # --- Really Old Unofficial Deprecated Stuff |
|---|
| 437 | n/a | |
|---|
| 438 | 1 | def do_plaintext(self, attrs): |
|---|
| 439 | 0 | self.start_pre(attrs) |
|---|
| 440 | 0 | self.setnomoretags() # Tell SGML parser |
|---|
| 441 | n/a | |
|---|
| 442 | n/a | # --- Unhandled tags |
|---|
| 443 | n/a | |
|---|
| 444 | 1 | def unknown_starttag(self, tag, attrs): |
|---|
| 445 | 0 | pass |
|---|
| 446 | n/a | |
|---|
| 447 | 1 | def unknown_endtag(self, tag): |
|---|
| 448 | 0 | pass |
|---|
| 449 | n/a | |
|---|
| 450 | n/a | |
|---|
| 451 | 1 | def test(args = None): |
|---|
| 452 | 0 | import sys, formatter |
|---|
| 453 | n/a | |
|---|
| 454 | 0 | if not args: |
|---|
| 455 | 0 | args = sys.argv[1:] |
|---|
| 456 | n/a | |
|---|
| 457 | 0 | silent = args and args[0] == '-s' |
|---|
| 458 | 0 | if silent: |
|---|
| 459 | 0 | del args[0] |
|---|
| 460 | n/a | |
|---|
| 461 | 0 | if args: |
|---|
| 462 | 0 | file = args[0] |
|---|
| 463 | n/a | else: |
|---|
| 464 | 0 | file = 'test.html' |
|---|
| 465 | n/a | |
|---|
| 466 | 0 | if file == '-': |
|---|
| 467 | 0 | f = sys.stdin |
|---|
| 468 | n/a | else: |
|---|
| 469 | 0 | try: |
|---|
| 470 | 0 | f = open(file, 'r') |
|---|
| 471 | 0 | except IOError, msg: |
|---|
| 472 | 0 | print file, ":", msg |
|---|
| 473 | 0 | sys.exit(1) |
|---|
| 474 | n/a | |
|---|
| 475 | 0 | data = f.read() |
|---|
| 476 | n/a | |
|---|
| 477 | 0 | if f is not sys.stdin: |
|---|
| 478 | 0 | f.close() |
|---|
| 479 | n/a | |
|---|
| 480 | 0 | if silent: |
|---|
| 481 | 0 | f = formatter.NullFormatter() |
|---|
| 482 | n/a | else: |
|---|
| 483 | 0 | f = formatter.AbstractFormatter(formatter.DumbWriter()) |
|---|
| 484 | n/a | |
|---|
| 485 | 0 | p = HTMLParser(f) |
|---|
| 486 | 0 | p.feed(data) |
|---|
| 487 | 0 | p.close() |
|---|
| 488 | n/a | |
|---|
| 489 | n/a | |
|---|
| 490 | 1 | if __name__ == '__main__': |
|---|
| 491 | 0 | test() |
|---|