Mercurial > genshi > genshi-test
comparison markup/output.py @ 109:2de3f9d84a1c
Reorder the conditional branches in the serializers so that the more common event kinds are on top.
author | cmlenz |
---|---|
date | Fri, 28 Jul 2006 17:34:18 +0000 |
parents | 334a338847af |
children | 93bbdcf9428b |
comparison
equal
deleted
inserted
replaced
108:9cf42fb6b21e | 109:2de3f9d84a1c |
---|---|
81 ns_mapping = {} | 81 ns_mapping = {} |
82 | 82 |
83 stream = _PushbackIterator(chain(self.preamble, stream)) | 83 stream = _PushbackIterator(chain(self.preamble, stream)) |
84 for kind, data, pos in stream: | 84 for kind, data, pos in stream: |
85 | 85 |
86 if kind is DOCTYPE: | 86 if kind is START: |
87 tag, attrib = data | |
88 | |
89 tagname = tag.localname | |
90 if tag.namespace: | |
91 try: | |
92 prefix = ns_mapping[tag.namespace] | |
93 if prefix: | |
94 tagname = '%s:%s' % (prefix, tag.localname) | |
95 except KeyError: | |
96 ns_attrib.append((QName('xmlns'), tag.namespace)) | |
97 buf = ['<%s' % tagname] | |
98 | |
99 if ns_attrib: | |
100 attrib.extend(ns_attrib) | |
101 ns_attrib = [] | |
102 for attr, value in attrib: | |
103 attrname = attr.localname | |
104 if attr.namespace: | |
105 prefix = ns_mapping.get(attr.namespace) | |
106 if prefix: | |
107 attrname = '%s:%s' % (prefix, attrname) | |
108 buf.append(' %s="%s"' % (attrname, escape(value))) | |
109 | |
110 kind, data, pos = stream.next() | |
111 if kind is END: | |
112 buf.append('/>') | |
113 else: | |
114 buf.append('>') | |
115 stream.pushback((kind, data, pos)) | |
116 | |
117 yield Markup(''.join(buf)) | |
118 | |
119 elif kind is END: | |
120 tag = data | |
121 tagname = tag.localname | |
122 if tag.namespace: | |
123 prefix = ns_mapping.get(tag.namespace) | |
124 if prefix: | |
125 tagname = '%s:%s' % (prefix, tag.localname) | |
126 yield Markup('</%s>' % tagname) | |
127 | |
128 elif kind is TEXT: | |
129 yield escape(data, quotes=False) | |
130 | |
131 elif kind is COMMENT: | |
132 yield Markup('<!--%s-->' % data) | |
133 | |
134 elif kind is DOCTYPE: | |
87 if not have_doctype: | 135 if not have_doctype: |
88 name, pubid, sysid = data | 136 name, pubid, sysid = data |
89 buf = ['<!DOCTYPE %s'] | 137 buf = ['<!DOCTYPE %s'] |
90 if pubid: | 138 if pubid: |
91 buf.append(' PUBLIC "%s"') | 139 buf.append(' PUBLIC "%s"') |
104 if not prefix: | 152 if not prefix: |
105 ns_attrib.append((QName('xmlns'), uri)) | 153 ns_attrib.append((QName('xmlns'), uri)) |
106 else: | 154 else: |
107 ns_attrib.append((QName('xmlns:%s' % prefix), uri)) | 155 ns_attrib.append((QName('xmlns:%s' % prefix), uri)) |
108 | 156 |
109 elif kind is START: | |
110 tag, attrib = data | |
111 | |
112 tagname = tag.localname | |
113 if tag.namespace: | |
114 try: | |
115 prefix = ns_mapping[tag.namespace] | |
116 if prefix: | |
117 tagname = '%s:%s' % (prefix, tag.localname) | |
118 except KeyError: | |
119 ns_attrib.append((QName('xmlns'), tag.namespace)) | |
120 buf = ['<%s' % tagname] | |
121 | |
122 if ns_attrib: | |
123 attrib.extend(ns_attrib) | |
124 ns_attrib = [] | |
125 for attr, value in attrib: | |
126 attrname = attr.localname | |
127 if attr.namespace: | |
128 prefix = ns_mapping.get(attr.namespace) | |
129 if prefix: | |
130 attrname = '%s:%s' % (prefix, attrname) | |
131 buf.append(' %s="%s"' % (attrname, escape(value))) | |
132 | |
133 kind, data, pos = stream.next() | |
134 if kind is END: | |
135 buf.append('/>') | |
136 else: | |
137 buf.append('>') | |
138 stream.pushback((kind, data, pos)) | |
139 | |
140 yield Markup(''.join(buf)) | |
141 | |
142 elif kind is END: | |
143 tag = data | |
144 tagname = tag.localname | |
145 if tag.namespace: | |
146 prefix = ns_mapping.get(tag.namespace) | |
147 if prefix: | |
148 tagname = '%s:%s' % (prefix, tag.localname) | |
149 yield Markup('</%s>' % tagname) | |
150 | |
151 elif kind is TEXT: | |
152 yield escape(data, quotes=False) | |
153 | |
154 elif kind is COMMENT: | |
155 yield Markup('<!--%s-->' % data) | |
156 | |
157 elif kind is PI: | 157 elif kind is PI: |
158 yield Markup('<?%s %s?>' % data) | 158 yield Markup('<?%s %s?>' % data) |
159 | 159 |
160 | 160 |
161 class XHTMLSerializer(XMLSerializer): | 161 class XHTMLSerializer(XMLSerializer): |
181 ns_mapping = {} | 181 ns_mapping = {} |
182 | 182 |
183 stream = _PushbackIterator(chain(self.preamble, stream)) | 183 stream = _PushbackIterator(chain(self.preamble, stream)) |
184 for kind, data, pos in stream: | 184 for kind, data, pos in stream: |
185 | 185 |
186 if kind is DOCTYPE: | 186 if kind is START: |
187 tag, attrib = data | |
188 if tag.namespace and tag not in self.NAMESPACE: | |
189 continue # not in the HTML namespace, so don't emit | |
190 buf = ['<', tag.localname] | |
191 | |
192 for attr, value in attrib: | |
193 if attr.namespace and attr not in self.NAMESPACE: | |
194 continue # not in the HTML namespace, so don't emit | |
195 if attr.localname in self._BOOLEAN_ATTRS: | |
196 if value: | |
197 buf.append(' %s="%s"' % (attr.localname, attr.localname)) | |
198 else: | |
199 buf.append(' %s="%s"' % (attr.localname, escape(value))) | |
200 | |
201 if tag.localname in self._EMPTY_ELEMS: | |
202 kind, data, pos = stream.next() | |
203 if kind is END: | |
204 buf.append(' />') | |
205 else: | |
206 buf.append('>') | |
207 stream.pushback((kind, data, pos)) | |
208 else: | |
209 buf.append('>') | |
210 | |
211 yield Markup(''.join(buf)) | |
212 | |
213 elif kind is END: | |
214 tag = data | |
215 if tag.namespace and tag not in self.NAMESPACE: | |
216 continue # not in the HTML namespace, so don't emit | |
217 yield Markup('</%s>' % tag.localname) | |
218 | |
219 elif kind is TEXT: | |
220 yield escape(data, quotes=False) | |
221 | |
222 elif kind is COMMENT: | |
223 yield Markup('<!--%s-->' % data) | |
224 | |
225 elif kind is DOCTYPE: | |
187 if not have_doctype: | 226 if not have_doctype: |
188 name, pubid, sysid = data | 227 name, pubid, sysid = data |
189 buf = ['<!DOCTYPE %s'] | 228 buf = ['<!DOCTYPE %s'] |
190 if pubid: | 229 if pubid: |
191 buf.append(' PUBLIC "%s"') | 230 buf.append(' PUBLIC "%s"') |
200 elif kind is START_NS: | 239 elif kind is START_NS: |
201 prefix, uri = data | 240 prefix, uri = data |
202 if uri not in ns_mapping: | 241 if uri not in ns_mapping: |
203 ns_mapping[uri] = prefix | 242 ns_mapping[uri] = prefix |
204 | 243 |
205 elif kind is START: | |
206 tag, attrib = data | |
207 if tag.namespace and tag not in self.NAMESPACE: | |
208 continue # not in the HTML namespace, so don't emit | |
209 buf = ['<', tag.localname] | |
210 | |
211 for attr, value in attrib: | |
212 if attr.namespace and attr not in self.NAMESPACE: | |
213 continue # not in the HTML namespace, so don't emit | |
214 if attr.localname in self._BOOLEAN_ATTRS: | |
215 if value: | |
216 buf.append(' %s="%s"' % (attr.localname, attr.localname)) | |
217 else: | |
218 buf.append(' %s="%s"' % (attr.localname, escape(value))) | |
219 | |
220 if tag.localname in self._EMPTY_ELEMS: | |
221 kind, data, pos = stream.next() | |
222 if kind is END: | |
223 buf.append(' />') | |
224 else: | |
225 buf.append('>') | |
226 stream.pushback((kind, data, pos)) | |
227 else: | |
228 buf.append('>') | |
229 | |
230 yield Markup(''.join(buf)) | |
231 | |
232 elif kind is END: | |
233 tag = data | |
234 if tag.namespace and tag not in self.NAMESPACE: | |
235 continue # not in the HTML namespace, so don't emit | |
236 yield Markup('</%s>' % tag.localname) | |
237 | |
238 elif kind is TEXT: | |
239 yield escape(data, quotes=False) | |
240 | |
241 elif kind is COMMENT: | |
242 yield Markup('<!--%s-->' % data) | |
243 | |
244 elif kind is PI: | 244 elif kind is PI: |
245 yield Markup('<?%s %s?>' % data) | 245 yield Markup('<?%s %s?>' % data) |
246 | 246 |
247 | 247 |
248 class HTMLSerializer(XHTMLSerializer): | 248 class HTMLSerializer(XHTMLSerializer): |
259 ns_mapping = {} | 259 ns_mapping = {} |
260 | 260 |
261 stream = _PushbackIterator(chain(self.preamble, stream)) | 261 stream = _PushbackIterator(chain(self.preamble, stream)) |
262 for kind, data, pos in stream: | 262 for kind, data, pos in stream: |
263 | 263 |
264 if kind is DOCTYPE: | 264 if kind is START: |
265 tag, attrib = data | |
266 if tag.namespace and tag not in self.NAMESPACE: | |
267 continue # not in the HTML namespace, so don't emit | |
268 buf = ['<', tag.localname] | |
269 | |
270 for attr, value in attrib: | |
271 if attr.namespace and attr not in self.NAMESPACE: | |
272 continue # not in the HTML namespace, so don't emit | |
273 if attr.localname in self._BOOLEAN_ATTRS: | |
274 if value: | |
275 buf.append(' %s' % attr.localname) | |
276 else: | |
277 buf.append(' %s="%s"' % (attr.localname, escape(value))) | |
278 | |
279 if tag.localname in self._EMPTY_ELEMS: | |
280 kind, data, pos = stream.next() | |
281 if kind is not END: | |
282 stream.pushback((kind, data, pos)) | |
283 | |
284 yield Markup(''.join(buf + ['>'])) | |
285 | |
286 elif kind is END: | |
287 tag = data | |
288 if tag.namespace and tag not in self.NAMESPACE: | |
289 continue # not in the HTML namespace, so don't emit | |
290 yield Markup('</%s>' % tag.localname) | |
291 | |
292 elif kind is TEXT: | |
293 yield escape(data, quotes=False) | |
294 | |
295 elif kind is COMMENT: | |
296 yield Markup('<!--%s-->' % data) | |
297 | |
298 elif kind is DOCTYPE: | |
265 if not have_doctype: | 299 if not have_doctype: |
266 name, pubid, sysid = data | 300 name, pubid, sysid = data |
267 buf = ['<!DOCTYPE %s'] | 301 buf = ['<!DOCTYPE %s'] |
268 if pubid: | 302 if pubid: |
269 buf.append(' PUBLIC "%s"') | 303 buf.append(' PUBLIC "%s"') |
278 elif kind is START_NS: | 312 elif kind is START_NS: |
279 prefix, uri = data | 313 prefix, uri = data |
280 if uri not in ns_mapping: | 314 if uri not in ns_mapping: |
281 ns_mapping[uri] = prefix | 315 ns_mapping[uri] = prefix |
282 | 316 |
283 elif kind is START: | |
284 tag, attrib = data | |
285 if tag.namespace and tag not in self.NAMESPACE: | |
286 continue # not in the HTML namespace, so don't emit | |
287 buf = ['<', tag.localname] | |
288 | |
289 for attr, value in attrib: | |
290 if attr.namespace and attr not in self.NAMESPACE: | |
291 continue # not in the HTML namespace, so don't emit | |
292 if attr.localname in self._BOOLEAN_ATTRS: | |
293 if value: | |
294 buf.append(' %s' % attr.localname) | |
295 else: | |
296 buf.append(' %s="%s"' % (attr.localname, escape(value))) | |
297 | |
298 if tag.localname in self._EMPTY_ELEMS: | |
299 kind, data, pos = stream.next() | |
300 if kind is not END: | |
301 stream.pushback((kind, data, pos)) | |
302 | |
303 yield Markup(''.join(buf + ['>'])) | |
304 | |
305 elif kind is END: | |
306 tag = data | |
307 if tag.namespace and tag not in self.NAMESPACE: | |
308 continue # not in the HTML namespace, so don't emit | |
309 yield Markup('</%s>' % tag.localname) | |
310 | |
311 elif kind is TEXT: | |
312 yield escape(data, quotes=False) | |
313 | |
314 elif kind is COMMENT: | |
315 yield Markup('<!--%s-->' % data) | |
316 | |
317 elif kind is PI: | 317 elif kind is PI: |
318 yield Markup('<?%s %s?>' % data) | 318 yield Markup('<?%s %s?>' % data) |
319 | 319 |
320 | 320 |
321 class _PushbackIterator(object): | 321 class _PushbackIterator(object): |