annotate markup/output.py @ 96:fa08aef181a2 trunk

Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
author cmlenz
date Fri, 21 Jul 2006 11:39:32 +0000
parents 80386d62814f
children 71f3db26eecb
rev   line source
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
2 #
66
59eb24184e9c Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 27
diff changeset
3 # Copyright (C) 2006 Edgewall Software
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
5 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
66
59eb24184e9c 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
9 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
66
59eb24184e9c 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
13
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
14 """This module provides different kinds of serialization methods for XML event
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
15 streams.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
16 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
17
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
18 try:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
19 frozenset
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
20 except NameError:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
21 from sets import ImmutableSet as frozenset
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
22 from itertools import chain
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
23
73
1da51d718391 Some more performance tweaks.
cmlenz
parents: 69
diff changeset
24 from markup.core import escape, Markup, Namespace, QName
89
80386d62814f 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
25 from markup.core import DOCTYPE, START, END, START_NS, END_NS, TEXT, COMMENT
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
26
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
27 __all__ = ['Serializer', 'XMLSerializer', 'HTMLSerializer']
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
28
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
29
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
30 class Serializer(object):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
31 """Base class for serializers."""
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
32
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
33 def serialize(self, stream):
26
3c1a022be04c * 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
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
35 stream.
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
36
3c1a022be04c * 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
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
38 serialized output incrementally as unicode strings.
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
39 """
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
40 raise NotImplementedError
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
41
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
42
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
43 class DocType(object):
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
44 """Defines a number of commonly used DOCTYPE declarations as constants."""
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
45
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
46 HTML_STRICT = ('html', '-//W3C//DTD HTML 4.01//EN',
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
47 'http://www.w3.org/TR/html4/strict.dtd')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
48 HTML_TRANSITIONAL = ('html', '-//W3C//DTD HTML 4.01 Transitional//EN',
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
49 'http://www.w3.org/TR/html4/loose.dtd')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
50 HTML = HTML_STRICT
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
51
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
52 XHTML_STRICT = ('html', '-//W3C//DTD XHTML 1.0 Strict//EN',
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
53 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
54 XHTML_TRANSITIONAL = ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
55 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
56 XHTML = XHTML_STRICT
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
57
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
58
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
59 class XMLSerializer(Serializer):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
60 """Produces XML text from an event stream.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
61
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
62 >>> from markup.builder import tag
20
cc92d74ce9e5 Fix tests broken in [20].
cmlenz
parents: 19
diff changeset
63 >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
64 >>> print ''.join(XMLSerializer().serialize(elem.generate()))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
65 <div><a href="foo"/><br/><hr noshade="True"/></div>
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
66 """
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
67 def __init__(self, doctype=None):
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
68 """Initialize the XML serializer.
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
69
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
70 @param doctype: a `(name, pubid, sysid)` tuple that represents the
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
71 DOCTYPE declaration that should be included at the top of the
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
72 generated output
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
73 """
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
74 self.preamble = []
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
75 if doctype:
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
76 self.preamble.append((DOCTYPE, doctype, (None, -1, -1)))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
77
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
78 def serialize(self, stream):
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
79 have_doctype = False
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
80 ns_attrib = []
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
81 ns_mapping = {}
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
82
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
83 stream = _PushbackIterator(chain(self.preamble, stream))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
84 for kind, data, pos in stream:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
85
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
86 if kind is DOCTYPE:
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
87 if not have_doctype:
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
88 name, pubid, sysid = data
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
89 buf = ['<!DOCTYPE %s']
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
90 if pubid:
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
91 buf.append(' PUBLIC "%s"')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
92 elif sysid:
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
93 buf.append(' SYSTEM')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
94 if sysid:
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
95 buf.append(' "%s"')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
96 buf.append('>\n')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
97 yield Markup(''.join(buf), *filter(None, data))
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
98 have_doctype = True
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
99
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
100 elif kind is START_NS:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
101 prefix, uri = data
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
102 if uri not in ns_mapping:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
103 ns_mapping[uri] = prefix
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
104 if not prefix:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
105 ns_attrib.append((QName('xmlns'), uri))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
106 else:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
107 ns_attrib.append((QName('xmlns:%s' % prefix), uri))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
108
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
109 elif kind is START:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
110 tag, attrib = data
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
111
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
112 tagname = tag.localname
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
113 if tag.namespace:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
114 try:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
115 prefix = ns_mapping[tag.namespace]
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
116 if prefix:
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
117 tagname = '%s:%s' % (prefix, tag.localname)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
118 except KeyError:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
119 ns_attrib.append((QName('xmlns'), tag.namespace))
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
120 buf = ['<%s' % tagname]
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
121
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
122 if ns_attrib:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
123 attrib.extend(ns_attrib)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
124 ns_attrib = []
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
125 for attr, value in attrib:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
126 attrname = attr.localname
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
127 if attr.namespace:
26
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
128 prefix = ns_mapping.get(attr.namespace)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
129 if prefix:
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
130 attrname = '%s:%s' % (prefix, attrname)
73
1da51d718391 Some more performance tweaks.
cmlenz
parents: 69
diff changeset
131 buf.append(' %s="%s"' % (attrname, escape(value)))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
132
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
133 kind, data, pos = stream.next()
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
134 if kind is END:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
135 buf.append('/>')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
136 else:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
137 buf.append('>')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
138 stream.pushback((kind, data, pos))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
139
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
140 yield Markup(''.join(buf))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
141
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
142 elif kind is END:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
143 tag = data
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
144 tagname = tag.localname
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
145 if tag.namespace:
26
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
146 prefix = ns_mapping.get(tag.namespace)
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
147 if prefix:
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
148 tagname = '%s:%s' % (prefix, tag.localname)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
149 yield Markup('</%s>' % tagname)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
150
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
151 elif kind is TEXT:
73
1da51d718391 Some more performance tweaks.
cmlenz
parents: 69
diff changeset
152 yield escape(data, quotes=False)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
153
89
80386d62814f 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:
80386d62814f 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)
80386d62814f 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
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
157
96
fa08aef181a2 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
158 class XHTMLSerializer(XMLSerializer):
fa08aef181a2 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
159 """Produces XHTML text from an event stream.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
160
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
161 >>> from markup.builder import tag
20
cc92d74ce9e5 Fix tests broken in [20].
cmlenz
parents: 19
diff changeset
162 >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True))
96
fa08aef181a2 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
163 >>> print ''.join(XHTMLSerializer().serialize(elem.generate()))
fa08aef181a2 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
164 <div><a href="foo"></a><br /><hr noshade="noshade" /></div>
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
165 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
166
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 1
diff changeset
167 NAMESPACE = Namespace('http://www.w3.org/1999/xhtml')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
168
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
169 _EMPTY_ELEMS = frozenset(['area', 'base', 'basefont', 'br', 'col', 'frame',
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
170 'hr', 'img', 'input', 'isindex', 'link', 'meta',
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
171 'param'])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
172 _BOOLEAN_ATTRS = frozenset(['selected', 'checked', 'compact', 'declare',
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
173 'defer', 'disabled', 'ismap', 'multiple',
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
174 'nohref', 'noresize', 'noshade', 'nowrap'])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
175
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
176 def serialize(self, stream):
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
177 have_doctype = False
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
178 ns_mapping = {}
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
179
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
180 stream = _PushbackIterator(chain(self.preamble, stream))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
181 for kind, data, pos in stream:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
182
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
183 if kind is DOCTYPE:
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
184 if not have_doctype:
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
185 name, pubid, sysid = data
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
186 buf = ['<!DOCTYPE %s']
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
187 if pubid:
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
188 buf.append(' PUBLIC "%s"')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
189 elif sysid:
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
190 buf.append(' SYSTEM')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
191 if sysid:
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
192 buf.append(' "%s"')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
193 buf.append('>\n')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
194 yield Markup(''.join(buf), *filter(None, data))
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
195 have_doctype = True
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
196
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
197 elif kind is START_NS:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
198 prefix, uri = data
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
199 if uri not in ns_mapping:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
200 ns_mapping[uri] = prefix
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
201
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
202 elif kind is START:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
203 tag, attrib = data
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 1
diff changeset
204 if tag.namespace and tag not in self.NAMESPACE:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
205 continue # not in the HTML namespace, so don't emit
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
206 buf = ['<', tag.localname]
96
fa08aef181a2 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
207
fa08aef181a2 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
208 for attr, value in attrib:
fa08aef181a2 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
209 if attr.namespace and attr not in self.NAMESPACE:
fa08aef181a2 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 continue # not in the HTML namespace, so don't emit
fa08aef181a2 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 if attr.localname in self._BOOLEAN_ATTRS:
fa08aef181a2 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 value:
fa08aef181a2 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 buf.append(' %s="%s"' % (attr.localname, attr.localname))
fa08aef181a2 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 else:
fa08aef181a2 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 buf.append(' %s="%s"' % (attr.localname, escape(value)))
fa08aef181a2 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
fa08aef181a2 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 if tag.localname in self._EMPTY_ELEMS:
fa08aef181a2 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 kind, data, pos = stream.next()
fa08aef181a2 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 if kind is END:
fa08aef181a2 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 buf.append(' />')
fa08aef181a2 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 else:
fa08aef181a2 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 buf.append('>')
fa08aef181a2 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 stream.pushback((kind, data, pos))
fa08aef181a2 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:
fa08aef181a2 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('>')
fa08aef181a2 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
fa08aef181a2 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 yield Markup(''.join(buf))
fa08aef181a2 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
fa08aef181a2 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 elif kind is END:
fa08aef181a2 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 tag = data
fa08aef181a2 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 if tag.namespace and tag not in self.NAMESPACE:
fa08aef181a2 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 continue # not in the HTML namespace, so don't emit
fa08aef181a2 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 yield Markup('</%s>' % tag.localname)
fa08aef181a2 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
fa08aef181a2 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 elif kind is TEXT:
fa08aef181a2 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 escape(data, quotes=False)
fa08aef181a2 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
fa08aef181a2 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 COMMENT:
fa08aef181a2 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 Markup('<!--%s-->' % data)
fa08aef181a2 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
fa08aef181a2 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
fa08aef181a2 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 class HTMLSerializer(XHTMLSerializer):
fa08aef181a2 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 """Produces HTML text from an event stream.
fa08aef181a2 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
244
fa08aef181a2 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
245 >>> from markup.builder import tag
fa08aef181a2 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
246 >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True))
fa08aef181a2 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 >>> print ''.join(HTMLSerializer().serialize(elem.generate()))
fa08aef181a2 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 <div><a href="foo"></a><br><hr noshade></div>
fa08aef181a2 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 """
fa08aef181a2 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
fa08aef181a2 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 def serialize(self, stream):
fa08aef181a2 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 have_doctype = False
fa08aef181a2 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 ns_mapping = {}
fa08aef181a2 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
fa08aef181a2 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 stream = _PushbackIterator(chain(self.preamble, stream))
fa08aef181a2 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 for kind, data, pos in stream:
fa08aef181a2 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
fa08aef181a2 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 if kind is DOCTYPE:
fa08aef181a2 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 if not have_doctype:
fa08aef181a2 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 name, pubid, sysid = data
fa08aef181a2 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 buf = ['<!DOCTYPE %s']
fa08aef181a2 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 if pubid:
fa08aef181a2 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 buf.append(' PUBLIC "%s"')
fa08aef181a2 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 elif sysid:
fa08aef181a2 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 buf.append(' SYSTEM')
fa08aef181a2 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 if sysid:
fa08aef181a2 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.append(' "%s"')
fa08aef181a2 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 buf.append('>\n')
fa08aef181a2 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 yield Markup(''.join(buf), *filter(None, data))
fa08aef181a2 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 have_doctype = True
fa08aef181a2 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
fa08aef181a2 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 elif kind is START_NS:
fa08aef181a2 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 prefix, uri = data
fa08aef181a2 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 if uri not in ns_mapping:
fa08aef181a2 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 ns_mapping[uri] = prefix
fa08aef181a2 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
fa08aef181a2 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 elif kind is START:
fa08aef181a2 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 tag, attrib = data
fa08aef181a2 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 if tag.namespace and tag not in self.NAMESPACE:
fa08aef181a2 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 continue # not in the HTML namespace, so don't emit
fa08aef181a2 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 buf = ['<', tag.localname]
fa08aef181a2 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
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
283 for attr, value in attrib:
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 1
diff changeset
284 if attr.namespace and attr not in self.NAMESPACE:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
285 continue # not in the HTML namespace, so don't emit
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
286 if attr.localname in self._BOOLEAN_ATTRS:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
287 if value:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
288 buf.append(' %s' % attr.localname)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
289 else:
73
1da51d718391 Some more performance tweaks.
cmlenz
parents: 69
diff changeset
290 buf.append(' %s="%s"' % (attr.localname, escape(value)))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
291
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
292 if tag.localname in self._EMPTY_ELEMS:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
293 kind, data, pos = stream.next()
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
294 if kind is not END:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
295 stream.pushback((kind, data, pos))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
296
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
297 yield Markup(''.join(buf + ['>']))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
298
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
299 elif kind is END:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
300 tag = data
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 1
diff changeset
301 if tag.namespace and tag not in self.NAMESPACE:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
302 continue # not in the HTML namespace, so don't emit
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
303 yield Markup('</%s>' % tag.localname)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
304
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
305 elif kind is TEXT:
73
1da51d718391 Some more performance tweaks.
cmlenz
parents: 69
diff changeset
306 yield escape(data, quotes=False)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
307
89
80386d62814f 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
308 elif kind is COMMENT:
80386d62814f 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
309 yield Markup('<!--%s-->' % data)
80386d62814f 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
310
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
311
26
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
312 class _PushbackIterator(object):
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
313 """A simple wrapper for iterators that allows pushing items back on the
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
314 queue via the `pushback()` method.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
315
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
316 That can effectively be used to peek at the next item."""
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
317 __slots__ = ['iterable', 'buf']
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
318
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
319 def __init__(self, iterable):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
320 self.iterable = iter(iterable)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
321 self.buf = []
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
322
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
323 def __iter__(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
324 return self
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
325
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
326 def next(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
327 if self.buf:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
328 return self.buf.pop(0)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
329 return self.iterable.next()
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
330
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
331 def pushback(self, item):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
332 self.buf.append(item)
Copyright (C) 2012-2017 Edgewall Software