1 # A parser for SGML, using the derived class as static DTD.
3 # XXX This only supports those SGML features used by HTML.
5 # XXX There should be a way to distinguish between PCDATA (parsed
6 # character data -- the normal case), RCDATA (replaceable character
7 # data -- only char and entity references and end tags are special)
8 # and CDATA (character data -- only end tags are special).
15 # Regular expressions used for parsing
17 incomplete
= regex
.compile( \
18 '<!-?\|</[a-zA-Z][a-zA-Z0-9]*[ \t\n]*\|</?\|' + \
19 '&#[a-zA-Z0-9]*\|&[a-zA-Z][a-zA-Z0-9]*\|&')
20 entityref
= regex
.compile('&[a-zA-Z][a-zA-Z0-9]*[;.]')
21 charref
= regex
.compile('&#[a-zA-Z0-9]+;')
22 starttagopen
= regex
.compile('<[a-zA-Z]')
23 endtag
= regex
.compile('</[a-zA-Z][a-zA-Z0-9]*[ \t\n]*>')
24 commentopen
= regex
.compile('<!--')
27 # SGML parser base class -- find tags and call handler functions.
28 # Usage: p = SGMLParser(); p.feed(data); ...; p.close().
29 # The dtd is defined by deriving a class which defines methods
30 # with special names to handle tags: start_foo and end_foo to handle
31 # <foo> and </foo>, respectively, or do_foo to handle <foo> by itself.
32 # (Tags are converted to lower case for this purpose.) The data
33 # between tags is passed to the parser by calling self.handle_data()
34 # with some data as argument (the data may be split up in arbutrary
35 # chunks). Entity references are passed by calling
36 # self.handle_entityref() with the entity reference as argument.
40 # Interface -- initialize and reset this instance
44 # Interface -- reset this instance. Loses all unprocessed data
51 # For derived classes only -- enter literal mode (CDATA) till EOF
52 def setnomoretags(self
):
53 self
.nomoretags
= self
.literal
= 1
55 # For derived classes only -- enter literal mode (CDATA)
56 def setliteral(self
, *args
):
59 # Interface -- feed some data to the parser. Call this as
60 # often as you want, with as little or as much text as you
61 # want (may include '\n'). (This just saves the text, all the
62 # processing is done by process() or close().)
64 self
.rawdata
= self
.rawdata
+ data
67 # Interface -- handle the remaining data
71 # Internal -- handle data as far as reasonable. May leave state
72 # and data to be processed by a subsequent call. If 'end' is
73 # true, force handling all data as if followed by EOF marker.
74 def goahead(self
, end
):
75 rawdata
= self
.rawdata
80 self
.handle_data(rawdata
[i
:n
])
83 j
= incomplete
.search(rawdata
, i
)
85 if i
< j
: self
.handle_data(rawdata
[i
:j
])
89 if starttagopen
.match(rawdata
, i
) >= 0:
91 self
.handle_data(rawdata
[i
])
94 k
= self
.parse_starttag(i
)
98 k
= endtag
.match(rawdata
, i
)
101 self
.parse_endtag(rawdata
[i
:j
])
105 if commentopen
.match(rawdata
, i
) >= 0:
107 self
.handle_data(rawdata
[i
])
110 k
= self
.parse_comment(i
)
114 elif rawdata
[i
] == '&':
115 k
= charref
.match(rawdata
, i
)
118 self
.handle_charref(rawdata
[i
+2:j
-1])
121 k
= entityref
.match(rawdata
, i
)
124 self
.handle_entityref(rawdata
[i
+1:j
-1])
128 raise RuntimeError, 'neither < nor & ??'
129 # We get here only if incomplete matches but
131 k
= incomplete
.match(rawdata
, i
)
132 if k
< 0: raise RuntimeError, 'no incomplete match ??'
134 if j
== n
: break # Really incomplete
135 self
.handle_data(rawdata
[i
:j
])
139 self
.handle_data(rawdata
[i
:n
])
141 self
.rawdata
= rawdata
[i
:]
142 # XXX if end: check for empty stack
144 # Internal -- parse comment, return length or -1 if not ternimated
145 def parse_comment(self
, i
):
146 rawdata
= self
.rawdata
147 if rawdata
[i
:i
+4] <> '<!--':
148 raise RuntimeError, 'unexpected call to handle_comment'
150 j
= string
.index(rawdata
, '--', i
+4)
151 except string
.index_error
:
153 self
.handle_comment(rawdata
[i
+4: j
])
156 while j
< n
and rawdata
[j
] in ' \t\n': j
= j
+1
157 if j
== n
: return -1 # Wait for final '>'
158 if rawdata
[j
] == '>':
161 print '*** comment not terminated with >'
162 print repr(rawdata
[j
-5:j
]), '*!*', repr(rawdata
[j
:j
+5])
165 # Internal -- handle starttag, return length or -1 if not terminated
166 def parse_starttag(self
, i
):
167 rawdata
= self
.rawdata
169 j
= string
.index(rawdata
, '>', i
)
170 except string
.index_error
:
172 # Now parse the data between i+1 and j into a tag and attrs
174 tagfind
= regex
.compile('[a-zA-Z][a-zA-Z0-9]*')
175 attrfind
= regex
.compile( \
176 '[ \t\n]+\([a-zA-Z][a-zA-Z0-9]*\)' + \
177 '\([ \t\n]*=[ \t\n]*' + \
178 '\(\'[^\']*\';\|"[^"]*"\|[-a-zA-Z0-9./:+*%?!()_#]+\)\)?')
179 k
= tagfind
.match(rawdata
, i
+1)
181 raise RuntimeError, 'unexpected call to parse_starttag'
183 tag
= string
.lower(rawdata
[i
+1:k
])
185 l
= attrfind
.match(rawdata
, k
)
191 attrname
= rawdata
[a1
:b1
]
192 if '=' in rawdata
[k
:k
+l
]:
193 attrvalue
= rawdata
[a3
:b3
]
194 if attrvalue
[:1] == '\'' == attrvalue
[-1:] or \
195 attrvalue
[:1] == '"' == attrvalue
[-1:]:
196 attrvalue
= attrvalue
[1:-1]
199 attrs
.append(string
.lower(attrname
), attrvalue
)
203 method
= getattr(self
, 'start_' + tag
)
204 except AttributeError:
206 method
= getattr(self
, 'do_' + tag
)
207 except AttributeError:
208 self
.unknown_starttag(tag
, attrs
)
212 self
.stack
.append(tag
)
216 # Internal -- parse endtag
217 def parse_endtag(self
, data
):
218 if data
[:2] <> '</' or data
[-1:] <> '>':
219 raise RuntimeError, 'unexpected call to parse_endtag'
220 tag
= string
.lower(string
.strip(data
[2:-1]))
222 method
= getattr(self
, 'end_' + tag
)
223 except AttributeError:
224 self
.unknown_endtag(tag
)
226 if self
.stack
and self
.stack
[-1] == tag
:
229 print '*** Unbalanced </' + tag
+ '>'
230 print '*** Stack:', self
.stack
232 for i
in range(len(self
.stack
)):
233 if self
.stack
[i
] == tag
: found
= i
235 del self
.stack
[found
:]
238 # Example -- handle character reference, no need to override
239 def handle_charref(self
, name
):
241 n
= string
.atoi(name
)
242 except string
.atoi_error
:
243 self
.unknown_charref(name
)
245 if not 0 <= n
<= 255:
246 self
.unknown_charref(name
)
248 self
.handle_data(chr(n
))
250 # Definition of entities -- derived classes may override
252 {'lt': '<', 'gt': '>', 'amp': '&', 'quot': '"', 'apos': '\''}
254 # Example -- handle entity reference, no need to override
255 def handle_entityref(self
, name
):
256 table
= self
.__class
__.entitydefs
257 name
= string
.lower(name
)
258 if table
.has_key(name
):
259 self
.handle_data(table
[name
])
261 self
.unknown_entityref(name
)
264 # Example -- handle data, should be overridden
265 def handle_data(self
, data
):
268 # Example -- handle comment, could be overridden
269 def handle_comment(self
, data
):
272 # To be overridden -- handlers for unknown objects
273 def unknown_starttag(self
, tag
, attrs
): pass
274 def unknown_endtag(self
, tag
): pass
275 def unknown_charref(self
, ref
): pass
276 def unknown_entityref(self
, ref
): pass
279 class TestSGML(SGMLParser
):
281 def handle_data(self
, data
):
284 r
= r
[:35] + '...' + r
[-35:]
287 def handle_comment(self
, data
):
290 r
= r
[:32] + '...' + r
[-32:]
293 def unknown_starttag(self
, tag
, attrs
):
294 print 'start tag: <' + tag
,
295 for name
, value
in attrs
:
296 print name
+ '=' + '"' + value
+ '"',
299 def unknown_endtag(self
, tag
):
300 print 'end tag: </' + tag
+ '>'
302 def unknown_entityref(self
, ref
):
303 print '*** unknown entity ref: &' + ref
+ ';'
305 def unknown_charref(self
, ref
):
306 print '*** unknown char ref: &#' + ref
+ ';'