Mercurial > genshi > genshi-test
annotate markup/output.py @ 108:9cf42fb6b21e
Minor improvements to transform example.
author | cmlenz |
---|---|
date | Fri, 28 Jul 2006 17:19:44 +0000 |
parents | 334a338847af |
children | 2de3f9d84a1c |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
66
822089ae65ce
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
27
diff
changeset
|
3 # Copyright (C) 2006 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
66
822089ae65ce
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
27
diff
changeset
|
8 # are also available at http://markup.edgewall.org/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
66
822089ae65ce
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
27
diff
changeset
|
12 # history and logs, available at http://markup.edgewall.org/log/. |
1 | 13 |
14 """This module provides different kinds of serialization methods for XML event | |
15 streams. | |
16 """ | |
17 | |
18 try: | |
19 frozenset | |
20 except NameError: | |
21 from sets import ImmutableSet as frozenset | |
85 | 22 from itertools import chain |
1 | 23 |
73 | 24 from markup.core import escape, Markup, Namespace, QName |
105
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
25 from markup.core import DOCTYPE, START, END, START_NS, END_NS, TEXT, COMMENT, PI |
1 | 26 |
27 __all__ = ['Serializer', 'XMLSerializer', 'HTMLSerializer'] | |
28 | |
29 | |
30 class Serializer(object): | |
31 """Base class for serializers.""" | |
32 | |
33 def serialize(self, stream): | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
20
diff
changeset
|
34 """Must be implemented by concrete subclasses to serialize the given |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
20
diff
changeset
|
35 stream. |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
20
diff
changeset
|
36 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
20
diff
changeset
|
37 This method must be implemented as a generator, producing the |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
20
diff
changeset
|
38 serialized output incrementally as unicode strings. |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
20
diff
changeset
|
39 """ |
1 | 40 raise NotImplementedError |
41 | |
42 | |
85 | 43 class DocType(object): |
44 """Defines a number of commonly used DOCTYPE declarations as constants.""" | |
45 | |
46 HTML_STRICT = ('html', '-//W3C//DTD HTML 4.01//EN', | |
47 'http://www.w3.org/TR/html4/strict.dtd') | |
48 HTML_TRANSITIONAL = ('html', '-//W3C//DTD HTML 4.01 Transitional//EN', | |
49 'http://www.w3.org/TR/html4/loose.dtd') | |
50 HTML = HTML_STRICT | |
51 | |
52 XHTML_STRICT = ('html', '-//W3C//DTD XHTML 1.0 Strict//EN', | |
53 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd') | |
54 XHTML_TRANSITIONAL = ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN', | |
55 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd') | |
56 XHTML = XHTML_STRICT | |
57 | |
58 | |
1 | 59 class XMLSerializer(Serializer): |
60 """Produces XML text from an event stream. | |
61 | |
62 >>> from markup.builder import tag | |
20 | 63 >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True)) |
1 | 64 >>> print ''.join(XMLSerializer().serialize(elem.generate())) |
65 <div><a href="foo"/><br/><hr noshade="True"/></div> | |
66 """ | |
85 | 67 def __init__(self, doctype=None): |
68 """Initialize the XML serializer. | |
69 | |
70 @param doctype: a `(name, pubid, sysid)` tuple that represents the | |
71 DOCTYPE declaration that should be included at the top of the | |
72 generated output | |
73 """ | |
74 self.preamble = [] | |
75 if doctype: | |
76 self.preamble.append((DOCTYPE, doctype, (None, -1, -1))) | |
1 | 77 |
78 def serialize(self, stream): | |
85 | 79 have_doctype = False |
1 | 80 ns_attrib = [] |
81 ns_mapping = {} | |
82 | |
85 | 83 stream = _PushbackIterator(chain(self.preamble, stream)) |
1 | 84 for kind, data, pos in stream: |
85 | |
69 | 86 if kind is DOCTYPE: |
85 | 87 if not have_doctype: |
88 name, pubid, sysid = data | |
89 buf = ['<!DOCTYPE %s'] | |
90 if pubid: | |
91 buf.append(' PUBLIC "%s"') | |
92 elif sysid: | |
93 buf.append(' SYSTEM') | |
94 if sysid: | |
95 buf.append(' "%s"') | |
96 buf.append('>\n') | |
97 yield Markup(''.join(buf), *filter(None, data)) | |
98 have_doctype = True | |
1 | 99 |
69 | 100 elif kind is START_NS: |
1 | 101 prefix, uri = data |
102 if uri not in ns_mapping: | |
103 ns_mapping[uri] = prefix | |
104 if not prefix: | |
105 ns_attrib.append((QName('xmlns'), uri)) | |
106 else: | |
107 ns_attrib.append((QName('xmlns:%s' % prefix), uri)) | |
108 | |
69 | 109 elif kind is START: |
1 | 110 tag, attrib = data |
111 | |
112 tagname = tag.localname | |
113 if tag.namespace: | |
114 try: | |
115 prefix = ns_mapping[tag.namespace] | |
116 if prefix: | |
69 | 117 tagname = '%s:%s' % (prefix, tag.localname) |
1 | 118 except KeyError: |
119 ns_attrib.append((QName('xmlns'), tag.namespace)) | |
69 | 120 buf = ['<%s' % tagname] |
1 | 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: | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
20
diff
changeset
|
128 prefix = ns_mapping.get(attr.namespace) |
1 | 129 if prefix: |
69 | 130 attrname = '%s:%s' % (prefix, attrname) |
73 | 131 buf.append(' %s="%s"' % (attrname, escape(value))) |
1 | 132 |
133 kind, data, pos = stream.next() | |
69 | 134 if kind is END: |
1 | 135 buf.append('/>') |
136 else: | |
137 buf.append('>') | |
138 stream.pushback((kind, data, pos)) | |
139 | |
140 yield Markup(''.join(buf)) | |
141 | |
69 | 142 elif kind is END: |
1 | 143 tag = data |
144 tagname = tag.localname | |
145 if tag.namespace: | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
20
diff
changeset
|
146 prefix = ns_mapping.get(tag.namespace) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
20
diff
changeset
|
147 if prefix: |
69 | 148 tagname = '%s:%s' % (prefix, tag.localname) |
1 | 149 yield Markup('</%s>' % tagname) |
150 | |
69 | 151 elif kind is TEXT: |
73 | 152 yield escape(data, quotes=False) |
1 | 153 |
89
d4c7617900e3
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
85
diff
changeset
|
154 elif kind is COMMENT: |
d4c7617900e3
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
85
diff
changeset
|
155 yield Markup('<!--%s-->' % data) |
d4c7617900e3
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
85
diff
changeset
|
156 |
105
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
157 elif kind is PI: |
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
158 yield Markup('<?%s %s?>' % data) |
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
159 |
1 | 160 |
96
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
161 class XHTMLSerializer(XMLSerializer): |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
162 """Produces XHTML text from an event stream. |
1 | 163 |
164 >>> from markup.builder import tag | |
20 | 165 >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True)) |
96
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
166 >>> print ''.join(XHTMLSerializer().serialize(elem.generate())) |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
167 <div><a href="foo"></a><br /><hr noshade="noshade" /></div> |
1 | 168 """ |
169 | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
1
diff
changeset
|
170 NAMESPACE = Namespace('http://www.w3.org/1999/xhtml') |
1 | 171 |
172 _EMPTY_ELEMS = frozenset(['area', 'base', 'basefont', 'br', 'col', 'frame', | |
173 'hr', 'img', 'input', 'isindex', 'link', 'meta', | |
174 'param']) | |
175 _BOOLEAN_ATTRS = frozenset(['selected', 'checked', 'compact', 'declare', | |
176 'defer', 'disabled', 'ismap', 'multiple', | |
177 'nohref', 'noresize', 'noshade', 'nowrap']) | |
178 | |
179 def serialize(self, stream): | |
85 | 180 have_doctype = False |
1 | 181 ns_mapping = {} |
182 | |
85 | 183 stream = _PushbackIterator(chain(self.preamble, stream)) |
1 | 184 for kind, data, pos in stream: |
185 | |
69 | 186 if kind is DOCTYPE: |
85 | 187 if not have_doctype: |
188 name, pubid, sysid = data | |
189 buf = ['<!DOCTYPE %s'] | |
190 if pubid: | |
191 buf.append(' PUBLIC "%s"') | |
192 elif sysid: | |
193 buf.append(' SYSTEM') | |
194 if sysid: | |
195 buf.append(' "%s"') | |
196 buf.append('>\n') | |
197 yield Markup(''.join(buf), *filter(None, data)) | |
198 have_doctype = True | |
1 | 199 |
69 | 200 elif kind is START_NS: |
1 | 201 prefix, uri = data |
202 if uri not in ns_mapping: | |
203 ns_mapping[uri] = prefix | |
204 | |
69 | 205 elif kind is START: |
1 | 206 tag, attrib = data |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
1
diff
changeset
|
207 if tag.namespace and tag not in self.NAMESPACE: |
1 | 208 continue # not in the HTML namespace, so don't emit |
209 buf = ['<', tag.localname] | |
96
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
210 |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
211 for attr, value in attrib: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
212 if attr.namespace and attr not in self.NAMESPACE: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
213 continue # not in the HTML namespace, so don't emit |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
214 if attr.localname in self._BOOLEAN_ATTRS: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
215 if value: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
216 buf.append(' %s="%s"' % (attr.localname, attr.localname)) |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
217 else: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
218 buf.append(' %s="%s"' % (attr.localname, escape(value))) |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
219 |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
220 if tag.localname in self._EMPTY_ELEMS: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
221 kind, data, pos = stream.next() |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
222 if kind is END: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
223 buf.append(' />') |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
224 else: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
225 buf.append('>') |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
226 stream.pushback((kind, data, pos)) |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
227 else: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
228 buf.append('>') |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
229 |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
230 yield Markup(''.join(buf)) |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
231 |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
232 elif kind is END: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
233 tag = data |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
234 if tag.namespace and tag not in self.NAMESPACE: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
235 continue # not in the HTML namespace, so don't emit |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
236 yield Markup('</%s>' % tag.localname) |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
237 |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
238 elif kind is TEXT: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
239 yield escape(data, quotes=False) |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
240 |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
241 elif kind is COMMENT: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
242 yield Markup('<!--%s-->' % data) |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
243 |
105
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
244 elif kind is PI: |
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
245 yield Markup('<?%s %s?>' % data) |
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
246 |
96
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
247 |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
248 class HTMLSerializer(XHTMLSerializer): |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
249 """Produces HTML text from an event stream. |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
250 |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
251 >>> from markup.builder import tag |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
252 >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True)) |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
253 >>> print ''.join(HTMLSerializer().serialize(elem.generate())) |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
254 <div><a href="foo"></a><br><hr noshade></div> |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
255 """ |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
256 |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
257 def serialize(self, stream): |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
258 have_doctype = False |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
259 ns_mapping = {} |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
260 |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
261 stream = _PushbackIterator(chain(self.preamble, stream)) |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
262 for kind, data, pos in stream: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
263 |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
264 if kind is DOCTYPE: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
265 if not have_doctype: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
266 name, pubid, sysid = data |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
267 buf = ['<!DOCTYPE %s'] |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
268 if pubid: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
269 buf.append(' PUBLIC "%s"') |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
270 elif sysid: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
271 buf.append(' SYSTEM') |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
272 if sysid: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
273 buf.append(' "%s"') |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
274 buf.append('>\n') |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
275 yield Markup(''.join(buf), *filter(None, data)) |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
276 have_doctype = True |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
277 |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
278 elif kind is START_NS: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
279 prefix, uri = data |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
280 if uri not in ns_mapping: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
281 ns_mapping[uri] = prefix |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
282 |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
283 elif kind is START: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
284 tag, attrib = data |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
285 if tag.namespace and tag not in self.NAMESPACE: |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
286 continue # not in the HTML namespace, so don't emit |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
287 buf = ['<', tag.localname] |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
288 |
1 | 289 for attr, value in attrib: |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
1
diff
changeset
|
290 if attr.namespace and attr not in self.NAMESPACE: |
1 | 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: | |
73 | 296 buf.append(' %s="%s"' % (attr.localname, escape(value))) |
1 | 297 |
298 if tag.localname in self._EMPTY_ELEMS: | |
299 kind, data, pos = stream.next() | |
69 | 300 if kind is not END: |
1 | 301 stream.pushback((kind, data, pos)) |
302 | |
303 yield Markup(''.join(buf + ['>'])) | |
304 | |
69 | 305 elif kind is END: |
1 | 306 tag = data |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
1
diff
changeset
|
307 if tag.namespace and tag not in self.NAMESPACE: |
1 | 308 continue # not in the HTML namespace, so don't emit |
309 yield Markup('</%s>' % tag.localname) | |
310 | |
69 | 311 elif kind is TEXT: |
73 | 312 yield escape(data, quotes=False) |
1 | 313 |
89
d4c7617900e3
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
85
diff
changeset
|
314 elif kind is COMMENT: |
d4c7617900e3
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
85
diff
changeset
|
315 yield Markup('<!--%s-->' % data) |
d4c7617900e3
Support comments in templates that are not included in the output, in the same way Kid does: if the comment text starts with a `!` character, it is stripped from the output.
cmlenz
parents:
85
diff
changeset
|
316 |
105
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
317 elif kind is PI: |
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
318 yield Markup('<?%s %s?>' % data) |
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
319 |
1 | 320 |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
20
diff
changeset
|
321 class _PushbackIterator(object): |
1 | 322 """A simple wrapper for iterators that allows pushing items back on the |
323 queue via the `pushback()` method. | |
324 | |
325 That can effectively be used to peek at the next item.""" | |
326 __slots__ = ['iterable', 'buf'] | |
327 | |
328 def __init__(self, iterable): | |
329 self.iterable = iter(iterable) | |
330 self.buf = [] | |
331 | |
332 def __iter__(self): | |
333 return self | |
334 | |
335 def next(self): | |
336 if self.buf: | |
337 return self.buf.pop(0) | |
338 return self.iterable.next() | |
339 | |
340 def pushback(self, item): | |
341 self.buf.append(item) |