Mercurial > genshi > mirror
comparison markup/output.py @ 73:1da51d718391 trunk
Some more performance tweaks.
author | cmlenz |
---|---|
date | Wed, 12 Jul 2006 18:47:39 +0000 |
parents | c40a5dcd2b55 |
children | 4938c310d904 |
comparison
equal
deleted
inserted
replaced
72:ee092ccb3af1 | 73:1da51d718391 |
---|---|
18 try: | 18 try: |
19 frozenset | 19 frozenset |
20 except NameError: | 20 except NameError: |
21 from sets import ImmutableSet as frozenset | 21 from sets import ImmutableSet as frozenset |
22 | 22 |
23 from markup.core import Markup, Namespace, QName | 23 from markup.core import escape, Markup, Namespace, QName |
24 from markup.core import DOCTYPE, START, END, START_NS, END_NS, TEXT | 24 from markup.core import DOCTYPE, START, END, START_NS, END_NS, TEXT |
25 | 25 |
26 __all__ = ['Serializer', 'XMLSerializer', 'HTMLSerializer'] | 26 __all__ = ['Serializer', 'XMLSerializer', 'HTMLSerializer'] |
27 | 27 |
28 | 28 |
88 attrname = attr.localname | 88 attrname = attr.localname |
89 if attr.namespace: | 89 if attr.namespace: |
90 prefix = ns_mapping.get(attr.namespace) | 90 prefix = ns_mapping.get(attr.namespace) |
91 if prefix: | 91 if prefix: |
92 attrname = '%s:%s' % (prefix, attrname) | 92 attrname = '%s:%s' % (prefix, attrname) |
93 buf.append(' %s="%s"' % (attrname, Markup.escape(value))) | 93 buf.append(' %s="%s"' % (attrname, escape(value))) |
94 | 94 |
95 kind, data, pos = stream.next() | 95 kind, data, pos = stream.next() |
96 if kind is END: | 96 if kind is END: |
97 buf.append('/>') | 97 buf.append('/>') |
98 else: | 98 else: |
109 if prefix: | 109 if prefix: |
110 tagname = '%s:%s' % (prefix, tag.localname) | 110 tagname = '%s:%s' % (prefix, tag.localname) |
111 yield Markup('</%s>' % tagname) | 111 yield Markup('</%s>' % tagname) |
112 | 112 |
113 elif kind is TEXT: | 113 elif kind is TEXT: |
114 yield Markup.escape(data, quotes=False) | 114 yield escape(data, quotes=False) |
115 | 115 |
116 | 116 |
117 class HTMLSerializer(Serializer): | 117 class HTMLSerializer(Serializer): |
118 """Produces HTML text from an event stream. | 118 """Produces HTML text from an event stream. |
119 | 119 |
156 continue # not in the HTML namespace, so don't emit | 156 continue # not in the HTML namespace, so don't emit |
157 if attr.localname in self._BOOLEAN_ATTRS: | 157 if attr.localname in self._BOOLEAN_ATTRS: |
158 if value: | 158 if value: |
159 buf.append(' %s' % attr.localname) | 159 buf.append(' %s' % attr.localname) |
160 else: | 160 else: |
161 buf.append(' %s="%s"' % (attr.localname, | 161 buf.append(' %s="%s"' % (attr.localname, escape(value))) |
162 Markup.escape(value))) | |
163 | 162 |
164 if tag.localname in self._EMPTY_ELEMS: | 163 if tag.localname in self._EMPTY_ELEMS: |
165 kind, data, pos = stream.next() | 164 kind, data, pos = stream.next() |
166 if kind is not END: | 165 if kind is not END: |
167 stream.pushback((kind, data, pos)) | 166 stream.pushback((kind, data, pos)) |
173 if tag.namespace and tag not in self.NAMESPACE: | 172 if tag.namespace and tag not in self.NAMESPACE: |
174 continue # not in the HTML namespace, so don't emit | 173 continue # not in the HTML namespace, so don't emit |
175 yield Markup('</%s>' % tag.localname) | 174 yield Markup('</%s>' % tag.localname) |
176 | 175 |
177 elif kind is TEXT: | 176 elif kind is TEXT: |
178 yield Markup.escape(data, quotes=False) | 177 yield escape(data, quotes=False) |
179 | 178 |
180 | 179 |
181 class _PushbackIterator(object): | 180 class _PushbackIterator(object): |
182 """A simple wrapper for iterators that allows pushing items back on the | 181 """A simple wrapper for iterators that allows pushing items back on the |
183 queue via the `pushback()` method. | 182 queue via the `pushback()` method. |