annotate Lib/sgmllib.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 357d4544085a
children 9614fdd81393
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # A parser for SGML, using the derived class as static DTD.
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # XXX This only supports those SGML features used by HTML.
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # XXX There should be a way to distinguish between PCDATA (parsed
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # character data -- the normal case), RCDATA (replaceable character
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # data -- only char and entity references and end tags are special)
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # and CDATA (character data -- only end tags are special).
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 import regex
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 import string
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # Regular expressions used for parsing
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
17 interesting = regex.compile('[&<]')
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
18 incomplete = regex.compile('&\([a-zA-Z][a-zA-Z0-9]*\|#[0-9]*\)?\|'
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
19 '<\([a-zA-Z][^<>]*\|'
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
20 '/\([a-zA-Z][^<>]*\)?\|'
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
21 '![^<>]*\)?')
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
22
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
23 entityref = regex.compile('&\([a-zA-Z][a-zA-Z0-9]*\)[^a-zA-Z0-9]')
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
24 charref = regex.compile('&#\([0-9]+\)[^0-9]')
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
25
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
26 starttagopen = regex.compile('<[>a-zA-Z]')
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
27 shorttagopen = regex.compile('<[a-zA-Z][a-zA-Z0-9]*/')
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
28 shorttag = regex.compile('<\([a-zA-Z][a-zA-Z0-9]*\)/\([^/]*\)/')
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
29 endtagopen = regex.compile('</[<>a-zA-Z]')
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
30 endbracket = regex.compile('[<>]')
2897
4d69fdee9f5d changed comment parsing
Guido van Rossum <guido@python.org>
parents: 2777
diff changeset
31 special = regex.compile('<![^<>]*>')
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 commentopen = regex.compile('<!--')
2897
4d69fdee9f5d changed comment parsing
Guido van Rossum <guido@python.org>
parents: 2777
diff changeset
33 commentclose = regex.compile('--[ \t\n]*>')
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
34 tagfind = regex.compile('[a-zA-Z][a-zA-Z0-9]*')
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
35 attrfind = regex.compile(
4743
357d4544085a (sgmllib.py): Partial acceptance of patch from David Leonard
Fred Drake <fdrake@acm.org>
parents: 3481
diff changeset
36 '[ \t\n]+\([a-zA-Z_][-.a-zA-Z_0-9]*\)'
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
37 '\([ \t\n]*=[ \t\n]*'
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
38 '\(\'[^\']*\'\|"[^"]*"\|[-a-zA-Z0-9./:+*%?!()_#=~]*\)\)?')
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 # SGML parser base class -- find tags and call handler functions.
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 # Usage: p = SGMLParser(); p.feed(data); ...; p.close().
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 # The dtd is defined by deriving a class which defines methods
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 # with special names to handle tags: start_foo and end_foo to handle
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 # <foo> and </foo>, respectively, or do_foo to handle <foo> by itself.
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 # (Tags are converted to lower case for this purpose.) The data
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 # between tags is passed to the parser by calling self.handle_data()
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 # with some data as argument (the data may be split up in arbutrary
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 # chunks). Entity references are passed by calling
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # self.handle_entityref() with the entity reference as argument.
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 class SGMLParser:
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
54 # Interface -- initialize and reset this instance
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
55 def __init__(self, verbose=0):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
56 self.verbose = verbose
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
57 self.reset()
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
59 # Interface -- reset this instance. Loses all unprocessed data
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
60 def reset(self):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
61 self.rawdata = ''
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
62 self.stack = []
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
63 self.lasttag = '???'
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
64 self.nomoretags = 0
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
65 self.literal = 0
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
67 # For derived classes only -- enter literal mode (CDATA) till EOF
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
68 def setnomoretags(self):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
69 self.nomoretags = self.literal = 1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
70
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
71 # For derived classes only -- enter literal mode (CDATA)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
72 def setliteral(self, *args):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
73 self.literal = 1
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
75 # Interface -- feed some data to the parser. Call this as
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
76 # often as you want, with as little or as much text as you
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
77 # want (may include '\n'). (This just saves the text, all the
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
78 # processing is done by goahead().)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
79 def feed(self, data):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
80 self.rawdata = self.rawdata + data
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
81 self.goahead(0)
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
83 # Interface -- handle the remaining data
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
84 def close(self):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
85 self.goahead(1)
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
87 # Internal -- handle data as far as reasonable. May leave state
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
88 # and data to be processed by a subsequent call. If 'end' is
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
89 # true, force handling all data as if followed by EOF marker.
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
90 def goahead(self, end):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
91 rawdata = self.rawdata
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
92 i = 0
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
93 n = len(rawdata)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
94 while i < n:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
95 if self.nomoretags:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
96 self.handle_data(rawdata[i:n])
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
97 i = n
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
98 break
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
99 j = interesting.search(rawdata, i)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
100 if j < 0: j = n
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
101 if i < j: self.handle_data(rawdata[i:j])
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
102 i = j
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
103 if i == n: break
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
104 if rawdata[i] == '<':
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
105 if starttagopen.match(rawdata, i) >= 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
106 if self.literal:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
107 self.handle_data(rawdata[i])
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
108 i = i+1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
109 continue
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
110 k = self.parse_starttag(i)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
111 if k < 0: break
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
112 i = k
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
113 continue
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
114 if endtagopen.match(rawdata, i) >= 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
115 k = self.parse_endtag(i)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
116 if k < 0: break
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
117 i = k
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
118 self.literal = 0
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
119 continue
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
120 if commentopen.match(rawdata, i) >= 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
121 if self.literal:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
122 self.handle_data(rawdata[i])
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
123 i = i+1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
124 continue
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
125 k = self.parse_comment(i)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
126 if k < 0: break
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
127 i = i+k
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
128 continue
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
129 k = special.match(rawdata, i)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
130 if k >= 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
131 if self.literal:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
132 self.handle_data(rawdata[i])
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
133 i = i+1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
134 continue
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
135 i = i+k
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
136 continue
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
137 elif rawdata[i] == '&':
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
138 k = charref.match(rawdata, i)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
139 if k >= 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
140 k = i+k
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
141 if rawdata[k-1] != ';': k = k-1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
142 name = charref.group(1)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
143 self.handle_charref(name)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
144 i = k
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
145 continue
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
146 k = entityref.match(rawdata, i)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
147 if k >= 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
148 k = i+k
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
149 if rawdata[k-1] != ';': k = k-1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
150 name = entityref.group(1)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
151 self.handle_entityref(name)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
152 i = k
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
153 continue
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
154 else:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
155 raise RuntimeError, 'neither < nor & ??'
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
156 # We get here only if incomplete matches but
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
157 # nothing else
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
158 k = incomplete.match(rawdata, i)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
159 if k < 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
160 self.handle_data(rawdata[i])
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
161 i = i+1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
162 continue
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
163 j = i+k
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
164 if j == n:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
165 break # Really incomplete
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
166 self.handle_data(rawdata[i:j])
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
167 i = j
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
168 # end while
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
169 if end and i < n:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
170 self.handle_data(rawdata[i:n])
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
171 i = n
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
172 self.rawdata = rawdata[i:]
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
173 # XXX if end: check for empty stack
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
174
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
175 # Internal -- parse comment, return length or -1 if not terminated
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
176 def parse_comment(self, i):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
177 rawdata = self.rawdata
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
178 if rawdata[i:i+4] <> '<!--':
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
179 raise RuntimeError, 'unexpected call to handle_comment'
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
180 j = commentclose.search(rawdata, i+4)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
181 if j < 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
182 return -1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
183 self.handle_comment(rawdata[i+4: j])
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
184 j = j+commentclose.match(rawdata, j)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
185 return j-i
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
187 # Internal -- handle starttag, return length or -1 if not terminated
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
188 def parse_starttag(self, i):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
189 rawdata = self.rawdata
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
190 if shorttagopen.match(rawdata, i) >= 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
191 # SGML shorthand: <tag/data/ == <tag>data</tag>
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
192 # XXX Can data contain &... (entity or char refs)?
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
193 # XXX Can data contain < or > (tag characters)?
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
194 # XXX Can there be whitespace before the first /?
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
195 j = shorttag.match(rawdata, i)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
196 if j < 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
197 return -1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
198 tag, data = shorttag.group(1, 2)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
199 tag = string.lower(tag)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
200 self.finish_shorttag(tag, data)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
201 k = i+j
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
202 if rawdata[k-1] == '<':
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
203 k = k-1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
204 return k
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
205 # XXX The following should skip matching quotes (' or ")
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
206 j = endbracket.search(rawdata, i+1)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
207 if j < 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
208 return -1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
209 # Now parse the data between i+1 and j into a tag and attrs
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
210 attrs = []
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
211 if rawdata[i:i+2] == '<>':
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
212 # SGML shorthand: <> == <last open tag seen>
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
213 k = j
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
214 tag = self.lasttag
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
215 else:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
216 k = tagfind.match(rawdata, i+1)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
217 if k < 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
218 raise RuntimeError, 'unexpected call to parse_starttag'
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
219 k = i+1+k
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
220 tag = string.lower(rawdata[i+1:k])
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
221 self.lasttag = tag
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
222 while k < j:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
223 l = attrfind.match(rawdata, k)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
224 if l < 0: break
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
225 attrname, rest, attrvalue = attrfind.group(1, 2, 3)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
226 if not rest:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
227 attrvalue = attrname
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
228 elif attrvalue[:1] == '\'' == attrvalue[-1:] or \
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
229 attrvalue[:1] == '"' == attrvalue[-1:]:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
230 attrvalue = attrvalue[1:-1]
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
231 attrs.append((string.lower(attrname), attrvalue))
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
232 k = k + l
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
233 if rawdata[j] == '>':
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
234 j = j+1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
235 self.finish_starttag(tag, attrs)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
236 return j
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
237
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
238 # Internal -- parse endtag
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
239 def parse_endtag(self, i):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
240 rawdata = self.rawdata
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
241 j = endbracket.search(rawdata, i+1)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
242 if j < 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
243 return -1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
244 tag = string.lower(string.strip(rawdata[i+2:j]))
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
245 if rawdata[j] == '>':
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
246 j = j+1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
247 self.finish_endtag(tag)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
248 return j
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
249
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
250 # Internal -- finish parsing of <tag/data/ (same as <tag>data</tag>)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
251 def finish_shorttag(self, tag, data):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
252 self.finish_starttag(tag, [])
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
253 self.handle_data(data)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
254 self.finish_endtag(tag)
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
256 # Internal -- finish processing of start tag
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
257 # Return -1 for unknown tag, 0 for open-only tag, 1 for balanced tag
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
258 def finish_starttag(self, tag, attrs):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
259 try:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
260 method = getattr(self, 'start_' + tag)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
261 except AttributeError:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
262 try:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
263 method = getattr(self, 'do_' + tag)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
264 except AttributeError:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
265 self.unknown_starttag(tag, attrs)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
266 return -1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
267 else:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
268 self.handle_starttag(tag, method, attrs)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
269 return 0
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
270 else:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
271 self.stack.append(tag)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
272 self.handle_starttag(tag, method, attrs)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
273 return 1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
274
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
275 # Internal -- finish processing of end tag
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
276 def finish_endtag(self, tag):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
277 if not tag:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
278 found = len(self.stack) - 1
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
279 if found < 0:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
280 self.unknown_endtag(tag)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
281 return
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
282 else:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
283 if tag not in self.stack:
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 try:
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
285 method = getattr(self, 'end_' + tag)
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 except AttributeError:
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
287 self.unknown_endtag(tag)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
288 return
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
289 found = len(self.stack)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
290 for i in range(found):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
291 if self.stack[i] == tag: found = i
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
292 while len(self.stack) > found:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
293 tag = self.stack[-1]
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
294 try:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
295 method = getattr(self, 'end_' + tag)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
296 except AttributeError:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
297 method = None
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
298 if method:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
299 self.handle_endtag(tag, method)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
300 else:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
301 self.unknown_endtag(tag)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
302 del self.stack[-1]
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
303
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
304 # Overridable -- handle start tag
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
305 def handle_starttag(self, tag, method, attrs):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
306 method(attrs)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
307
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
308 # Overridable -- handle end tag
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
309 def handle_endtag(self, tag, method):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
310 method()
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
311
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
312 # Example -- report an unbalanced </...> tag.
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
313 def report_unbalanced(self, tag):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
314 if self.verbose:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
315 print '*** Unbalanced </' + tag + '>'
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
316 print '*** Stack:', self.stack
2777
bf1ded75962d make reporting unbalanced tags an overridable method
Guido van Rossum <guido@python.org>
parents: 2476
diff changeset
317
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
318 # Example -- handle character reference, no need to override
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
319 def handle_charref(self, name):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
320 try:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
321 n = string.atoi(name)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
322 except string.atoi_error:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
323 self.unknown_charref(name)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
324 return
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
325 if not 0 <= n <= 255:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
326 self.unknown_charref(name)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
327 return
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
328 self.handle_data(chr(n))
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
329
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
330 # Definition of entities -- derived classes may override
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
331 entitydefs = \
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
332 {'lt': '<', 'gt': '>', 'amp': '&', 'quot': '"', 'apos': '\''}
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
333
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
334 # Example -- handle entity reference, no need to override
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
335 def handle_entityref(self, name):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
336 table = self.entitydefs
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
337 if table.has_key(name):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
338 self.handle_data(table[name])
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
339 else:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
340 self.unknown_entityref(name)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
341 return
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
342
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
343 # Example -- handle data, should be overridden
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
344 def handle_data(self, data):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
345 pass
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
346
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
347 # Example -- handle comment, could be overridden
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
348 def handle_comment(self, data):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
349 pass
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
350
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
351 # To be overridden -- handlers for unknown objects
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
352 def unknown_starttag(self, tag, attrs): pass
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
353 def unknown_endtag(self, tag): pass
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
354 def unknown_charref(self, ref): pass
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
355 def unknown_entityref(self, ref): pass
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
356
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
357
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
358 class TestSGMLParser(SGMLParser):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
359
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
360 def __init__(self, verbose=0):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
361 self.testdata = ""
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
362 SGMLParser.__init__(self, verbose)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
363
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
364 def handle_data(self, data):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
365 self.testdata = self.testdata + data
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
366 if len(`self.testdata`) >= 70:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
367 self.flush()
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
368
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
369 def flush(self):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
370 data = self.testdata
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
371 if data:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
372 self.testdata = ""
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
373 print 'data:', `data`
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
374
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
375 def handle_comment(self, data):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
376 self.flush()
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
377 r = `data`
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
378 if len(r) > 68:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
379 r = r[:32] + '...' + r[-32:]
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
380 print 'comment:', r
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
381
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
382 def unknown_starttag(self, tag, attrs):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
383 self.flush()
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
384 if not attrs:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
385 print 'start tag: <' + tag + '>'
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
386 else:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
387 print 'start tag: <' + tag,
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
388 for name, value in attrs:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
389 print name + '=' + '"' + value + '"',
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
390 print '>'
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
391
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
392 def unknown_endtag(self, tag):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
393 self.flush()
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
394 print 'end tag: </' + tag + '>'
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
395
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
396 def unknown_entityref(self, ref):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
397 self.flush()
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
398 print '*** unknown entity ref: &' + ref + ';'
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
399
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
400 def unknown_charref(self, ref):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
401 self.flush()
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
402 print '*** unknown char ref: &#' + ref + ';'
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
403
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
404 def close(self):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
405 SGMLParser.close(self)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
406 self.flush()
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
407
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
408
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
409 def test(args = None):
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
410 import sys
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
411
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
412 if not args:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
413 args = sys.argv[1:]
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
414
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
415 if args and args[0] == '-s':
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
416 args = args[1:]
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
417 klass = SGMLParser
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
418 else:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
419 klass = TestSGMLParser
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
420
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
421 if args:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
422 file = args[0]
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
423 else:
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 file = 'test.html'
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
425
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
426 if file == '-':
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
427 f = sys.stdin
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
428 else:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
429 try:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
430 f = open(file, 'r')
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
431 except IOError, msg:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
432 print file, ":", msg
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
433 sys.exit(1)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
434
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
435 data = f.read()
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
436 if f is not sys.stdin:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
437 f.close()
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
438
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
439 x = klass()
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
440 for c in data:
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
441 x.feed(c)
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
442 x.close()
2442
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
a5e95f9de900 added html parser and supporting cast
Guido van Rossum <guido@python.org>
parents:
diff changeset
444
2476
7089d4843407 remove redundant backslashes; some cosnetics
Guido van Rossum <guido@python.org>
parents: 2442
diff changeset
445 if __name__ == '__main__':
3481
e290faeb92c8 Reformatted with 4-space tab stops.
Guido van Rossum <guido@python.org>
parents: 3148
diff changeset
446 test()