Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 1 | """HTML 2.0 parser. |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 3 | See the HTML 2.0 specification: |
| 4 | http://www.w3.org/hypertext/WWW/MarkUp/html-spec/html-spec_toc.html |
| 5 | """ |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 6 | |
| 7 | |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 8 | import string |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 9 | from sgmllib import SGMLParser |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 10 | from formatter import AS_IS |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 13 | class HTMLParser(SGMLParser): |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 65126d5 | 1995-09-27 16:22:17 +0000 | [diff] [blame] | 15 | from htmlentitydefs import entitydefs |
| 16 | |
Guido van Rossum | 453534a | 1995-09-22 00:55:50 +0000 | [diff] [blame] | 17 | def __init__(self, formatter, verbose=0): |
| 18 | SGMLParser.__init__(self, verbose) |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 19 | self.formatter = formatter |
| 20 | self.savedata = None |
| 21 | self.isindex = 0 |
| 22 | self.title = None |
| 23 | self.base = None |
| 24 | self.anchor = None |
| 25 | self.anchorlist = [] |
| 26 | self.nofill = 0 |
| 27 | self.list_stack = [] |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 29 | # ------ Methods used internally; some may be overridden |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 31 | # --- Formatter interface, taking care of 'savedata' mode; |
| 32 | # shouldn't need to be overridden |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 34 | def handle_data(self, data): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 35 | if self.savedata is not None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 36 | self.savedata = self.savedata + data |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 37 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 38 | if self.nofill: |
| 39 | self.formatter.add_literal_data(data) |
| 40 | else: |
| 41 | self.formatter.add_flowing_data(data) |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 43 | # --- Hooks to save data; shouldn't need to be overridden |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 45 | def save_bgn(self): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 46 | self.savedata = '' |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 48 | def save_end(self): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 49 | data = self.savedata |
| 50 | self.savedata = None |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 51 | if not self.nofill: |
| 52 | data = string.join(string.split(data)) |
| 53 | return data |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 55 | # --- Hooks for anchors; should probably be overridden |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 56 | |
| 57 | def anchor_bgn(self, href, name, type): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 58 | self.anchor = href |
| 59 | if self.anchor: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 60 | self.anchorlist.append(href) |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 61 | |
| 62 | def anchor_end(self): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 63 | if self.anchor: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 64 | self.handle_data("[%d]" % len(self.anchorlist)) |
| 65 | self.anchor = None |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 67 | # --- Hook for images; should probably be overridden |
| 68 | |
Guido van Rossum | 453534a | 1995-09-22 00:55:50 +0000 | [diff] [blame] | 69 | def handle_image(self, src, alt, *args): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 70 | self.handle_data(alt) |
| 71 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 72 | # --------- Top level elememts |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 73 | |
| 74 | def start_html(self, attrs): pass |
| 75 | def end_html(self): pass |
| 76 | |
| 77 | def start_head(self, attrs): pass |
| 78 | def end_head(self): pass |
| 79 | |
| 80 | def start_body(self, attrs): pass |
| 81 | def end_body(self): pass |
| 82 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 83 | # ------ Head elements |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 84 | |
| 85 | def start_title(self, attrs): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 86 | self.save_bgn() |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 87 | |
| 88 | def end_title(self): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 89 | self.title = self.save_end() |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 91 | def do_base(self, attrs): |
| 92 | for a, v in attrs: |
| 93 | if a == 'href': |
| 94 | self.base = v |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 96 | def do_isindex(self, attrs): |
| 97 | self.isindex = 1 |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 99 | def do_link(self, attrs): |
| 100 | pass |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 101 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 102 | def do_meta(self, attrs): |
| 103 | pass |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 105 | def do_nextid(self, attrs): # Deprecated |
| 106 | pass |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 108 | # ------ Body elements |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 110 | # --- Headings |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 111 | |
| 112 | def start_h1(self, attrs): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 113 | self.formatter.end_paragraph(1) |
| 114 | self.formatter.push_font(('h1', 0, 1, 0)) |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 115 | |
| 116 | def end_h1(self): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 117 | self.formatter.end_paragraph(1) |
| 118 | self.formatter.pop_font() |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 120 | def start_h2(self, attrs): |
| 121 | self.formatter.end_paragraph(1) |
| 122 | self.formatter.push_font(('h2', 0, 1, 0)) |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 123 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 124 | def end_h2(self): |
| 125 | self.formatter.end_paragraph(1) |
| 126 | self.formatter.pop_font() |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 128 | def start_h3(self, attrs): |
| 129 | self.formatter.end_paragraph(1) |
| 130 | self.formatter.push_font(('h3', 0, 1, 0)) |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 132 | def end_h3(self): |
| 133 | self.formatter.end_paragraph(1) |
| 134 | self.formatter.pop_font() |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 136 | def start_h4(self, attrs): |
| 137 | self.formatter.end_paragraph(1) |
| 138 | self.formatter.push_font(('h4', 0, 1, 0)) |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 140 | def end_h4(self): |
| 141 | self.formatter.end_paragraph(1) |
| 142 | self.formatter.pop_font() |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 144 | def start_h5(self, attrs): |
| 145 | self.formatter.end_paragraph(1) |
| 146 | self.formatter.push_font(('h5', 0, 1, 0)) |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 147 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 148 | def end_h5(self): |
| 149 | self.formatter.end_paragraph(1) |
| 150 | self.formatter.pop_font() |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 152 | def start_h6(self, attrs): |
| 153 | self.formatter.end_paragraph(1) |
| 154 | self.formatter.push_font(('h6', 0, 1, 0)) |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 156 | def end_h6(self): |
| 157 | self.formatter.end_paragraph(1) |
| 158 | self.formatter.pop_font() |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 160 | # --- Block Structuring Elements |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 162 | def do_p(self, attrs): |
| 163 | self.formatter.end_paragraph(1) |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 164 | |
| 165 | def start_pre(self, attrs): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 166 | self.formatter.end_paragraph(1) |
| 167 | self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1)) |
| 168 | self.nofill = self.nofill + 1 |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 169 | |
| 170 | def end_pre(self): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 171 | self.formatter.end_paragraph(1) |
| 172 | self.formatter.pop_font() |
| 173 | self.nofill = max(0, self.nofill - 1) |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 175 | def start_xmp(self, attrs): |
| 176 | self.start_pre(attrs) |
| 177 | self.setliteral('xmp') # Tell SGML parser |
| 178 | |
| 179 | def end_xmp(self): |
| 180 | self.end_pre() |
| 181 | |
| 182 | def start_listing(self, attrs): |
| 183 | self.start_pre(attrs) |
| 184 | self.setliteral('listing') # Tell SGML parser |
| 185 | |
| 186 | def end_listing(self): |
| 187 | self.end_pre() |
| 188 | |
| 189 | def start_address(self, attrs): |
| 190 | self.formatter.end_paragraph(0) |
| 191 | self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS)) |
| 192 | |
| 193 | def end_address(self): |
| 194 | self.formatter.end_paragraph(0) |
| 195 | self.formatter.pop_font() |
| 196 | |
| 197 | def start_blockquote(self, attrs): |
| 198 | self.formatter.end_paragraph(1) |
| 199 | self.formatter.push_margin('blockquote') |
| 200 | |
| 201 | def end_blockquote(self): |
Guido van Rossum | aa76344 | 1995-08-09 02:31:00 +0000 | [diff] [blame] | 202 | self.formatter.end_paragraph(1) |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 203 | self.formatter.pop_margin() |
| 204 | |
| 205 | # --- List Elements |
| 206 | |
| 207 | def start_ul(self, attrs): |
| 208 | self.formatter.end_paragraph(not self.list_stack) |
| 209 | self.formatter.push_margin('ul') |
| 210 | self.list_stack.append(['ul', '*', 0]) |
| 211 | |
| 212 | def end_ul(self): |
| 213 | if self.list_stack: del self.list_stack[-1] |
| 214 | self.formatter.end_paragraph(not self.list_stack) |
| 215 | self.formatter.pop_margin() |
| 216 | |
| 217 | def do_li(self, attrs): |
| 218 | self.formatter.end_paragraph(0) |
| 219 | if self.list_stack: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 220 | [dummy, label, counter] = top = self.list_stack[-1] |
| 221 | top[2] = counter = counter+1 |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 222 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 223 | label, counter = '*', 0 |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 224 | self.formatter.add_label_data(label, counter) |
| 225 | |
| 226 | def start_ol(self, attrs): |
| 227 | self.formatter.end_paragraph(not self.list_stack) |
| 228 | self.formatter.push_margin('ol') |
| 229 | label = '1.' |
| 230 | for a, v in attrs: |
| 231 | if a == 'type': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 232 | if len(v) == 1: v = v + '.' |
| 233 | label = v |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 234 | self.list_stack.append(['ol', label, 0]) |
| 235 | |
| 236 | def end_ol(self): |
| 237 | if self.list_stack: del self.list_stack[-1] |
| 238 | self.formatter.end_paragraph(not self.list_stack) |
| 239 | self.formatter.pop_margin() |
| 240 | |
| 241 | def start_menu(self, attrs): |
| 242 | self.start_ul(attrs) |
| 243 | |
| 244 | def end_menu(self): |
| 245 | self.end_ul() |
| 246 | |
| 247 | def start_dir(self, attrs): |
| 248 | self.start_ul(attrs) |
| 249 | |
| 250 | def end_dir(self): |
| 251 | self.end_ul() |
| 252 | |
| 253 | def start_dl(self, attrs): |
Guido van Rossum | aa76344 | 1995-08-09 02:31:00 +0000 | [diff] [blame] | 254 | self.formatter.end_paragraph(1) |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 255 | self.list_stack.append(['dl', '', 0]) |
| 256 | |
| 257 | def end_dl(self): |
Guido van Rossum | aa76344 | 1995-08-09 02:31:00 +0000 | [diff] [blame] | 258 | self.ddpop(1) |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 259 | if self.list_stack: del self.list_stack[-1] |
| 260 | |
| 261 | def do_dt(self, attrs): |
| 262 | self.ddpop() |
| 263 | |
| 264 | def do_dd(self, attrs): |
| 265 | self.ddpop() |
| 266 | self.formatter.push_margin('dd') |
| 267 | self.list_stack.append(['dd', '', 0]) |
| 268 | |
Guido van Rossum | aa76344 | 1995-08-09 02:31:00 +0000 | [diff] [blame] | 269 | def ddpop(self, bl=0): |
| 270 | self.formatter.end_paragraph(bl) |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 271 | if self.list_stack: |
| 272 | if self.list_stack[-1][0] == 'dd': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 273 | del self.list_stack[-1] |
| 274 | self.formatter.pop_margin() |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 275 | |
| 276 | # --- Phrase Markup |
| 277 | |
| 278 | # Idiomatic Elements |
| 279 | |
| 280 | def start_cite(self, attrs): self.start_i(attrs) |
| 281 | def end_cite(self): self.end_i() |
| 282 | |
| 283 | def start_code(self, attrs): self.start_tt(attrs) |
| 284 | def end_code(self): self.end_tt() |
| 285 | |
| 286 | def start_em(self, attrs): self.start_i(attrs) |
| 287 | def end_em(self): self.end_i() |
| 288 | |
| 289 | def start_kbd(self, attrs): self.start_tt(attrs) |
| 290 | def end_kbd(self): self.end_tt() |
| 291 | |
| 292 | def start_samp(self, attrs): self.start_tt(attrs) |
| 293 | def end_samp(self): self.end_tt() |
| 294 | |
Guido van Rossum | aa76344 | 1995-08-09 02:31:00 +0000 | [diff] [blame] | 295 | def start_strong(self, attrs): self.start_b(attrs) |
| 296 | def end_strong(self): self.end_b() |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 297 | |
| 298 | def start_var(self, attrs): self.start_i(attrs) |
Guido van Rossum | aa76344 | 1995-08-09 02:31:00 +0000 | [diff] [blame] | 299 | def end_var(self): self.end_i() |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 300 | |
| 301 | # Typographic Elements |
| 302 | |
| 303 | def start_i(self, attrs): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 304 | self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS)) |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 305 | def end_i(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 306 | self.formatter.pop_font() |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 307 | |
| 308 | def start_b(self, attrs): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 309 | self.formatter.push_font((AS_IS, AS_IS, 1, AS_IS)) |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 310 | def end_b(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 311 | self.formatter.pop_font() |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 312 | |
| 313 | def start_tt(self, attrs): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 314 | self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1)) |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 315 | def end_tt(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 316 | self.formatter.pop_font() |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 317 | |
| 318 | def start_a(self, attrs): |
| 319 | href = '' |
| 320 | name = '' |
| 321 | type = '' |
| 322 | for attrname, value in attrs: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 323 | value = string.strip(value) |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 324 | if attrname == 'href': |
| 325 | href = value |
| 326 | if attrname == 'name': |
| 327 | name = value |
| 328 | if attrname == 'type': |
| 329 | type = string.lower(value) |
| 330 | self.anchor_bgn(href, name, type) |
| 331 | |
| 332 | def end_a(self): |
| 333 | self.anchor_end() |
| 334 | |
| 335 | # --- Line Break |
| 336 | |
| 337 | def do_br(self, attrs): |
| 338 | self.formatter.add_line_break() |
| 339 | |
| 340 | # --- Horizontal Rule |
| 341 | |
| 342 | def do_hr(self, attrs): |
| 343 | self.formatter.add_hor_rule() |
| 344 | |
| 345 | # --- Image |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 346 | |
| 347 | def do_img(self, attrs): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 348 | align = '' |
| 349 | alt = '(image)' |
| 350 | ismap = '' |
| 351 | src = '' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 352 | width = 0 |
| 353 | height = 0 |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 354 | for attrname, value in attrs: |
| 355 | if attrname == 'align': |
| 356 | align = value |
| 357 | if attrname == 'alt': |
| 358 | alt = value |
| 359 | if attrname == 'ismap': |
| 360 | ismap = value |
| 361 | if attrname == 'src': |
| 362 | src = value |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 363 | if attrname == 'width': |
| 364 | try: width = string.atoi(value) |
| 365 | except: pass |
| 366 | if attrname == 'height': |
| 367 | try: height = string.atoi(value) |
| 368 | except: pass |
Guido van Rossum | 453534a | 1995-09-22 00:55:50 +0000 | [diff] [blame] | 369 | self.handle_image(src, alt, ismap, align, width, height) |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 370 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 371 | # --- Really Old Unofficial Deprecated Stuff |
| 372 | |
| 373 | def do_plaintext(self, attrs): |
| 374 | self.start_pre(attrs) |
| 375 | self.setnomoretags() # Tell SGML parser |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 376 | |
| 377 | # --- Unhandled tags |
| 378 | |
| 379 | def unknown_starttag(self, tag, attrs): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 380 | pass |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 381 | |
| 382 | def unknown_endtag(self, tag): |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 383 | pass |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 384 | |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 385 | |
Guido van Rossum | a98b0a1 | 1996-02-13 00:02:10 +0000 | [diff] [blame] | 386 | def test(args = None): |
| 387 | import sys, formatter |
| 388 | |
| 389 | if not args: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 390 | args = sys.argv[1:] |
Guido van Rossum | a98b0a1 | 1996-02-13 00:02:10 +0000 | [diff] [blame] | 391 | |
| 392 | silent = args and args[0] == '-s' |
| 393 | if silent: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 394 | del args[0] |
Guido van Rossum | a98b0a1 | 1996-02-13 00:02:10 +0000 | [diff] [blame] | 395 | |
| 396 | if args: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 397 | file = args[0] |
Guido van Rossum | a98b0a1 | 1996-02-13 00:02:10 +0000 | [diff] [blame] | 398 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 399 | file = 'test.html' |
Guido van Rossum | a98b0a1 | 1996-02-13 00:02:10 +0000 | [diff] [blame] | 400 | |
| 401 | if file == '-': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 402 | f = sys.stdin |
Guido van Rossum | a98b0a1 | 1996-02-13 00:02:10 +0000 | [diff] [blame] | 403 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 404 | try: |
| 405 | f = open(file, 'r') |
| 406 | except IOError, msg: |
| 407 | print file, ":", msg |
| 408 | sys.exit(1) |
Guido van Rossum | a98b0a1 | 1996-02-13 00:02:10 +0000 | [diff] [blame] | 409 | |
| 410 | data = f.read() |
| 411 | |
| 412 | if f is not sys.stdin: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 413 | f.close() |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame^] | 414 | |
Guido van Rossum | a98b0a1 | 1996-02-13 00:02:10 +0000 | [diff] [blame] | 415 | if silent: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 416 | f = formatter.NullFormatter() |
Guido van Rossum | a98b0a1 | 1996-02-13 00:02:10 +0000 | [diff] [blame] | 417 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 418 | f = formatter.AbstractFormatter(formatter.DumbWriter()) |
Guido van Rossum | a98b0a1 | 1996-02-13 00:02:10 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | f54d967 | 1995-08-07 20:07:44 +0000 | [diff] [blame] | 420 | p = HTMLParser(f) |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 421 | p.feed(data) |
| 422 | p.close() |
Guido van Rossum | 7c750e1 | 1995-02-27 13:16:55 +0000 | [diff] [blame] | 423 | |
| 424 | |
| 425 | if __name__ == '__main__': |
Guido van Rossum | 7ff5d7f | 1995-08-04 04:23:30 +0000 | [diff] [blame] | 426 | test() |