annotate markup/output.py @ 136:636e0100fcaf

Minor performance improvements in serialization.
author cmlenz
date Sun, 06 Aug 2006 21:22:21 +0000
parents 93bbdcf9428b
children a2edde90ad24
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
73
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
25 from markup.core import escape, Markup, Namespace, QName
105
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
26 from markup.core import DOCTYPE, START, END, START_NS, END_NS, TEXT, COMMENT, PI
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
27
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
28 __all__ = ['Serializer', 'XMLSerializer', 'HTMLSerializer']
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
29
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
30
85
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
31 class DocType(object):
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
32 """Defines a number of commonly used DOCTYPE declarations as constants."""
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
33
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
34 HTML_STRICT = ('html', '-//W3C//DTD HTML 4.01//EN',
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
35 'http://www.w3.org/TR/html4/strict.dtd')
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
36 HTML_TRANSITIONAL = ('html', '-//W3C//DTD HTML 4.01 Transitional//EN',
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
37 'http://www.w3.org/TR/html4/loose.dtd')
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
38 HTML = HTML_STRICT
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
39
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
40 XHTML_STRICT = ('html', '-//W3C//DTD XHTML 1.0 Strict//EN',
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
41 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd')
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
42 XHTML_TRANSITIONAL = ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
43 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
44 XHTML = XHTML_STRICT
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
45
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
46
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
47 class XMLSerializer(object):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
48 """Produces XML text from an event stream.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
49
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
50 >>> from markup.builder import tag
20
e3d3c1d8c98a Fix tests broken in [20].
cmlenz
parents: 19
diff changeset
51 >>> 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
52 >>> print ''.join(XMLSerializer()(elem.generate()))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
53 <div><a href="foo"/><br/><hr noshade="True"/></div>
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
54 """
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
55
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
56 _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
57
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
58 def __init__(self, doctype=None, strip_whitespace=True):
85
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
59 """Initialize the XML serializer.
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
60
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
61 @param doctype: a `(name, pubid, sysid)` tuple that represents the
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
62 DOCTYPE declaration that should be included at the top of the
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
63 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
64 @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
65 stripped from the output
85
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
66 """
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
67 self.preamble = []
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
68 if doctype:
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
69 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
70 self.filters = []
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
71 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
72 self.filters.append(WhitespaceFilter(self._PRESERVE_SPACE))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
73
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
74 def __call__(self, stream):
85
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
75 have_doctype = False
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
76 ns_attrib = []
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
77 ns_mapping = {}
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
78
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
79 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
80 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
81 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
82 stream = _PushbackIterator(stream)
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
83 pushback = stream.pushback
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
84 for kind, data, pos in stream:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
85
109
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
86 if kind is START:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
87 tag, attrib = data
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
88
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
89 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
90 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
91 if namespace:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
92 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
93 prefix = ns_mapping[namespace]
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
94 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
95 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
96 else:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
97 ns_attrib.append((QName('xmlns'), namespace))
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
98 buf = ['<', tagname]
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
99
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
100 for attr, value in attrib + ns_attrib:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
101 attrname = attr.localname
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
102 if attr.namespace:
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
103 prefix = ns_mapping.get(attr.namespace)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
104 if prefix:
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
105 attrname = '%s:%s' % (prefix, attrname)
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
106 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
107 ns_attrib = []
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
108
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
109 kind, data, pos = stream.next()
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
110 if kind is END:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
111 buf += ['/>']
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
112 else:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
113 buf += ['>']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
114 pushback((kind, data, pos))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
115
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
116 yield Markup(''.join(buf))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
117
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
118 elif kind is END:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
119 tag = data
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
120 tagname = tag.localname
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
121 if tag.namespace:
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
122 prefix = ns_mapping.get(tag.namespace)
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
123 if prefix:
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
124 tagname = '%s:%s' % (prefix, tag.localname)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
125 yield Markup('</%s>' % tagname)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
126
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
127 elif kind is TEXT:
73
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
128 yield escape(data, quotes=False)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
129
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
130 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
131 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
132
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
133 elif kind is DOCTYPE and not have_doctype:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
134 name, pubid, sysid = data
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
135 buf = ['<!DOCTYPE %s']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
136 if pubid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
137 buf += [' PUBLIC "%s"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
138 elif sysid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
139 buf += [' SYSTEM']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
140 if sysid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
141 buf += [' "%s"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
142 buf += ['>\n']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
143 yield Markup(''.join(buf), *filter(None, data))
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
144 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
145
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
146 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
147 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
148 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
149 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
150 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
151 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
152 else:
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
153 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
154
105
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
155 elif kind is PI:
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
156 yield Markup('<?%s %s?>' % data)
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
157
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
158
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
159 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
160 """Produces XHTML text from an event stream.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
161
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
162 >>> from markup.builder import tag
20
e3d3c1d8c98a Fix tests broken in [20].
cmlenz
parents: 19
diff changeset
163 >>> 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
164 >>> 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
165 <div><a href="foo"></a><br /><hr noshade="noshade" /></div>
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
166 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
167
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 1
diff changeset
168 NAMESPACE = Namespace('http://www.w3.org/1999/xhtml')
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
169
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
170 _EMPTY_ELEMS = frozenset(['area', 'base', 'basefont', 'br', 'col', 'frame',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
171 'hr', 'img', 'input', 'isindex', 'link', 'meta',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
172 'param'])
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
173 _BOOLEAN_ATTRS = frozenset(['selected', 'checked', 'compact', 'declare',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
174 'defer', 'disabled', 'ismap', 'multiple',
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
175 '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
176 _PRESERVE_SPACE = frozenset([QName('pre'), QName('textarea')])
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
177
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
178 def __call__(self, stream):
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
179 namespace = self.NAMESPACE
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
180 ns_mapping = {}
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
181 boolean_attrs = self._BOOLEAN_ATTRS
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
182 empty_elems = self._EMPTY_ELEMS
85
db8f2958c670 Improve handling of DOCTYPE declarations.
cmlenz
parents: 73
diff changeset
183 have_doctype = False
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
184
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
185 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
186 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
187 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
188 stream = _PushbackIterator(stream)
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
189 pushback = stream.pushback
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
190 for kind, data, pos in stream:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
191
109
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
192 if kind is START:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
193 tag, attrib = data
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
194 if not tag.namespace or tag in namespace:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
195 tagname = tag.localname
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
196 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
197
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
198 for attr, value in attrib:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
199 if not attr.namespace or attr in namespace:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
200 attrname = attr.localname
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
201 if attrname in boolean_attrs:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
202 if value:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
203 buf += [' ', attrname, '="', attrname, '"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
204 else:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
205 buf += [' ', attrname, '="', escape(value), '"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
206
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
207 if tagname in empty_elems:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
208 kind, data, pos = stream.next()
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
209 if kind is END:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
210 buf += [' />']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
211 else:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
212 buf += ['>']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
213 pushback((kind, data, pos))
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
214 else:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
215 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
216
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
217 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
218
35d681a94763 Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents: 89
diff changeset
219 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
220 tag = data
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
221 if not tag.namespace or tag in namespace:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
222 yield Markup('</%s>' % tag.localname)
96
35d681a94763 Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents: 89
diff changeset
223
35d681a94763 Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents: 89
diff changeset
224 elif kind is TEXT:
35d681a94763 Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents: 89
diff changeset
225 yield escape(data, quotes=False)
35d681a94763 Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents: 89
diff changeset
226
35d681a94763 Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents: 89
diff changeset
227 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
228 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
229
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
230 elif kind is DOCTYPE and not have_doctype:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
231 name, pubid, sysid = data
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
232 buf = ['<!DOCTYPE %s']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
233 if pubid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
234 buf += [' PUBLIC "%s"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
235 elif sysid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
236 buf += [' SYSTEM']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
237 if sysid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
238 buf += [' "%s"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
239 buf += ['>\n']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
240 yield Markup(''.join(buf), *filter(None, data))
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
241 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
242
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
243 elif kind is START_NS and data[1] not in ns_mapping:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
244 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
245
105
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
246 elif kind is PI:
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
247 yield Markup('<?%s %s?>' % data)
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
248
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
249
35d681a94763 Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents: 89
diff changeset
250 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
251 """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
252
35d681a94763 Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents: 89
diff changeset
253 >>> 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
254 >>> 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
255 >>> 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
256 <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
257 """
35d681a94763 Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents: 89
diff changeset
258
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
259 def __call__(self, stream):
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
260 namespace = self.NAMESPACE
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
261 ns_mapping = {}
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
262 boolean_attrs = self._BOOLEAN_ATTRS
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
263 empty_elems = self._EMPTY_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
264 have_doctype = False
35d681a94763 Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents: 89
diff changeset
265
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
266 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
267 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
268 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
269 stream = _PushbackIterator(stream)
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
270 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
271
109
2de3f9d84a1c Reorder the conditional branches in the serializers so that the more common event kinds are on top.
cmlenz
parents: 105
diff changeset
272 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
273 tag, attrib = data
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
274 if not tag.namespace or tag in namespace:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
275 tagname = tag.localname
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
276 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
277
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
278 for attr, value in attrib:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
279 attrname = attr.localname
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
280 if not attr.namespace and not \
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
281 attrname.startswith('xml:') or \
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
282 attr in namespace:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
283 if attrname in boolean_attrs:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
284 if value:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
285 buf += [' ', attrname]
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
286 else:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
287 buf += [' ', attrname, '="', escape(value), '"']
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
288
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
289 if tagname in empty_elems:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
290 kind, data, pos = stream.next()
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
291 if kind is not END:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
292 stream.pushback((kind, data, pos))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
293
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
294 buf += ['>']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
295 yield Markup(''.join(buf))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
296
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
297 elif kind is END:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
298 tag = data
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
299 if not tag.namespace or tag in namespace:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
300 yield Markup('</%s>' % tag.localname)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
301
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
302 elif kind is TEXT:
73
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
303 yield escape(data, quotes=False)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
304
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
305 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
306 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
307
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
308 elif kind is DOCTYPE and not have_doctype:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
309 name, pubid, sysid = data
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
310 buf = ['<!DOCTYPE %s']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
311 if pubid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
312 buf += [' PUBLIC "%s"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
313 elif sysid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
314 buf += [' SYSTEM']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
315 if sysid:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
316 buf += [' "%s"']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
317 buf += ['>\n']
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
318 yield Markup(''.join(buf), *filter(None, data))
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
319 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
320
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
321 elif kind is START_NS and data[1] not in ns_mapping:
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
322 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
323
105
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
324 elif kind is PI:
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
325 yield Markup('<?%s %s?>' % data)
334a338847af Include processing instructions in serialized streams.
cmlenz
parents: 96
diff changeset
326
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
327
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
328 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
329 """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
330 stream."""
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
331
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
332 _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
333 _LINE_COLLAPSE = re.compile('\n{2,}')
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
334
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
335 def __init__(self, preserve=None):
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
336 """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
337
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
338 @param preserve: a sequence of tag names for which white-space should
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
339 be ignored.
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
340 """
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
341 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
342 preserve = []
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
343 self.preserve = frozenset(preserve)
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
344
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
345 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
346 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
347 collapse_lines = self._LINE_COLLAPSE.sub
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
348 mjoin = Markup('').join
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
349 preserve = [False]
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
350 append_preserve = preserve.append
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
351 pop_preserve = preserve.pop
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
352
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
353 textbuf = []
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
354 append_text = textbuf.append
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
355 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
356 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
357 if kind is TEXT:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
358 append_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
359 else:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
360 if kind is START:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
361 append_preserve(data[0] in self.preserve or
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
362 data[1].get('xml:space') == 'preserve')
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
363 if textbuf:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
364 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
365 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
366 del textbuf[:]
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
367 else:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
368 text = escape(pop_text(), quotes=False)
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
369 if not preserve[-1]:
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
370 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
371 yield TEXT, Markup(text), pos
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
372 if kind is END:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
373 pop_preserve()
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 123
diff changeset
374 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
375 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
376
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 109
diff changeset
377
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 20
diff changeset
378 class _PushbackIterator(object):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
379 """A simple wrapper for iterators that allows pushing items back on the
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
380 queue via the `pushback()` method.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
381
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
382 That can effectively be used to peek at the next item."""
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
383 __slots__ = ['iterable', 'buf']
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
384
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
385 def __init__(self, iterable):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
386 self.iterable = iter(iterable)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
387 self.buf = []
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
388
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
389 def __iter__(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
390 return self
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
391
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
392 def next(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
393 if self.buf:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
394 return self.buf.pop(0)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
395 return self.iterable.next()
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
396
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
397 def pushback(self, item):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
398 self.buf.append(item)
Copyright (C) 2012-2017 Edgewall Software