1995-08-07 17:07:44 -03:00
|
|
|
"""HTML 2.0 parser.
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
See the HTML 2.0 specification:
|
|
|
|
http://www.w3.org/hypertext/WWW/MarkUp/html-spec/html-spec_toc.html
|
|
|
|
"""
|
1995-02-27 09:16:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
import sys
|
1995-08-04 01:23:30 -03:00
|
|
|
import regsub
|
1995-02-27 09:16:55 -04:00
|
|
|
import string
|
1995-08-04 01:23:30 -03:00
|
|
|
from sgmllib import SGMLParser
|
1995-08-07 17:07:44 -03:00
|
|
|
from formatter import AS_IS
|
1995-02-27 09:16:55 -04:00
|
|
|
|
|
|
|
|
1995-08-04 01:23:30 -03:00
|
|
|
class HTMLParser(SGMLParser):
|
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def __init__(self, formatter):
|
|
|
|
SGMLParser.__init__(self)
|
|
|
|
self.formatter = formatter
|
|
|
|
self.savedata = None
|
|
|
|
self.isindex = 0
|
|
|
|
self.title = None
|
|
|
|
self.base = None
|
|
|
|
self.anchor = None
|
|
|
|
self.anchorlist = []
|
|
|
|
self.nofill = 0
|
|
|
|
self.list_stack = []
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# ------ Methods used internally; some may be overridden
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# --- Formatter interface, taking care of 'savedata' mode;
|
|
|
|
# shouldn't need to be overridden
|
1995-08-04 01:23:30 -03:00
|
|
|
|
|
|
|
def handle_data(self, data):
|
1995-08-07 17:07:44 -03:00
|
|
|
if self.savedata is not None:
|
1995-08-04 01:23:30 -03:00
|
|
|
self.savedata = self.savedata + data
|
1995-08-07 17:07:44 -03:00
|
|
|
else:
|
|
|
|
if self.nofill:
|
|
|
|
self.formatter.add_literal_data(data)
|
|
|
|
else:
|
|
|
|
self.formatter.add_flowing_data(data)
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# --- Hooks to save data; shouldn't need to be overridden
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-04 01:23:30 -03:00
|
|
|
def save_bgn(self):
|
1995-08-07 17:07:44 -03:00
|
|
|
self.savedata = ''
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-04 01:23:30 -03:00
|
|
|
def save_end(self):
|
1995-08-07 17:07:44 -03:00
|
|
|
data = self.savedata
|
|
|
|
self.savedata = None
|
|
|
|
return string.join(string.split(data))
|
|
|
|
|
|
|
|
# --- Hooks for anchors; should probably be overridden
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def anchor_bgn(self, href, name, type):
|
|
|
|
self.anchor = href
|
|
|
|
if self.anchor:
|
|
|
|
self.anchorlist.append(href)
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def anchor_end(self):
|
|
|
|
if self.anchor:
|
|
|
|
self.handle_data("[%d]" % len(self.anchorlist))
|
|
|
|
self.anchor = None
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# --- Hook for images; should probably be overridden
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def handle_image(self, src, alt):
|
|
|
|
self.handle_data(alt)
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# --- Hooks for forms; should probably be overridden
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def form_bgn(self, action, method, enctype):
|
|
|
|
self.do_p([])
|
|
|
|
self.handle_data("<FORM>")
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def form_end(self):
|
|
|
|
self.handle_data("</FORM>")
|
|
|
|
self.do_p([])
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def handle_input(self, type, options):
|
|
|
|
self.handle_data("<INPUT>")
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def select_bgn(self, name, size, multiple):
|
|
|
|
self.handle_data("<SELECT>")
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def select_end(self):
|
|
|
|
self.handle_data("</SELECT>")
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def handle_option(self, value, selected):
|
|
|
|
self.handle_data("<OPTION>")
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def textarea_bgn(self, name, rows, cols):
|
|
|
|
self.handle_data("<TEXTAREA>")
|
|
|
|
self.start_pre([])
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def textarea_end(self):
|
|
|
|
self.end_pre()
|
|
|
|
self.handle_data("</TEXTAREA>")
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# --------- Top level elememts
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-04 01:23:30 -03:00
|
|
|
def start_html(self, attrs): pass
|
|
|
|
def end_html(self): pass
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-04 01:23:30 -03:00
|
|
|
def start_head(self, attrs): pass
|
|
|
|
def end_head(self): pass
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-04 01:23:30 -03:00
|
|
|
def start_body(self, attrs): pass
|
|
|
|
def end_body(self): pass
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# ------ Head elements
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-04 01:23:30 -03:00
|
|
|
def start_title(self, attrs):
|
1995-08-07 17:07:44 -03:00
|
|
|
self.save_bgn()
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-04 01:23:30 -03:00
|
|
|
def end_title(self):
|
1995-08-07 17:07:44 -03:00
|
|
|
self.title = self.save_end()
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def do_base(self, attrs):
|
|
|
|
for a, v in attrs:
|
|
|
|
if a == 'href':
|
|
|
|
self.base = v
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def do_isindex(self, attrs):
|
|
|
|
self.isindex = 1
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def do_link(self, attrs):
|
|
|
|
pass
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def do_meta(self, attrs):
|
|
|
|
pass
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def do_nextid(self, attrs): # Deprecated
|
|
|
|
pass
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# ------ Body elements
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# --- Headings
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_h1(self, attrs):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.push_font(('h1', 0, 1, 0))
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def end_h1(self):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.pop_font()
|
|
|
|
|
|
|
|
def start_h2(self, attrs):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.push_font(('h2', 0, 1, 0))
|
|
|
|
|
|
|
|
def end_h2(self):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.pop_font()
|
|
|
|
|
|
|
|
def start_h3(self, attrs):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.push_font(('h3', 0, 1, 0))
|
|
|
|
|
|
|
|
def end_h3(self):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.pop_font()
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_h4(self, attrs):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.push_font(('h4', 0, 1, 0))
|
|
|
|
|
|
|
|
def end_h4(self):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.pop_font()
|
|
|
|
|
|
|
|
def start_h5(self, attrs):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.push_font(('h5', 0, 1, 0))
|
|
|
|
|
|
|
|
def end_h5(self):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.pop_font()
|
|
|
|
|
|
|
|
def start_h6(self, attrs):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.push_font(('h6', 0, 1, 0))
|
|
|
|
|
|
|
|
def end_h6(self):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.pop_font()
|
|
|
|
|
|
|
|
# --- Block Structuring Elements
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-04 01:23:30 -03:00
|
|
|
def do_p(self, attrs):
|
1995-08-07 17:07:44 -03:00
|
|
|
self.formatter.end_paragraph(1)
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_pre(self, attrs):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1))
|
|
|
|
self.nofill = self.nofill + 1
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def end_pre(self):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.pop_font()
|
|
|
|
self.nofill = max(0, self.nofill - 1)
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_xmp(self, attrs):
|
|
|
|
self.start_pre(attrs)
|
|
|
|
self.setliteral('xmp') # Tell SGML parser
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def end_xmp(self):
|
|
|
|
self.end_pre()
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_listing(self, attrs):
|
|
|
|
self.start_pre(attrs)
|
|
|
|
self.setliteral('listing') # Tell SGML parser
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def end_listing(self):
|
|
|
|
self.end_pre()
|
1995-02-27 09:16:55 -04:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_address(self, attrs):
|
|
|
|
self.formatter.end_paragraph(0)
|
|
|
|
self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS))
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def end_address(self):
|
|
|
|
self.formatter.end_paragraph(0)
|
|
|
|
self.formatter.pop_font()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_blockquote(self, attrs):
|
|
|
|
self.formatter.end_paragraph(1)
|
|
|
|
self.formatter.push_margin('blockquote')
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def end_blockquote(self):
|
1995-08-08 23:31:00 -03:00
|
|
|
self.formatter.end_paragraph(1)
|
1995-08-07 17:07:44 -03:00
|
|
|
self.formatter.pop_margin()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# --- List Elements
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_ul(self, attrs):
|
|
|
|
self.formatter.end_paragraph(not self.list_stack)
|
|
|
|
self.formatter.push_margin('ul')
|
|
|
|
self.list_stack.append(['ul', '*', 0])
|
1995-08-04 01:23:30 -03:00
|
|
|
|
|
|
|
def end_ul(self):
|
1995-08-07 17:07:44 -03:00
|
|
|
if self.list_stack: del self.list_stack[-1]
|
|
|
|
self.formatter.end_paragraph(not self.list_stack)
|
|
|
|
self.formatter.pop_margin()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
|
|
|
def do_li(self, attrs):
|
1995-08-07 17:07:44 -03:00
|
|
|
self.formatter.end_paragraph(0)
|
|
|
|
if self.list_stack:
|
|
|
|
[dummy, label, counter] = top = self.list_stack[-1]
|
|
|
|
top[2] = counter = counter+1
|
|
|
|
else:
|
|
|
|
label, counter = '*', 0
|
|
|
|
self.formatter.add_label_data(label, counter)
|
|
|
|
|
|
|
|
def start_ol(self, attrs):
|
|
|
|
self.formatter.end_paragraph(not self.list_stack)
|
|
|
|
self.formatter.push_margin('ol')
|
|
|
|
label = '1.'
|
|
|
|
for a, v in attrs:
|
|
|
|
if a == 'type':
|
|
|
|
if len(v) == 1: v = v + '.'
|
|
|
|
label = v
|
|
|
|
self.list_stack.append(['ol', label, 0])
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def end_ol(self):
|
|
|
|
if self.list_stack: del self.list_stack[-1]
|
|
|
|
self.formatter.end_paragraph(not self.list_stack)
|
|
|
|
self.formatter.pop_margin()
|
|
|
|
|
|
|
|
def start_menu(self, attrs):
|
|
|
|
self.start_ul(attrs)
|
|
|
|
|
|
|
|
def end_menu(self):
|
|
|
|
self.end_ul()
|
|
|
|
|
|
|
|
def start_dir(self, attrs):
|
|
|
|
self.start_ul(attrs)
|
|
|
|
|
|
|
|
def end_dir(self):
|
|
|
|
self.end_ul()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
|
|
|
def start_dl(self, attrs):
|
1995-08-08 23:31:00 -03:00
|
|
|
self.formatter.end_paragraph(1)
|
1995-08-07 17:07:44 -03:00
|
|
|
self.list_stack.append(['dl', '', 0])
|
1995-08-04 01:23:30 -03:00
|
|
|
|
|
|
|
def end_dl(self):
|
1995-08-08 23:31:00 -03:00
|
|
|
self.ddpop(1)
|
1995-08-07 17:07:44 -03:00
|
|
|
if self.list_stack: del self.list_stack[-1]
|
1995-08-04 01:23:30 -03:00
|
|
|
|
|
|
|
def do_dt(self, attrs):
|
1995-08-07 17:07:44 -03:00
|
|
|
self.ddpop()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
|
|
|
def do_dd(self, attrs):
|
1995-08-07 17:07:44 -03:00
|
|
|
self.ddpop()
|
|
|
|
self.formatter.push_margin('dd')
|
|
|
|
self.list_stack.append(['dd', '', 0])
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-08 23:31:00 -03:00
|
|
|
def ddpop(self, bl=0):
|
|
|
|
self.formatter.end_paragraph(bl)
|
1995-08-07 17:07:44 -03:00
|
|
|
if self.list_stack:
|
|
|
|
if self.list_stack[-1][0] == 'dd':
|
|
|
|
del self.list_stack[-1]
|
|
|
|
self.formatter.pop_margin()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# --- Phrase Markup
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# Idiomatic Elements
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_cite(self, attrs): self.start_i(attrs)
|
|
|
|
def end_cite(self): self.end_i()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_code(self, attrs): self.start_tt(attrs)
|
|
|
|
def end_code(self): self.end_tt()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_em(self, attrs): self.start_i(attrs)
|
|
|
|
def end_em(self): self.end_i()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_kbd(self, attrs): self.start_tt(attrs)
|
|
|
|
def end_kbd(self): self.end_tt()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_samp(self, attrs): self.start_tt(attrs)
|
|
|
|
def end_samp(self): self.end_tt()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-08 23:31:00 -03:00
|
|
|
def start_strong(self, attrs): self.start_b(attrs)
|
|
|
|
def end_strong(self): self.end_b()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_var(self, attrs): self.start_i(attrs)
|
1995-08-08 23:31:00 -03:00
|
|
|
def end_var(self): self.end_i()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# Typographic Elements
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_i(self, attrs):
|
|
|
|
self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS))
|
|
|
|
def end_i(self):
|
|
|
|
self.formatter.pop_font()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_b(self, attrs):
|
|
|
|
self.formatter.push_font((AS_IS, AS_IS, 1, AS_IS))
|
|
|
|
def end_b(self):
|
|
|
|
self.formatter.pop_font()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_tt(self, attrs):
|
|
|
|
self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1))
|
|
|
|
def end_tt(self):
|
|
|
|
self.formatter.pop_font()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def start_a(self, attrs):
|
|
|
|
href = ''
|
|
|
|
name = ''
|
|
|
|
type = ''
|
|
|
|
for attrname, value in attrs:
|
|
|
|
if attrname == 'href':
|
|
|
|
href = value
|
|
|
|
if attrname == 'name':
|
|
|
|
name = value
|
|
|
|
if attrname == 'type':
|
|
|
|
type = string.lower(value)
|
|
|
|
self.anchor_bgn(href, name, type)
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def end_a(self):
|
|
|
|
self.anchor_end()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# --- Line Break
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def do_br(self, attrs):
|
|
|
|
self.formatter.add_line_break()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# --- Horizontal Rule
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def do_hr(self, attrs):
|
|
|
|
self.formatter.add_hor_rule()
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
# --- Image
|
1995-08-04 01:23:30 -03:00
|
|
|
|
1995-08-07 17:07:44 -03:00
|
|
|
def do_img(self, attrs):
|
|
|
|
align = ''
|
|
|
|
alt = '(image)'
|
|
|
|
ismap = ''
|
|
|
|
src = ''
|
|
|
|
for attrname, value in attrs:
|
|
|
|
if attrname == 'align':
|
|
|
|
align = value
|
|
|
|
if attrname == 'alt':
|
|
|
|
alt = value
|
|
|
|
if attrname == 'ismap':
|
|
|
|
ismap = value
|
|
|
|
if attrname == 'src':
|
|
|
|
src = value
|
|
|
|
self.handle_image(src, alt)
|
|
|
|
|
|
|
|
# ------ Forms
|
1995-08-04 01:23:30 -03:00
|
|
|
|
|
|
|
def start_form(self, attrs):
|
1995-08-07 17:07:44 -03:00
|
|
|
action = ''
|
|
|
|
method = ''
|
|
|
|
enctype = ''
|
|
|
|
for a, v in attrs:
|
|
|
|
if a == 'action': action = v
|
|
|
|
if a == 'method': method = v
|
|
|
|
if a == 'enctype': enctype = v
|
|
|
|
self.form_bgn(action, method, enctype)
|
1995-08-04 01:23:30 -03:00
|
|
|
|
|
|
|
def end_form(self):
|
1995-08-07 17:07:44 -03:00
|
|
|
self.form_end()
|
|
|
|
|
|
|
|
def do_input(self, attrs):
|
|
|
|
type = ''
|
|
|
|
options = {}
|
|
|
|
for a, v in attrs:
|
|
|
|
if a == 'type': type = string.lower(v)
|
|
|
|
else: options[a] = v
|
|
|
|
self.handle_input(type, options)
|
|
|
|
|
|
|
|
def start_select(self, attrs):
|
|
|
|
name = ''
|
|
|
|
size = 0
|
|
|
|
multiple = 0
|
|
|
|
for a, v in attrs:
|
|
|
|
if a == 'multiple': multiple = 1
|
|
|
|
if a == 'name': name = v
|
|
|
|
if a == 'size':
|
|
|
|
try: size = string.atoi(size)
|
|
|
|
except: pass
|
|
|
|
self.select_bgn(name, size, multiple)
|
|
|
|
|
|
|
|
def end_select(self):
|
|
|
|
self.select_end()
|
|
|
|
|
|
|
|
def do_option(self, attrs):
|
|
|
|
value = ''
|
|
|
|
selected = 1
|
|
|
|
for a, v in attrs:
|
|
|
|
if a == 'value': value = v
|
|
|
|
if a == 'selected': selected = 1
|
|
|
|
self.handle_option(value, selected)
|
|
|
|
|
|
|
|
def start_textarea(self, attrs):
|
|
|
|
name = ''
|
|
|
|
rows = 0
|
|
|
|
cols = 0
|
|
|
|
for a, v in attrs:
|
|
|
|
if a == 'name': name = v
|
|
|
|
if a == 'rows':
|
|
|
|
try: rows = string.atoi(v)
|
|
|
|
except: pass
|
|
|
|
if a == 'cols':
|
|
|
|
try: cols = string.atoi(v)
|
|
|
|
except: pass
|
|
|
|
self.textarea_bgn(name, rows, cols)
|
|
|
|
|
|
|
|
def end_textarea(self):
|
|
|
|
self.textarea_end()
|
|
|
|
|
|
|
|
# --- Really Old Unofficial Deprecated Stuff
|
|
|
|
|
|
|
|
def do_plaintext(self, attrs):
|
|
|
|
self.start_pre(attrs)
|
|
|
|
self.setnomoretags() # Tell SGML parser
|
1995-08-04 01:23:30 -03:00
|
|
|
|
|
|
|
# --- Unhandled tags
|
|
|
|
|
|
|
|
def unknown_starttag(self, tag, attrs):
|
1995-08-07 17:07:44 -03:00
|
|
|
pass
|
1995-08-04 01:23:30 -03:00
|
|
|
|
|
|
|
def unknown_endtag(self, tag):
|
1995-08-07 17:07:44 -03:00
|
|
|
pass
|
1995-02-27 09:16:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test():
|
1995-08-07 17:07:44 -03:00
|
|
|
import sys
|
1995-08-04 01:23:30 -03:00
|
|
|
file = 'test.html'
|
1995-08-07 17:07:44 -03:00
|
|
|
if sys.argv[1:]: file = sys.argv[1]
|
|
|
|
fp = open(file, 'r')
|
|
|
|
data = fp.read()
|
|
|
|
fp.close()
|
|
|
|
from formatter import DumbWriter, AbstractFormatter
|
|
|
|
w = DumbWriter()
|
|
|
|
f = AbstractFormatter(w)
|
|
|
|
p = HTMLParser(f)
|
1995-08-04 01:23:30 -03:00
|
|
|
p.feed(data)
|
|
|
|
p.close()
|
1995-02-27 09:16:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
1995-08-04 01:23:30 -03:00
|
|
|
test()
|