annotate markup/output.py @ 186:0c2d52c6c3f7

Moved installation instructions to `INSTALL.txt`.
author cmlenz
date Tue, 22 Aug 2006 15:25:45 +0000
parents 8fb7df2e1281
children 50eab0469148
rev   line source
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
5 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
9 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
13
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
14 """This module provides different kinds of serialization methods for XML event
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
15 streams.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
16 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
17
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
18 from itertools import chain
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
19 try:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
20 frozenset
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
21 except NameError:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
22 from sets import ImmutableSet as frozenset
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
23 import re
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
24
145
56d534eb53f9 * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 143
diff changeset
25 from markup.core import escape, Markup, Namespace, QName
56d534eb53f9 * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 143
diff changeset
26 from markup.core import DOCTYPE, START, END, START_NS, TEXT, START_CDATA, \
56d534eb53f9 * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 143
diff changeset
27 END_CDATA, PI, COMMENT, XML_NAMESPACE
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
28
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
29 __all__ = ['Serializer', 'XMLSerializer', 'HTMLSerializer']
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
30
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
31
85
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
32 class DocType(object):
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
33 """Defines a number of commonly used DOCTYPE declarations as constants."""
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
34
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
35 HTML_STRICT = ('html', '-//W3C//DTD HTML 4.01//EN',
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
36 'http://www.w3.org/TR/html4/strict.dtd')
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
37 HTML_TRANSITIONAL = ('html', '-//W3C//DTD HTML 4.01 Transitional//EN',
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
38 'http://www.w3.org/TR/html4/loose.dtd')
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
39 HTML = HTML_STRICT
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
40
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
41 XHTML_STRICT = ('html', '-//W3C//DTD XHTML 1.0 Strict//EN',
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
42 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd')
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
43 XHTML_TRANSITIONAL = ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
44 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
45 XHTML = XHTML_STRICT
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
46
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
47
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
48 class XMLSerializer(object):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
49 """Produces XML text from an event stream.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
50
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
51 >>> from markup.builder import tag
20
e3d3c1d8c98a Fix tests broken in [20].
cmlenz
parents: 19
diff changeset
52 >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True))
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
53 >>> print ''.join(XMLSerializer()(elem.generate()))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
54 <div><a href="foo"/><br/><hr noshade="True"/></div>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
55 """
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
56
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
57 _PRESERVE_SPACE = frozenset()
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
58
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
59 def __init__(self, doctype=None, strip_whitespace=True):
85
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
60 """Initialize the XML serializer.
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
61
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
62 @param doctype: a `(name, pubid, sysid)` tuple that represents the
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
63 DOCTYPE declaration that should be included at the top of the
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
64 generated output
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
65 @param strip_whitespace: whether extraneous whitespace should be
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
66 stripped from the output
85
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
67 """
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
68 self.preamble = []
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
69 if doctype:
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
70 self.preamble.append((DOCTYPE, doctype, (None, -1, -1)))
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
71 self.filters = []
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
72 if strip_whitespace:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
73 self.filters.append(WhitespaceFilter(self._PRESERVE_SPACE))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
74
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
75 def __call__(self, stream):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
76 ns_attrib = []
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
77 ns_mapping = {XML_NAMESPACE.uri: 'xml'}
143
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
78 have_doctype = False
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
79 in_cdata = False
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
80
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
81 stream = chain(self.preamble, stream)
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
82 for filter_ in self.filters:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
83 stream = filter_(stream)
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
84 stream = _PushbackIterator(stream)
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
85 pushback = stream.pushback
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
86 for kind, data, pos in stream:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
87
109
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
88 if kind is START:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
89 tag, attrib = data
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
90
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
91 tagname = tag.localname
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
92 namespace = tag.namespace
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
93 if namespace:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
94 if namespace in ns_mapping:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
95 prefix = ns_mapping[namespace]
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
96 if prefix:
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
97 tagname = '%s:%s' % (prefix, tagname)
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
98 else:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
99 ns_attrib.append((QName('xmlns'), namespace))
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
100 buf = ['<', tagname]
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
101
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
102 for attr, value in attrib + ns_attrib:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
103 attrname = attr.localname
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
104 if attr.namespace:
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
105 prefix = ns_mapping.get(attr.namespace)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
106 if prefix:
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
107 attrname = '%s:%s' % (prefix, attrname)
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
108 buf += [' ', attrname, '="', escape(value), '"']
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
109 ns_attrib = []
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
110
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
111 kind, data, pos = stream.next()
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
112 if kind is END:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
113 buf += ['/>']
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
114 else:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
115 buf += ['>']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
116 pushback((kind, data, pos))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
117
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
118 yield Markup(''.join(buf))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
119
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
120 elif kind is END:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
121 tag = data
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
122 tagname = tag.localname
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
123 if tag.namespace:
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
124 prefix = ns_mapping.get(tag.namespace)
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
125 if prefix:
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
126 tagname = '%s:%s' % (prefix, tag.localname)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
127 yield Markup('</%s>' % tagname)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
128
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
129 elif kind is TEXT:
143
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
130 if in_cdata:
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
131 yield data
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
132 else:
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
133 yield escape(data, quotes=False)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
134
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
135 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
136 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
137
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
138 elif kind is DOCTYPE and not have_doctype:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
139 name, pubid, sysid = data
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
140 buf = ['<!DOCTYPE %s']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
141 if pubid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
142 buf += [' PUBLIC "%s"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
143 elif sysid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
144 buf += [' SYSTEM']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
145 if sysid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
146 buf += [' "%s"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
147 buf += ['>\n']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
148 yield Markup(''.join(buf), *filter(None, data))
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
149 have_doctype = True
109
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
150
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
151 elif kind is START_NS:
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
152 prefix, uri = data
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
153 if uri not in ns_mapping:
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
154 ns_mapping[uri] = prefix
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
155 if not prefix:
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
156 ns_attrib.append((QName('xmlns'), uri))
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
157 else:
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
158 ns_attrib.append((QName('xmlns:%s' % prefix), uri))
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
159
143
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
160 elif kind is START_CDATA:
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
161 yield Markup('<![CDATA[')
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
162 in_cdata = True
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
163
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
164 elif kind is END_CDATA:
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
165 yield Markup(']]>')
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
166 in_cdata = False
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
167
105
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
168 elif kind is PI:
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
169 yield Markup('<?%s %s?>' % data)
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
170
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
171
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
172 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
173 """Produces XHTML text from an event stream.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
174
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
175 >>> from markup.builder import tag
20
e3d3c1d8c98a Fix tests broken in [20].
cmlenz
parents: 19
diff changeset
176 >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True))
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
177 >>> print ''.join(XHTMLSerializer()(elem.generate()))
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
178 <div><a href="foo"></a><br /><hr noshade="noshade" /></div>
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
179 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
180
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 1
diff changeset
181 NAMESPACE = Namespace('http://www.w3.org/1999/xhtml')
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
182
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
183 _EMPTY_ELEMS = frozenset(['area', 'base', 'basefont', 'br', 'col', 'frame',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
184 'hr', 'img', 'input', 'isindex', 'link', 'meta',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
185 'param'])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
186 _BOOLEAN_ATTRS = frozenset(['selected', 'checked', 'compact', 'declare',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
187 'defer', 'disabled', 'ismap', 'multiple',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
188 'nohref', 'noresize', 'noshade', 'nowrap'])
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
189 _PRESERVE_SPACE = frozenset([QName('pre'), QName('textarea')])
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
190
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
191 def __call__(self, stream):
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
192 namespace = self.NAMESPACE
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
193 ns_attrib = []
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
194 ns_mapping = {XML_NAMESPACE.uri: 'xml'}
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
195 boolean_attrs = self._BOOLEAN_ATTRS
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
196 empty_elems = self._EMPTY_ELEMS
85
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
197 have_doctype = False
143
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
198 in_cdata = False
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
199
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
200 stream = chain(self.preamble, stream)
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
201 for filter_ in self.filters:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
202 stream = filter_(stream)
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
203 stream = _PushbackIterator(stream)
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
204 pushback = stream.pushback
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
205 for kind, data, pos in stream:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
206
109
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
207 if kind is START:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
208 tag, attrib = data
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
209
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
210 tagname = tag.localname
177
dbae9efe5704 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 158
diff changeset
211 tagns = tag.namespace
dbae9efe5704 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 158
diff changeset
212 if tagns:
dbae9efe5704 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 158
diff changeset
213 if tagns in ns_mapping:
dbae9efe5704 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 158
diff changeset
214 prefix = ns_mapping[tagns]
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
215 if prefix:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
216 tagname = '%s:%s' % (prefix, tagname)
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
217 else:
177
dbae9efe5704 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 158
diff changeset
218 ns_attrib.append((QName('xmlns'), tagns))
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
219 buf = ['<', tagname]
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
220
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
221 for attr, value in attrib + ns_attrib:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
222 attrname = attr.localname
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
223 if attr.namespace:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
224 prefix = ns_mapping.get(attr.namespace)
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
225 if prefix:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
226 attrname = '%s:%s' % (prefix, attrname)
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
227 if attrname in boolean_attrs:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
228 if value:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
229 buf += [' ', attrname, '="', attrname, '"']
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
230 else:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
231 buf += [' ', attrname, '="', escape(value), '"']
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
232 ns_attrib = []
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
233
178
8fb7df2e1281 Fix bug in XHTML serialization: all elements were allowed to be written out as empty if the namespace was set.
cmlenz
parents: 177
diff changeset
234 if (tagns and tagns != namespace.uri) or tagname in empty_elems:
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
235 kind, data, pos = stream.next()
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
236 if kind is END:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
237 buf += [' />']
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
238 else:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
239 buf += ['>']
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
240 pushback((kind, data, pos))
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
241 else:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
242 buf += ['>']
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
243
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
244 yield Markup(''.join(buf))
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
245
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
246 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
247 tag = data
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
248 tagname = tag.localname
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
249 if tag.namespace:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
250 prefix = ns_mapping.get(tag.namespace)
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
251 if prefix:
177
dbae9efe5704 * Minor fix for the XHTML serializer (the local namespace var got clobbered)
cmlenz
parents: 158
diff changeset
252 tagname = '%s:%s' % (prefix, tagname)
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
253 yield Markup('</%s>' % tagname)
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
254
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 elif kind is TEXT:
143
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
256 if in_cdata:
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
257 yield data
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
258 else:
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
259 yield escape(data, quotes=False)
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
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 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
262 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
263
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
264 elif kind is DOCTYPE and not have_doctype:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
265 name, pubid, sysid = data
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
266 buf = ['<!DOCTYPE %s']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
267 if pubid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
268 buf += [' PUBLIC "%s"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
269 elif sysid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
270 buf += [' SYSTEM']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
271 if sysid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
272 buf += [' "%s"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
273 buf += ['>\n']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
274 yield Markup(''.join(buf), *filter(None, data))
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
275 have_doctype = True
109
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
276
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
277 elif kind is START_NS:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
278 prefix, uri = data
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
279 if uri not in ns_mapping:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
280 ns_mapping[uri] = prefix
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
281 if not prefix:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
282 ns_attrib.append((QName('xmlns'), uri))
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
283 else:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
284 ns_attrib.append((QName('xmlns:%s' % prefix), uri))
109
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
285
143
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
286 elif kind is START_CDATA:
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
287 yield Markup('<![CDATA[')
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
288 in_cdata = True
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
289
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
290 elif kind is END_CDATA:
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
291 yield Markup(']]>')
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
292 in_cdata = False
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
293
105
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
294 elif kind is PI:
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
295 yield Markup('<?%s %s?>' % data)
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
296
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
297
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
298 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
299 """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
300
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
301 >>> 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
302 >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True))
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
303 >>> print ''.join(HTMLSerializer()(elem.generate()))
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
304 <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
305 """
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
306
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
307 _NOESCAPE_ELEMS = frozenset([QName('script'), QName('style')])
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
308
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
309 def __init__(self, doctype=None, strip_whitespace=True):
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
310 """Initialize the HTML serializer.
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
311
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
312 @param doctype: a `(name, pubid, sysid)` tuple that represents the
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
313 DOCTYPE declaration that should be included at the top of the
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
314 generated output
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
315 @param strip_whitespace: whether extraneous whitespace should be
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
316 stripped from the output
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
317 """
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
318 super(HTMLSerializer, self).__init__(doctype, False)
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
319 if strip_whitespace:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
320 self.filters.append(WhitespaceFilter(self._PRESERVE_SPACE,
143
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
321 self._NOESCAPE_ELEMS, True))
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
322
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
323 def __call__(self, stream):
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
324 namespace = self.NAMESPACE
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
325 ns_mapping = {}
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
326 boolean_attrs = self._BOOLEAN_ATTRS
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
327 empty_elems = self._EMPTY_ELEMS
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
328 noescape_elems = self._NOESCAPE_ELEMS
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
329 have_doctype = False
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
330 noescape = False
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
331
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
332 stream = chain(self.preamble, stream)
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
333 for filter_ in self.filters:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
334 stream = filter_(stream)
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
335 stream = _PushbackIterator(stream)
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
336 pushback = stream.pushback
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
337 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
338
109
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
339 if kind is START:
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
340 tag, attrib = data
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
341 if not tag.namespace or tag in namespace:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
342 tagname = tag.localname
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
343 buf = ['<', tagname]
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
344
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
345 for attr, value in attrib:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
346 attrname = attr.localname
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
347 if not attr.namespace or attr in namespace:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
348 if attrname in boolean_attrs:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
349 if value:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
350 buf += [' ', attrname]
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
351 else:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
352 buf += [' ', attrname, '="', escape(value), '"']
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
353
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
354 if tagname in empty_elems:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
355 kind, data, pos = stream.next()
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
356 if kind is not END:
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
357 pushback((kind, data, pos))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
358
140
a2edde90ad24 Fix bug in HTML serializer, plus some other minor tweaks.
cmlenz
parents: 136
diff changeset
359 buf += ['>']
a2edde90ad24 Fix bug in HTML serializer, plus some other minor tweaks.
cmlenz
parents: 136
diff changeset
360 yield Markup(''.join(buf))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
361
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
362 if tagname in noescape_elems:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
363 noescape = True
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
364
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
365 elif kind is END:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
366 tag = data
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
367 if not tag.namespace or tag in namespace:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
368 yield Markup('</%s>' % tag.localname)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
369
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
370 noescape = False
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
371
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
372 elif kind is TEXT:
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
373 if noescape:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
374 yield data
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
375 else:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
376 yield escape(data, quotes=False)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
377
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
378 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
379 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
380
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
381 elif kind is DOCTYPE and not have_doctype:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
382 name, pubid, sysid = data
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
383 buf = ['<!DOCTYPE %s']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
384 if pubid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
385 buf += [' PUBLIC "%s"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
386 elif sysid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
387 buf += [' SYSTEM']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
388 if sysid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
389 buf += [' "%s"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
390 buf += ['>\n']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
391 yield Markup(''.join(buf), *filter(None, data))
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
392 have_doctype = True
109
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
393
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
394 elif kind is START_NS and data[1] not in ns_mapping:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
395 ns_mapping[data[1]] = data[0]
109
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
396
105
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
397 elif kind is PI:
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
398 yield Markup('<?%s %s?>' % data)
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
399
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
400
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
401 class WhitespaceFilter(object):
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
402 """A filter that removes extraneous ignorable white space from the
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
403 stream."""
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
404
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
405 _TRAILING_SPACE = re.compile('[ \t]+(?=\n)')
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
406 _LINE_COLLAPSE = re.compile('\n{2,}')
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
407 _XML_SPACE = XML_NAMESPACE['space']
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
408
143
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
409 def __init__(self, preserve=None, noescape=None, escape_cdata=False):
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
410 """Initialize the filter.
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
411
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
412 @param preserve: a set or sequence of tag names for which white-space
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
413 should be ignored.
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
414 @param noescape: a set or sequence of tag names for which text content
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
415 should not be escaped
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
416
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
417 Both the `preserve` and `noescape` sets are expected to refer to
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
418 elements that cannot contain further child elements.
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
419 """
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
420 if preserve is None:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
421 preserve = []
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
422 self.preserve = frozenset(preserve)
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
423 if noescape is None:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
424 noescape = []
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
425 self.noescape = frozenset(noescape)
143
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
426 self.escape_cdata = escape_cdata
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
427
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
428 def __call__(self, stream, ctxt=None):
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
429 trim_trailing_space = self._TRAILING_SPACE.sub
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
430 collapse_lines = self._LINE_COLLAPSE.sub
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
431 xml_space = self._XML_SPACE
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
432 mjoin = Markup('').join
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
433 preserve_elems = self.preserve
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
434 preserve = False
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
435 noescape_elems = self.noescape
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
436 noescape = False
143
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
437 escape_cdata = self.escape_cdata
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
438
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
439 textbuf = []
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
440 push_text = textbuf.append
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
441 pop_text = textbuf.pop
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
442 for kind, data, pos in chain(stream, [(None, None, None)]):
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
443 if kind is TEXT:
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
444 if noescape:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
445 data = Markup(data)
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
446 push_text(data)
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
447 else:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
448 if textbuf:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
449 if len(textbuf) > 1:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
450 text = mjoin(textbuf, escape_quotes=False)
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
451 del textbuf[:]
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
452 else:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
453 text = escape(pop_text(), quotes=False)
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
454 if not preserve:
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
455 text = collapse_lines('\n', trim_trailing_space('', text))
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
456 yield TEXT, Markup(text), pos
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
457
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
458 if kind is START:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
459 tag, attrib = data
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
460 if tag.localname in preserve_elems or \
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
461 data[1].get(xml_space) == 'preserve':
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
462 preserve = True
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
463
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
464 if tag.localname in noescape_elems:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
465 noescape = True
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
466
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
467 elif kind is END:
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
468 preserve = noescape = False
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
469
143
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
470 elif kind is START_CDATA and not escape_cdata:
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
471 noescape = True
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
472
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
473 elif kind is END_CDATA and not escape_cdata:
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
474 noescape = False
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
475
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
476 if kind:
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
477 yield kind, data, pos
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
478
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
479
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
480 class _PushbackIterator(object):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
481 """A simple wrapper for iterators that allows pushing items back on the
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
482 queue via the `pushback()` method.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
483
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
484 That can effectively be used to peek at the next item."""
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
485 __slots__ = ['iterable', 'buf']
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
486
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
487 def __init__(self, iterable):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
488 self.iterable = iter(iterable)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
489 self.buf = []
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
490
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
491 def __iter__(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
492 return self
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
493
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
494 def next(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
495 if self.buf:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
496 return self.buf.pop(0)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
497 return self.iterable.next()
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
498
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
499 def pushback(self, item):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
500 self.buf.append(item)
Copyright (C) 2012-2017 Edgewall Software