annotate markup/output.py @ 143:3d4c214c979a trunk

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.
author cmlenz
date Fri, 11 Aug 2006 14:08:13 +0000
parents 520a5b7dd6d2
children 47bbd9d2a5af
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
123
10279d2eeec9 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
19 try:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
20 frozenset
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
21 except NameError:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
22 from sets import ImmutableSet as frozenset
123
10279d2eeec9 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
24
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
25 from markup.core import escape, Markup, Namespace, QName, XML_NAMESPACE
143
3d4c214c979a 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
26 from markup.core import DOCTYPE, START, END, START_NS, END_NS, TEXT, \
3d4c214c979a 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
27 START_CDATA, END_CDATA, PI, COMMENT
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
28
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
29 __all__ = ['Serializer', 'XMLSerializer', 'HTMLSerializer']
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
30
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
31
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
32 class DocType(object):
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
33 """Defines a number of commonly used DOCTYPE declarations as constants."""
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
34
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
35 HTML_STRICT = ('html', '-//W3C//DTD HTML 4.01//EN',
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
36 'http://www.w3.org/TR/html4/strict.dtd')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
37 HTML_TRANSITIONAL = ('html', '-//W3C//DTD HTML 4.01 Transitional//EN',
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
38 'http://www.w3.org/TR/html4/loose.dtd')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
39 HTML = HTML_STRICT
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
40
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
41 XHTML_STRICT = ('html', '-//W3C//DTD XHTML 1.0 Strict//EN',
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
42 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
43 XHTML_TRANSITIONAL = ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
44 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
45 XHTML = XHTML_STRICT
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
46
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
47
123
10279d2eeec9 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
49 """Produces XML text from an event stream.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
50
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
51 >>> from markup.builder import tag
20
cc92d74ce9e5 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
10279d2eeec9 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
54 <div><a href="foo"/><br/><hr noshade="True"/></div>
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
55 """
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
56
10279d2eeec9 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()
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
58
10279d2eeec9 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
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
60 """Initialize the XML serializer.
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
61
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
62 @param doctype: a `(name, pubid, sysid)` tuple that represents the
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
63 DOCTYPE declaration that should be included at the top of the
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
64 generated output
123
10279d2eeec9 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
10279d2eeec9 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
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
67 """
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
68 self.preamble = []
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
69 if doctype:
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
70 self.preamble.append((DOCTYPE, doctype, (None, -1, -1)))
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
71 self.filters = []
10279d2eeec9 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:
10279d2eeec9 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
74
123
10279d2eeec9 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
76 ns_attrib = []
141
520a5b7dd6d2 * 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
3d4c214c979a 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
3d4c214c979a 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
80
123
10279d2eeec9 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)
10279d2eeec9 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:
10279d2eeec9 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)
10279d2eeec9 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
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
85 pushback = stream.pushback
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
86 for kind, data, pos in stream:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
87
109
230ee6a2c6b2 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
89 tag, attrib = data
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
90
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
91 tagname = tag.localname
123
10279d2eeec9 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
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
93 if namespace:
10279d2eeec9 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:
10279d2eeec9 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
96 if prefix:
123
10279d2eeec9 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)
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
98 else:
10279d2eeec9 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
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
100 buf = ['<', tagname]
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
101
123
10279d2eeec9 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
103 attrname = attr.localname
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
104 if attr.namespace:
26
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
105 prefix = ns_mapping.get(attr.namespace)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
106 if prefix:
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
107 attrname = '%s:%s' % (prefix, attrname)
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
108 buf += [' ', attrname, '="', escape(value), '"']
123
10279d2eeec9 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
110
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
111 kind, data, pos = stream.next()
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
112 if kind is END:
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
113 buf += ['/>']
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
114 else:
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
115 buf += ['>']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
116 pushback((kind, data, pos))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
117
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
118 yield Markup(''.join(buf))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
119
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
120 elif kind is END:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
121 tag = data
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
122 tagname = tag.localname
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
123 if tag.namespace:
26
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
124 prefix = ns_mapping.get(tag.namespace)
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
125 if prefix:
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
126 tagname = '%s:%s' % (prefix, tag.localname)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
127 yield Markup('</%s>' % tagname)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
128
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
129 elif kind is TEXT:
143
3d4c214c979a 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:
3d4c214c979a 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
3d4c214c979a 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:
3d4c214c979a 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
134
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
135 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
136 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
137
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
138 elif kind is DOCTYPE and not have_doctype:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
139 name, pubid, sysid = data
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
140 buf = ['<!DOCTYPE %s']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
141 if pubid:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
142 buf += [' PUBLIC "%s"']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
143 elif sysid:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
144 buf += [' SYSTEM']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
145 if sysid:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
146 buf += [' "%s"']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
147 buf += ['>\n']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
148 yield Markup(''.join(buf), *filter(None, data))
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
149 have_doctype = True
109
230ee6a2c6b2 Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
150
230ee6a2c6b2 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:
230ee6a2c6b2 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
230ee6a2c6b2 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:
230ee6a2c6b2 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
230ee6a2c6b2 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:
230ee6a2c6b2 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))
230ee6a2c6b2 Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
157 else:
230ee6a2c6b2 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))
230ee6a2c6b2 Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
159
143
3d4c214c979a 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:
3d4c214c979a 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[')
3d4c214c979a 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
3d4c214c979a 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
3d4c214c979a 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:
3d4c214c979a 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(']]>')
3d4c214c979a 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
3d4c214c979a 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
71f3db26eecb Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
168 elif kind is PI:
71f3db26eecb Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
169 yield Markup('<?%s %s?>' % data)
71f3db26eecb Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
170
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
171
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
172 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
173 """Produces XHTML text from an event stream.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
174
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
175 >>> from markup.builder import tag
20
cc92d74ce9e5 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
10279d2eeec9 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
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
178 <div><a href="foo"></a><br /><hr noshade="noshade" /></div>
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
179 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
180
18
5420cfe42d36 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
182
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
183 _EMPTY_ELEMS = frozenset(['area', 'base', 'basefont', 'br', 'col', 'frame',
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
184 'hr', 'img', 'input', 'isindex', 'link', 'meta',
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
185 'param'])
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
186 _BOOLEAN_ATTRS = frozenset(['selected', 'checked', 'compact', 'declare',
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
187 'defer', 'disabled', 'ismap', 'multiple',
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
188 'nohref', 'noresize', 'noshade', 'nowrap'])
123
10279d2eeec9 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
190
123
10279d2eeec9 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
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
192 namespace = self.NAMESPACE
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
193 ns_attrib = []
520a5b7dd6d2 * 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
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
195 boolean_attrs = self._BOOLEAN_ATTRS
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
196 empty_elems = self._EMPTY_ELEMS
85
4938c310d904 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
197 have_doctype = False
143
3d4c214c979a 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
199
123
10279d2eeec9 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)
10279d2eeec9 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:
10279d2eeec9 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)
10279d2eeec9 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
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
204 pushback = stream.pushback
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
205 for kind, data, pos in stream:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
206
109
230ee6a2c6b2 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
208 tag, attrib = data
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
209
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
210 tagname = tag.localname
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
211 namespace = tag.namespace
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
212 if namespace:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
213 if namespace in ns_mapping:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
214 prefix = ns_mapping[namespace]
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
215 if prefix:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
216 tagname = '%s:%s' % (prefix, tagname)
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
217 else:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
218 ns_attrib.append((QName('xmlns'), namespace))
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
219 buf = ['<', tagname]
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
220
141
520a5b7dd6d2 * 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:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
222 attrname = attr.localname
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
223 if attr.namespace:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
224 prefix = ns_mapping.get(attr.namespace)
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
225 if prefix:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
226 attrname = '%s:%s' % (prefix, attrname)
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
227 if attrname in boolean_attrs:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
228 if value:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
229 buf += [' ', attrname, '="', attrname, '"']
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
230 else:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
231 buf += [' ', attrname, '="', escape(value), '"']
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
232 ns_attrib = []
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
233
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
234 if (not tag.namespace or tag in namespace) and \
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
235 tagname in empty_elems:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
236 kind, data, pos = stream.next()
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
237 if kind is END:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
238 buf += [' />']
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
239 else:
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
240 buf += ['>']
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
241 pushback((kind, data, pos))
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
242 else:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
243 buf += ['>']
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
244
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
245 yield Markup(''.join(buf))
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
246
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 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
248 tag = data
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
249 tagname = tag.localname
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
250 if tag.namespace:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
251 prefix = ns_mapping.get(tag.namespace)
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
252 if prefix:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
253 tagname = '%s:%s' % (prefix, tag.localname)
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
254 yield Markup('</%s>' % tagname)
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
255
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 elif kind is TEXT:
143
3d4c214c979a 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 if in_cdata:
3d4c214c979a 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 yield data
3d4c214c979a 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 else:
3d4c214c979a 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
260 yield escape(data, quotes=False)
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
261
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 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
263 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
264
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
265 elif kind is DOCTYPE and not have_doctype:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
266 name, pubid, sysid = data
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
267 buf = ['<!DOCTYPE %s']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
268 if pubid:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
269 buf += [' PUBLIC "%s"']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
270 elif sysid:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
271 buf += [' SYSTEM']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
272 if sysid:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
273 buf += [' "%s"']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
274 buf += ['>\n']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
275 yield Markup(''.join(buf), *filter(None, data))
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
276 have_doctype = True
109
230ee6a2c6b2 Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
277
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
278 elif kind is START_NS:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
279 prefix, uri = data
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
280 if uri not in ns_mapping:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
281 ns_mapping[uri] = prefix
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
282 if not prefix:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
283 ns_attrib.append((QName('xmlns'), uri))
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
284 else:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
285 ns_attrib.append((QName('xmlns:%s' % prefix), uri))
109
230ee6a2c6b2 Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
286
143
3d4c214c979a 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 elif kind is START_CDATA:
3d4c214c979a 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 yield Markup('<![CDATA[')
3d4c214c979a 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 in_cdata = True
3d4c214c979a 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
3d4c214c979a 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 elif kind is END_CDATA:
3d4c214c979a 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 yield Markup(']]>')
3d4c214c979a 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 in_cdata = False
3d4c214c979a 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
294
105
71f3db26eecb Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
295 elif kind is PI:
71f3db26eecb Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
296 yield Markup('<?%s %s?>' % data)
71f3db26eecb Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
297
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
298
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
299 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
300 """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
301
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
302 >>> 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
303 >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True))
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
304 >>> print ''.join(HTMLSerializer()(elem.generate()))
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
305 <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
306 """
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
307
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
308 _NOESCAPE_ELEMS = frozenset([QName('script'), QName('style')])
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
309
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
310 def __init__(self, doctype=None, strip_whitespace=True):
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
311 """Initialize the HTML serializer.
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
312
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
313 @param doctype: a `(name, pubid, sysid)` tuple that represents the
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
314 DOCTYPE declaration that should be included at the top of the
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
315 generated output
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
316 @param strip_whitespace: whether extraneous whitespace should be
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
317 stripped from the output
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
318 """
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
319 super(HTMLSerializer, self).__init__(doctype, False)
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
320 if strip_whitespace:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
321 self.filters.append(WhitespaceFilter(self._PRESERVE_SPACE,
143
3d4c214c979a 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
322 self._NOESCAPE_ELEMS, True))
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
323
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
324 def __call__(self, stream):
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
325 namespace = self.NAMESPACE
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
326 ns_mapping = {}
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
327 boolean_attrs = self._BOOLEAN_ATTRS
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
328 empty_elems = self._EMPTY_ELEMS
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
329 noescape_elems = self._NOESCAPE_ELEMS
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
330 have_doctype = False
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
331 noescape = False
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
332
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
333 stream = chain(self.preamble, stream)
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
334 for filter_ in self.filters:
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
335 stream = filter_(stream)
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
336 stream = _PushbackIterator(stream)
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
337 pushback = stream.pushback
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
338 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
339
109
230ee6a2c6b2 Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
340 if kind is START:
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
341 tag, attrib = data
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
342 if not tag.namespace or tag in namespace:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
343 tagname = tag.localname
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
344 buf = ['<', tagname]
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
345
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
346 for attr, value in attrib:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
347 attrname = attr.localname
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
348 if not attr.namespace or attr in namespace:
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
349 if attrname in boolean_attrs:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
350 if value:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
351 buf += [' ', attrname]
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
352 else:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
353 buf += [' ', attrname, '="', escape(value), '"']
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
354
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
355 if tagname in empty_elems:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
356 kind, data, pos = stream.next()
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
357 if kind is not END:
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
358 pushback((kind, data, pos))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
359
140
c1f4390d50f8 Fix bug in HTML serializer, plus some other minor tweaks.
cmlenz
parents: 136
diff changeset
360 buf += ['>']
c1f4390d50f8 Fix bug in HTML serializer, plus some other minor tweaks.
cmlenz
parents: 136
diff changeset
361 yield Markup(''.join(buf))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
362
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
363 if tagname in noescape_elems:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
364 noescape = True
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
365
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
366 elif kind is END:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
367 tag = data
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
368 if not tag.namespace or tag in namespace:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
369 yield Markup('</%s>' % tag.localname)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
370
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
371 noescape = False
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
372
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
373 elif kind is TEXT:
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
374 if noescape:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
375 yield data
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
376 else:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
377 yield escape(data, quotes=False)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
378
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
379 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
380 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
381
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
382 elif kind is DOCTYPE and not have_doctype:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
383 name, pubid, sysid = data
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
384 buf = ['<!DOCTYPE %s']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
385 if pubid:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
386 buf += [' PUBLIC "%s"']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
387 elif sysid:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
388 buf += [' SYSTEM']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
389 if sysid:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
390 buf += [' "%s"']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
391 buf += ['>\n']
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
392 yield Markup(''.join(buf), *filter(None, data))
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
393 have_doctype = True
109
230ee6a2c6b2 Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
394
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
395 elif kind is START_NS and data[1] not in ns_mapping:
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
396 ns_mapping[data[1]] = data[0]
109
230ee6a2c6b2 Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
397
105
71f3db26eecb Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
398 elif kind is PI:
71f3db26eecb Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
399 yield Markup('<?%s %s?>' % data)
71f3db26eecb Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
400
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
401
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
402 class WhitespaceFilter(object):
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
403 """A filter that removes extraneous ignorable white space from the
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
404 stream."""
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
405
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
406 _TRAILING_SPACE = re.compile('[ \t]+(?=\n)')
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
407 _LINE_COLLAPSE = re.compile('\n{2,}')
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
408 _XML_SPACE = XML_NAMESPACE['space']
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
409
143
3d4c214c979a 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
410 def __init__(self, preserve=None, noescape=None, escape_cdata=False):
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
411 """Initialize the filter.
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
412
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
413 @param preserve: a set or sequence of tag names for which white-space
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
414 should be ignored.
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
415 @param noescape: a set or sequence of tag names for which text content
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
416 should not be escaped
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
417
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
418 Both the `preserve` and `noescape` sets are expected to refer to
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
419 elements that cannot contain further child elements.
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
420 """
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
421 if preserve is None:
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
422 preserve = []
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
423 self.preserve = frozenset(preserve)
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
424 if noescape is None:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
425 noescape = []
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
426 self.noescape = frozenset(noescape)
143
3d4c214c979a 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
427 self.escape_cdata = escape_cdata
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
428
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
429 def __call__(self, stream, ctxt=None):
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
430 trim_trailing_space = self._TRAILING_SPACE.sub
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
431 collapse_lines = self._LINE_COLLAPSE.sub
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
432 xml_space = self._XML_SPACE
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
433 mjoin = Markup('').join
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
434 preserve_elems = self.preserve
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
435 preserve = False
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
436 noescape_elems = self.noescape
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
437 noescape = False
143
3d4c214c979a 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
438 escape_cdata = self.escape_cdata
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
439
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
440 textbuf = []
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
441 push_text = textbuf.append
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
442 pop_text = textbuf.pop
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
443 for kind, data, pos in chain(stream, [(None, None, None)]):
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
444 if kind is TEXT:
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
445 if noescape:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
446 data = Markup(data)
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
447 push_text(data)
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
448 else:
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
449 if textbuf:
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
450 if len(textbuf) > 1:
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
451 text = mjoin(textbuf, escape_quotes=False)
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
452 del textbuf[:]
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
453 else:
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
454 text = escape(pop_text(), quotes=False)
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
455 if not preserve:
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
456 text = collapse_lines('\n', trim_trailing_space('', text))
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
457 yield TEXT, Markup(text), pos
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
458
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
459 if kind is START:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
460 tag, attrib = data
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
461 if tag.localname in preserve_elems or \
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
462 data[1].get(xml_space) == 'preserve':
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
463 preserve = True
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
464
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
465 if tag.localname in noescape_elems:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
466 noescape = True
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
467
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
468 elif kind is END:
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
469 preserve = noescape = False
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
470
143
3d4c214c979a 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 elif kind is START_CDATA and not escape_cdata:
3d4c214c979a 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 noescape = True
3d4c214c979a 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
3d4c214c979a 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 elif kind is END_CDATA and not escape_cdata:
3d4c214c979a 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 noescape = False
3d4c214c979a 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
476
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
477 if kind:
123
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
478 yield kind, data, pos
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
479
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
480
26
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
481 class _PushbackIterator(object):
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
482 """A simple wrapper for iterators that allows pushing items back on the
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
483 queue via the `pushback()` method.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
484
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
485 That can effectively be used to peek at the next item."""
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
486 __slots__ = ['iterable', 'buf']
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
487
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
488 def __init__(self, iterable):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
489 self.iterable = iter(iterable)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
490 self.buf = []
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
491
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
492 def __iter__(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
493 return self
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
494
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
495 def next(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
496 if self.buf:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
497 return self.buf.pop(0)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
498 return self.iterable.next()
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
499
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
500 def pushback(self, item):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
501 self.buf.append(item)
Copyright (C) 2012-2017 Edgewall Software