Mercurial > genshi > genshi-test
annotate genshi/output.py @ 915:9fafb35032a1 experimental-py3k
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
* default input and output encodings changed from UTF-8 to None (i.e. unicode strings)
* Namespace and QName objects do not call stringrepr in __repr__ in Python 3 since repr() returns a unicode string there.
* track changes to expat parser in Python 3 (mostly it accepts bytes instead of strings)
author | hodgestar |
---|---|
date | Sun, 24 Oct 2010 22:08:11 +0000 |
parents | 72d56a8441e8 |
children |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
3 # Copyright (C) 2006-2009 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
230 | 8 # are also available at http://genshi.edgewall.org/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
230 | 12 # history and logs, available at http://genshi.edgewall.org/log/. |
1 | 13 |
14 """This module provides different kinds of serialization methods for XML event | |
15 streams. | |
16 """ | |
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 |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
19 import re |
1 | 20 |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
21 from genshi.core import escape, Attrs, Markup, Namespace, QName, StreamEventKind |
460
6b5544bb5a99
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
448
diff
changeset
|
22 from genshi.core import START, END, TEXT, XML_DECL, DOCTYPE, START_NS, END_NS, \ |
402
cc7f5b3fbbed
Fix output of namespace declarations for namespace URLs appearing more than once in a stream. Thanks to Jeff Cutsinger for reporting the problem.
cmlenz
parents:
397
diff
changeset
|
23 START_CDATA, END_CDATA, PI, COMMENT, XML_NAMESPACE |
1 | 24 |
462 | 25 __all__ = ['encode', 'get_serializer', 'DocType', 'XMLSerializer', |
26 'XHTMLSerializer', 'HTMLSerializer', 'TextSerializer'] | |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
27 __docformat__ = 'restructuredtext en' |
1 | 28 |
863 | 29 |
915
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
863
diff
changeset
|
30 def encode(iterator, method='xml', encoding=None, out=None): |
462 | 31 """Encode serializer output into a string. |
32 | |
33 :param iterator: the iterator returned from serializing a stream (basically | |
34 any iterator that yields unicode objects) | |
35 :param method: the serialization method; determines how characters not | |
36 representable in the specified encoding are treated | |
37 :param encoding: how the output string should be encoded; if set to `None`, | |
38 this method returns a `unicode` object | |
688
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
39 :param out: a file-like object that the output should be written to |
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
40 instead of being returned as one big string; note that if |
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
41 this is a file or socket (or similar), the `encoding` must |
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
42 not be `None` (that is, the output must be encoded) |
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
43 :return: a `str` or `unicode` object (depending on the `encoding` |
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
44 parameter), or `None` if the `out` parameter is provided |
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
45 |
462 | 46 :since: version 0.4.1 |
688
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
47 :note: Changed in 0.5: added the `out` parameter |
462 | 48 """ |
49 if encoding is not None: | |
50 errors = 'replace' | |
51 if method != 'text' and not isinstance(method, TextSerializer): | |
52 errors = 'xmlcharrefreplace' | |
688
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
53 _encode = lambda string: string.encode(encoding, errors) |
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
54 else: |
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
55 _encode = lambda string: string |
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
56 if out is None: |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
57 return _encode(''.join(list(iterator))) |
688
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
58 for chunk in iterator: |
88814cc26d2b
The `Stream.render` now accepts an optional `out` parameter that can be used to pass in a writable file-like object to use for assembling the output, instead of building a big string and returning it.
cmlenz
parents:
672
diff
changeset
|
59 out.write(_encode(chunk)) |
462 | 60 |
863 | 61 |
462 | 62 def get_serializer(method='xml', **kwargs): |
63 """Return a serializer object for the given method. | |
64 | |
65 :param method: the serialization method; can be either "xml", "xhtml", | |
66 "html", "text", or a custom serializer class | |
67 | |
68 Any additional keyword arguments are passed to the serializer, and thus | |
69 depend on the `method` parameter value. | |
70 | |
71 :see: `XMLSerializer`, `XHTMLSerializer`, `HTMLSerializer`, `TextSerializer` | |
72 :since: version 0.4.1 | |
73 """ | |
74 if isinstance(method, basestring): | |
75 method = {'xml': XMLSerializer, | |
76 'xhtml': XHTMLSerializer, | |
77 'html': HTMLSerializer, | |
78 'text': TextSerializer}[method.lower()] | |
79 return method(**kwargs) | |
80 | |
1 | 81 |
85 | 82 class DocType(object): |
83 """Defines a number of commonly used DOCTYPE declarations as constants.""" | |
84 | |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
85 HTML_STRICT = ( |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
86 'html', '-//W3C//DTD HTML 4.01//EN', |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
87 'http://www.w3.org/TR/html4/strict.dtd' |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
88 ) |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
89 HTML_TRANSITIONAL = ( |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
90 'html', '-//W3C//DTD HTML 4.01 Transitional//EN', |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
91 'http://www.w3.org/TR/html4/loose.dtd' |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
92 ) |
464
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
93 HTML_FRAMESET = ( |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
94 'html', '-//W3C//DTD HTML 4.01 Frameset//EN', |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
95 'http://www.w3.org/TR/html4/frameset.dtd' |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
96 ) |
85 | 97 HTML = HTML_STRICT |
98 | |
464
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
99 HTML5 = ('html', None, None) |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
100 |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
101 XHTML_STRICT = ( |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
102 'html', '-//W3C//DTD XHTML 1.0 Strict//EN', |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
103 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd' |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
104 ) |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
105 XHTML_TRANSITIONAL = ( |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
106 'html', '-//W3C//DTD XHTML 1.0 Transitional//EN', |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
107 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd' |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
108 ) |
464
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
109 XHTML_FRAMESET = ( |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
110 'html', '-//W3C//DTD XHTML 1.0 Frameset//EN', |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
111 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd' |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
112 ) |
85 | 113 XHTML = XHTML_STRICT |
114 | |
729 | 115 XHTML11 = ( |
116 'html', '-//W3C//DTD XHTML 1.1//EN', | |
117 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd' | |
118 ) | |
119 | |
663 | 120 SVG_FULL = ( |
121 'svg', '-//W3C//DTD SVG 1.1//EN', | |
122 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd' | |
123 ) | |
124 SVG_BASIC = ( | |
125 'svg', '-//W3C//DTD SVG Basic 1.1//EN', | |
126 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-basic.dtd' | |
127 ) | |
128 SVG_TINY = ( | |
129 'svg', '-//W3C//DTD SVG Tiny 1.1//EN', | |
130 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd' | |
131 ) | |
132 SVG = SVG_FULL | |
133 | |
822
ce5ad2d540b3
Get rid of some Python 2.3 legacy that's no longer needed now that 2.4 is the baseline.
cmlenz
parents:
750
diff
changeset
|
134 @classmethod |
464
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
135 def get(cls, name): |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
136 """Return the ``(name, pubid, sysid)`` tuple of the ``DOCTYPE`` |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
137 declaration for the specified name. |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
138 |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
139 The following names are recognized in this version: |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
140 * "html" or "html-strict" for the HTML 4.01 strict DTD |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
141 * "html-transitional" for the HTML 4.01 transitional DTD |
745 | 142 * "html-frameset" for the HTML 4.01 frameset DTD |
464
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
143 * "html5" for the ``DOCTYPE`` proposed for HTML5 |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
144 * "xhtml" or "xhtml-strict" for the XHTML 1.0 strict DTD |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
145 * "xhtml-transitional" for the XHTML 1.0 transitional DTD |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
146 * "xhtml-frameset" for the XHTML 1.0 frameset DTD |
729 | 147 * "xhtml11" for the XHTML 1.1 DTD |
663 | 148 * "svg" or "svg-full" for the SVG 1.1 DTD |
149 * "svg-basic" for the SVG Basic 1.1 DTD | |
150 * "svg-tiny" for the SVG Tiny 1.1 DTD | |
464
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
151 |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
152 :param name: the name of the ``DOCTYPE`` |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
153 :return: the ``(name, pubid, sysid)`` tuple for the requested |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
154 ``DOCTYPE``, or ``None`` if the name is not recognized |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
155 :since: version 0.4.1 |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
156 """ |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
157 return { |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
158 'html': cls.HTML, 'html-strict': cls.HTML_STRICT, |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
159 'html-transitional': DocType.HTML_TRANSITIONAL, |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
160 'html-frameset': DocType.HTML_FRAMESET, |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
161 'html5': cls.HTML5, |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
162 'xhtml': cls.XHTML, 'xhtml-strict': cls.XHTML_STRICT, |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
163 'xhtml-transitional': cls.XHTML_TRANSITIONAL, |
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
164 'xhtml-frameset': cls.XHTML_FRAMESET, |
729 | 165 'xhtml11': cls.XHTML11, |
663 | 166 'svg': cls.SVG, 'svg-full': cls.SVG_FULL, |
167 'svg-basic': cls.SVG_BASIC, | |
168 'svg-tiny': cls.SVG_TINY | |
464
dafc6f4c20fb
Move the mapping of doctype names to tuples out of the plugin into the `DocType` class.
cmlenz
parents:
462
diff
changeset
|
169 }.get(name.lower()) |
448 | 170 |
85 | 171 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
172 class XMLSerializer(object): |
1 | 173 """Produces XML text from an event stream. |
174 | |
230 | 175 >>> from genshi.builder import tag |
20 | 176 >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True)) |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
177 >>> print(''.join(XMLSerializer()(elem.generate()))) |
1 | 178 <div><a href="foo"/><br/><hr noshade="True"/></div> |
179 """ | |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
180 |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
181 _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
|
182 |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
183 def __init__(self, doctype=None, strip_whitespace=True, |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
184 namespace_prefixes=None, cache=True): |
85 | 185 """Initialize the XML serializer. |
186 | |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
187 :param doctype: a ``(name, pubid, sysid)`` tuple that represents the |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
188 DOCTYPE declaration that should be included at the top |
494
8a90e761d5ff
The `doctype` parameter for serializers can now be a string.
cmlenz
parents:
464
diff
changeset
|
189 of the generated output, or the name of a DOCTYPE as |
8a90e761d5ff
The `doctype` parameter for serializers can now be a string.
cmlenz
parents:
464
diff
changeset
|
190 defined in `DocType.get` |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
191 :param strip_whitespace: whether extraneous whitespace should be |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
192 stripped from the output |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
193 :param cache: whether to cache the text output per event, which |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
194 improves performance for repetitive markup |
494
8a90e761d5ff
The `doctype` parameter for serializers can now be a string.
cmlenz
parents:
464
diff
changeset
|
195 :note: Changed in 0.4.2: The `doctype` parameter can now be a string. |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
196 :note: Changed in 0.6: The `cache` parameter was added |
85 | 197 """ |
212
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
198 self.filters = [EmptyTagFilter()] |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
199 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
|
200 self.filters.append(WhitespaceFilter(self._PRESERVE_SPACE)) |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
201 self.filters.append(NamespaceFlattener(prefixes=namespace_prefixes, |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
202 cache=cache)) |
671
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
203 if doctype: |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
204 self.filters.append(DocTypeInserter(doctype)) |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
205 self.cache = cache |
1 | 206 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
207 def __call__(self, stream): |
460
6b5544bb5a99
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
448
diff
changeset
|
208 have_decl = have_doctype = False |
143
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
209 in_cdata = False |
1 | 210 |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
211 cache = {} |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
212 cache_get = cache.get |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
213 if self.cache: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
214 def _emit(kind, input, output): |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
215 cache[kind, input] = output |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
216 return output |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
217 else: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
218 def _emit(kind, input, output): |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
219 return output |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
220 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
221 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
|
222 stream = filter_(stream) |
1 | 223 for kind, data, pos in stream: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
224 cached = cache_get((kind, data)) |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
225 if cached is not None: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
226 yield cached |
1 | 227 |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
228 elif kind is START or kind is EMPTY: |
1 | 229 tag, attrib = data |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
230 buf = ['<', tag] |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
231 for attr, value in attrib: |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
232 buf += [' ', attr, '="', escape(value), '"'] |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
233 buf.append(kind is EMPTY and '/>' or '>') |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
234 yield _emit(kind, data, Markup(''.join(buf))) |
1 | 235 |
69 | 236 elif kind is END: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
237 yield _emit(kind, data, Markup('</%s>' % data)) |
1 | 238 |
69 | 239 elif kind is TEXT: |
143
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
240 if in_cdata: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
241 yield _emit(kind, data, data) |
143
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
242 else: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
243 yield _emit(kind, data, escape(data, quotes=False)) |
1 | 244 |
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
|
245 elif kind is COMMENT: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
246 yield _emit(kind, data, Markup('<!--%s-->' % data)) |
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
|
247 |
460
6b5544bb5a99
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
448
diff
changeset
|
248 elif kind is XML_DECL and not have_decl: |
6b5544bb5a99
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
448
diff
changeset
|
249 version, encoding, standalone = data |
6b5544bb5a99
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
448
diff
changeset
|
250 buf = ['<?xml version="%s"' % version] |
6b5544bb5a99
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
448
diff
changeset
|
251 if encoding: |
6b5544bb5a99
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
448
diff
changeset
|
252 buf.append(' encoding="%s"' % encoding) |
6b5544bb5a99
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
448
diff
changeset
|
253 if standalone != -1: |
6b5544bb5a99
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
448
diff
changeset
|
254 standalone = standalone and 'yes' or 'no' |
6b5544bb5a99
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
448
diff
changeset
|
255 buf.append(' standalone="%s"' % standalone) |
6b5544bb5a99
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
448
diff
changeset
|
256 buf.append('?>\n') |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
257 yield Markup(''.join(buf)) |
460
6b5544bb5a99
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
448
diff
changeset
|
258 have_decl = True |
6b5544bb5a99
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
448
diff
changeset
|
259 |
136 | 260 elif kind is DOCTYPE and not have_doctype: |
261 name, pubid, sysid = data | |
262 buf = ['<!DOCTYPE %s'] | |
263 if pubid: | |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
264 buf.append(' PUBLIC "%s"') |
136 | 265 elif sysid: |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
266 buf.append(' SYSTEM') |
136 | 267 if sysid: |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
268 buf.append(' "%s"') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
269 buf.append('>\n') |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
270 yield Markup(''.join(buf)) % tuple([p for p in data if p]) |
136 | 271 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
|
272 |
143
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
273 elif kind is START_CDATA: |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
274 yield Markup('<![CDATA[') |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
275 in_cdata = True |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
276 |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
277 elif kind is END_CDATA: |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
278 yield Markup(']]>') |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
279 in_cdata = False |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
280 |
105
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
281 elif kind is PI: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
282 yield _emit(kind, data, Markup('<?%s %s?>' % data)) |
105
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
283 |
1 | 284 |
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
|
285 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
|
286 """Produces XHTML text from an event stream. |
1 | 287 |
230 | 288 >>> from genshi.builder import tag |
20 | 289 >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True)) |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
290 >>> 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
|
291 <div><a href="foo"></a><br /><hr noshade="noshade" /></div> |
1 | 292 """ |
293 | |
294 _EMPTY_ELEMS = frozenset(['area', 'base', 'basefont', 'br', 'col', 'frame', | |
295 'hr', 'img', 'input', 'isindex', 'link', 'meta', | |
296 'param']) | |
297 _BOOLEAN_ATTRS = frozenset(['selected', 'checked', 'compact', 'declare', | |
298 'defer', 'disabled', 'ismap', 'multiple', | |
299 'nohref', 'noresize', 'noshade', 'nowrap']) | |
346
2304e080ec07
Whitespace was not getting preserved in HTML `<pre>` elements that contained other HTML elements.
cmlenz
parents:
345
diff
changeset
|
300 _PRESERVE_SPACE = frozenset([ |
2304e080ec07
Whitespace was not getting preserved in HTML `<pre>` elements that contained other HTML elements.
cmlenz
parents:
345
diff
changeset
|
301 QName('pre'), QName('http://www.w3.org/1999/xhtml}pre'), |
2304e080ec07
Whitespace was not getting preserved in HTML `<pre>` elements that contained other HTML elements.
cmlenz
parents:
345
diff
changeset
|
302 QName('textarea'), QName('http://www.w3.org/1999/xhtml}textarea') |
2304e080ec07
Whitespace was not getting preserved in HTML `<pre>` elements that contained other HTML elements.
cmlenz
parents:
345
diff
changeset
|
303 ]) |
1 | 304 |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
305 def __init__(self, doctype=None, strip_whitespace=True, |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
306 namespace_prefixes=None, drop_xml_decl=True, cache=True): |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
307 super(XHTMLSerializer, self).__init__(doctype, False) |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
308 self.filters = [EmptyTagFilter()] |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
309 if strip_whitespace: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
310 self.filters.append(WhitespaceFilter(self._PRESERVE_SPACE)) |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
311 namespace_prefixes = namespace_prefixes or {} |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
312 namespace_prefixes['http://www.w3.org/1999/xhtml'] = '' |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
313 self.filters.append(NamespaceFlattener(prefixes=namespace_prefixes, |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
314 cache=cache)) |
671
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
315 if doctype: |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
316 self.filters.append(DocTypeInserter(doctype)) |
729 | 317 self.drop_xml_decl = drop_xml_decl |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
318 self.cache = cache |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
319 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
320 def __call__(self, stream): |
136 | 321 boolean_attrs = self._BOOLEAN_ATTRS |
322 empty_elems = self._EMPTY_ELEMS | |
729 | 323 drop_xml_decl = self.drop_xml_decl |
324 have_decl = have_doctype = False | |
143
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
325 in_cdata = False |
1 | 326 |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
327 cache = {} |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
328 cache_get = cache.get |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
329 if self.cache: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
330 def _emit(kind, input, output): |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
331 cache[kind, input] = output |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
332 return output |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
333 else: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
334 def _emit(kind, input, output): |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
335 return output |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
336 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
337 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
|
338 stream = filter_(stream) |
1 | 339 for kind, data, pos in stream: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
340 cached = cache_get((kind, data)) |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
341 if cached is not None: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
342 yield cached |
1 | 343 |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
344 elif kind is START or kind is EMPTY: |
1 | 345 tag, attrib = data |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
346 buf = ['<', tag] |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
347 for attr, value in attrib: |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
348 if attr in boolean_attrs: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
349 value = attr |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
350 elif attr == 'xml:lang' and 'lang' not in attrib: |
524
49848aaa7839
Add special handling for `xml:lang` to HTML/XHTML serialization.
cmlenz
parents:
494
diff
changeset
|
351 buf += [' lang="', escape(value), '"'] |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
352 elif attr == 'xml:space': |
689
46a21414151b
The XHTML serializer now strips `xml:space` attributes as they are only allowed on very few tags.
cmlenz
parents:
688
diff
changeset
|
353 continue |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
354 buf += [' ', attr, '="', escape(value), '"'] |
212
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
355 if kind is EMPTY: |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
356 if tag in empty_elems: |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
357 buf.append(' />') |
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
|
358 else: |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
359 buf.append('></%s>' % tag) |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
360 else: |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
361 buf.append('>') |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
362 yield _emit(kind, data, 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
|
363 |
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
|
364 elif kind is END: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
365 yield _emit(kind, data, Markup('</%s>' % data)) |
96
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
366 |
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
|
367 elif kind is TEXT: |
143
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
368 if in_cdata: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
369 yield _emit(kind, data, data) |
143
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
370 else: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
371 yield _emit(kind, data, escape(data, quotes=False)) |
96
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
372 |
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
|
373 elif kind is COMMENT: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
374 yield _emit(kind, data, Markup('<!--%s-->' % data)) |
96
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
375 |
136 | 376 elif kind is DOCTYPE and not have_doctype: |
377 name, pubid, sysid = data | |
378 buf = ['<!DOCTYPE %s'] | |
379 if pubid: | |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
380 buf.append(' PUBLIC "%s"') |
136 | 381 elif sysid: |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
382 buf.append(' SYSTEM') |
136 | 383 if sysid: |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
384 buf.append(' "%s"') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
385 buf.append('>\n') |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
386 yield Markup(''.join(buf)) % tuple([p for p in data if p]) |
136 | 387 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
|
388 |
729 | 389 elif kind is XML_DECL and not have_decl and not drop_xml_decl: |
390 version, encoding, standalone = data | |
391 buf = ['<?xml version="%s"' % version] | |
392 if encoding: | |
393 buf.append(' encoding="%s"' % encoding) | |
394 if standalone != -1: | |
395 standalone = standalone and 'yes' or 'no' | |
396 buf.append(' standalone="%s"' % standalone) | |
397 buf.append('?>\n') | |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
398 yield Markup(''.join(buf)) |
729 | 399 have_decl = True |
400 | |
143
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
401 elif kind is START_CDATA: |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
402 yield Markup('<![CDATA[') |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
403 in_cdata = True |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
404 |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
405 elif kind is END_CDATA: |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
406 yield Markup(']]>') |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
407 in_cdata = False |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
408 |
105
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
409 elif kind is PI: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
410 yield _emit(kind, data, Markup('<?%s %s?>' % data)) |
105
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
411 |
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
|
412 |
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
|
413 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
|
414 """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
|
415 |
230 | 416 >>> from genshi.builder import tag |
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
|
417 >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True)) |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
418 >>> 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
|
419 <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
|
420 """ |
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
|
421 |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
422 _NOESCAPE_ELEMS = frozenset([ |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
423 QName('script'), QName('http://www.w3.org/1999/xhtml}script'), |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
424 QName('style'), QName('http://www.w3.org/1999/xhtml}style') |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
425 ]) |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
426 |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
427 def __init__(self, doctype=None, strip_whitespace=True, cache=True): |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
428 """Initialize the HTML serializer. |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
429 |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
430 :param doctype: a ``(name, pubid, sysid)`` tuple that represents the |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
431 DOCTYPE declaration that should be included at the top |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
432 of the generated output |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
433 :param strip_whitespace: whether extraneous whitespace should be |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
434 stripped from the output |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
435 :param cache: whether to cache the text output per event, which |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
436 improves performance for repetitive markup |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
437 :note: Changed in 0.6: The `cache` parameter was added |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
438 """ |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
439 super(HTMLSerializer, self).__init__(doctype, False) |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
440 self.filters = [EmptyTagFilter()] |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
441 if strip_whitespace: |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
442 self.filters.append(WhitespaceFilter(self._PRESERVE_SPACE, |
305 | 443 self._NOESCAPE_ELEMS)) |
524
49848aaa7839
Add special handling for `xml:lang` to HTML/XHTML serialization.
cmlenz
parents:
494
diff
changeset
|
444 self.filters.append(NamespaceFlattener(prefixes={ |
49848aaa7839
Add special handling for `xml:lang` to HTML/XHTML serialization.
cmlenz
parents:
494
diff
changeset
|
445 'http://www.w3.org/1999/xhtml': '' |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
446 }, cache=cache)) |
671
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
447 if doctype: |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
448 self.filters.append(DocTypeInserter(doctype)) |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
449 self.cache = True |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
450 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
451 def __call__(self, stream): |
136 | 452 boolean_attrs = self._BOOLEAN_ATTRS |
453 empty_elems = self._EMPTY_ELEMS | |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
454 noescape_elems = self._NOESCAPE_ELEMS |
96
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
455 have_doctype = False |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
456 noescape = False |
96
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
457 |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
458 cache = {} |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
459 cache_get = cache.get |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
460 if self.cache: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
461 def _emit(kind, input, output): |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
462 cache[kind, input] = output |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
463 return output |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
464 else: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
465 def _emit(kind, input, output): |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
466 return output |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
467 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
468 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
|
469 stream = filter_(stream) |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
470 for kind, data, _ in stream: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
471 output = cache_get((kind, data)) |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
472 if output is not None: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
473 yield output |
831 | 474 if (kind is START or kind is EMPTY) \ |
475 and data[0] in noescape_elems: | |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
476 noescape = True |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
477 elif kind is END: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
478 noescape = False |
96
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
89
diff
changeset
|
479 |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
480 elif kind is START or kind is EMPTY: |
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
|
481 tag, attrib = data |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
482 buf = ['<', tag] |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
483 for attr, value in attrib: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
484 if attr in boolean_attrs: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
485 if value: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
486 buf += [' ', attr] |
524
49848aaa7839
Add special handling for `xml:lang` to HTML/XHTML serialization.
cmlenz
parents:
494
diff
changeset
|
487 elif ':' in attr: |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
488 if attr == 'xml:lang' and 'lang' not in attrib: |
524
49848aaa7839
Add special handling for `xml:lang` to HTML/XHTML serialization.
cmlenz
parents:
494
diff
changeset
|
489 buf += [' lang="', escape(value), '"'] |
49848aaa7839
Add special handling for `xml:lang` to HTML/XHTML serialization.
cmlenz
parents:
494
diff
changeset
|
490 elif attr != 'xmlns': |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
491 buf += [' ', attr, '="', escape(value), '"'] |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
492 buf.append('>') |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
493 if kind is EMPTY: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
494 if tag not in empty_elems: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
495 buf.append('</%s>' % tag) |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
496 yield _emit(kind, data, Markup(''.join(buf))) |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
497 if tag in noescape_elems: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
498 noescape = True |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
499 |
69 | 500 elif kind is END: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
501 yield _emit(kind, data, Markup('</%s>' % data)) |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
502 noescape = False |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
503 |
69 | 504 elif kind is TEXT: |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
505 if noescape: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
506 yield _emit(kind, data, data) |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
507 else: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
508 yield _emit(kind, data, escape(data, quotes=False)) |
1 | 509 |
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
|
510 elif kind is COMMENT: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
511 yield _emit(kind, data, Markup('<!--%s-->' % data)) |
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
|
512 |
136 | 513 elif kind is DOCTYPE and not have_doctype: |
514 name, pubid, sysid = data | |
515 buf = ['<!DOCTYPE %s'] | |
516 if pubid: | |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
517 buf.append(' PUBLIC "%s"') |
136 | 518 elif sysid: |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
519 buf.append(' SYSTEM') |
136 | 520 if sysid: |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
521 buf.append(' "%s"') |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
346
diff
changeset
|
522 buf.append('>\n') |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
523 yield Markup(''.join(buf)) % tuple([p for p in data if p]) |
136 | 524 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
|
525 |
105
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
526 elif kind is PI: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
527 yield _emit(kind, data, Markup('<?%s %s?>' % data)) |
105
334a338847af
Include processing instructions in serialized streams.
cmlenz
parents:
96
diff
changeset
|
528 |
1 | 529 |
200
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
530 class TextSerializer(object): |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
531 """Produces plain text from an event stream. |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
532 |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
533 Only text events are included in the output. Unlike the other serializer, |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
534 special XML characters are not escaped: |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
535 |
230 | 536 >>> from genshi.builder import tag |
200
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
537 >>> elem = tag.div(tag.a('<Hello!>', href='foo'), tag.br) |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
538 >>> print(elem) |
200
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
539 <div><a href="foo"><Hello!></a><br/></div> |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
540 >>> print(''.join(TextSerializer()(elem.generate()))) |
200
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
541 <Hello!> |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
542 |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
543 If text events contain literal markup (instances of the `Markup` class), |
658
a445c9e5ee96
The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146.
cmlenz
parents:
524
diff
changeset
|
544 that markup is by default passed through unchanged: |
200
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
545 |
658
a445c9e5ee96
The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146.
cmlenz
parents:
524
diff
changeset
|
546 >>> elem = tag.div(Markup('<a href="foo">Hello & Bye!</a><br/>')) |
863 | 547 >>> print(elem.generate().render(TextSerializer, encoding=None)) |
658
a445c9e5ee96
The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146.
cmlenz
parents:
524
diff
changeset
|
548 <a href="foo">Hello & Bye!</a><br/> |
a445c9e5ee96
The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146.
cmlenz
parents:
524
diff
changeset
|
549 |
740
915ccf659797
Fix a bad reference in the `TextSerializer` docstring.
cmlenz
parents:
729
diff
changeset
|
550 You can use the ``strip_markup`` to change this behavior, so that tags and |
658
a445c9e5ee96
The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146.
cmlenz
parents:
524
diff
changeset
|
551 entities are stripped from the output (or in the case of entities, |
a445c9e5ee96
The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146.
cmlenz
parents:
524
diff
changeset
|
552 replaced with the equivalent character): |
a445c9e5ee96
The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146.
cmlenz
parents:
524
diff
changeset
|
553 |
863 | 554 >>> print(elem.generate().render(TextSerializer, strip_markup=True, |
555 ... encoding=None)) | |
658
a445c9e5ee96
The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146.
cmlenz
parents:
524
diff
changeset
|
556 Hello & Bye! |
200
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
557 """ |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
558 |
658
a445c9e5ee96
The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146.
cmlenz
parents:
524
diff
changeset
|
559 def __init__(self, strip_markup=False): |
740
915ccf659797
Fix a bad reference in the `TextSerializer` docstring.
cmlenz
parents:
729
diff
changeset
|
560 """Create the serializer. |
915ccf659797
Fix a bad reference in the `TextSerializer` docstring.
cmlenz
parents:
729
diff
changeset
|
561 |
915ccf659797
Fix a bad reference in the `TextSerializer` docstring.
cmlenz
parents:
729
diff
changeset
|
562 :param strip_markup: whether markup (tags and encoded characters) found |
915ccf659797
Fix a bad reference in the `TextSerializer` docstring.
cmlenz
parents:
729
diff
changeset
|
563 in the text should be removed |
915ccf659797
Fix a bad reference in the `TextSerializer` docstring.
cmlenz
parents:
729
diff
changeset
|
564 """ |
658
a445c9e5ee96
The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146.
cmlenz
parents:
524
diff
changeset
|
565 self.strip_markup = strip_markup |
a445c9e5ee96
The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146.
cmlenz
parents:
524
diff
changeset
|
566 |
200
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
567 def __call__(self, stream): |
658
a445c9e5ee96
The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146.
cmlenz
parents:
524
diff
changeset
|
568 strip_markup = self.strip_markup |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
569 for event in stream: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
570 if event[0] is TEXT: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
571 data = event[1] |
658
a445c9e5ee96
The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146.
cmlenz
parents:
524
diff
changeset
|
572 if strip_markup and type(data) is Markup: |
200
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
573 data = data.striptags().stripentities() |
201
0f16c907077e
The `TextSerializer` should produce `unicode` objects, not `Markup` objects.
cmlenz
parents:
200
diff
changeset
|
574 yield unicode(data) |
200
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
575 |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
178
diff
changeset
|
576 |
212
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
577 class EmptyTagFilter(object): |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
578 """Combines `START` and `STOP` events into `EMPTY` events for elements that |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
579 have no contents. |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
580 """ |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
581 |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
582 EMPTY = StreamEventKind('EMPTY') |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
583 |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
584 def __call__(self, stream): |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
585 prev = (None, None, None) |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
586 for ev in stream: |
212
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
587 if prev[0] is START: |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
588 if ev[0] is END: |
212
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
589 prev = EMPTY, prev[1], prev[2] |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
590 yield prev |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
591 continue |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
592 else: |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
593 yield prev |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
594 if ev[0] is not START: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
595 yield ev |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
596 prev = ev |
212
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
597 |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
598 |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
599 EMPTY = EmptyTagFilter.EMPTY |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
600 |
e8c43127d9a9
Refactored the handling of empty tags in the serializer: use an `EmptyTagFilter` that combines adjacent start/end events, instead of the generic pushback-iterator.
cmlenz
parents:
201
diff
changeset
|
601 |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
602 class NamespaceFlattener(object): |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
603 r"""Output stream filter that removes namespace information from the stream, |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
604 instead adding namespace attributes and prefixes as needed. |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
605 |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
606 :param prefixes: optional mapping of namespace URIs to prefixes |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
607 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
608 >>> from genshi.input import XML |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
609 >>> xml = XML('''<doc xmlns="NS1" xmlns:two="NS2"> |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
610 ... <two:item/> |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
611 ... </doc>''') |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
612 >>> for kind, data, pos in NamespaceFlattener()(xml): |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
613 ... print('%s %r' % (kind, data)) |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
614 START (u'doc', Attrs([('xmlns', u'NS1'), (u'xmlns:two', u'NS2')])) |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
615 TEXT u'\n ' |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
616 START (u'two:item', Attrs()) |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
617 END u'two:item' |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
618 TEXT u'\n' |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
619 END u'doc' |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
620 """ |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
621 |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
622 def __init__(self, prefixes=None, cache=True): |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
623 self.prefixes = {XML_NAMESPACE.uri: 'xml'} |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
624 if prefixes is not None: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
625 self.prefixes.update(prefixes) |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
626 self.cache = cache |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
627 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
628 def __call__(self, stream): |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
629 cache = {} |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
630 cache_get = cache.get |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
631 if self.cache: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
632 def _emit(kind, input, output, pos): |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
633 cache[kind, input] = output |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
634 return kind, output, pos |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
635 else: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
636 def _emit(kind, input, output, pos): |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
637 return output |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
638 |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
639 prefixes = dict([(v, [k]) for k, v in self.prefixes.items()]) |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
640 namespaces = {XML_NAMESPACE.uri: ['xml']} |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
641 def _push_ns(prefix, uri): |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
642 namespaces.setdefault(uri, []).append(prefix) |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
643 prefixes.setdefault(prefix, []).append(uri) |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
644 cache.clear() |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
645 def _pop_ns(prefix): |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
646 uris = prefixes.get(prefix) |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
647 uri = uris.pop() |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
648 if not uris: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
649 del prefixes[prefix] |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
650 if uri not in uris or uri != uris[-1]: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
651 uri_prefixes = namespaces[uri] |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
652 uri_prefixes.pop() |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
653 if not uri_prefixes: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
654 del namespaces[uri] |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
655 cache.clear() |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
656 return uri |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
657 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
658 ns_attrs = [] |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
659 _push_ns_attr = ns_attrs.append |
437 | 660 def _make_ns_attr(prefix, uri): |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
661 return 'xmlns%s' % (prefix and ':%s' % prefix or ''), uri |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
662 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
663 def _gen_prefix(): |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
664 val = 0 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
665 while 1: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
666 val += 1 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
667 yield 'ns%d' % val |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
668 _gen_prefix = _gen_prefix().next |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
669 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
670 for kind, data, pos in stream: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
671 output = cache_get((kind, data)) |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
672 if output is not None: |
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
673 yield kind, output, pos |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
674 |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
675 elif kind is START or kind is EMPTY: |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
676 tag, attrs = data |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
677 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
678 tagname = tag.localname |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
679 tagns = tag.namespace |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
680 if tagns: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
681 if tagns in namespaces: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
682 prefix = namespaces[tagns][-1] |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
683 if prefix: |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
684 tagname = '%s:%s' % (prefix, tagname) |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
685 else: |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
686 _push_ns_attr(('xmlns', tagns)) |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
687 _push_ns('', tagns) |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
688 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
689 new_attrs = [] |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
690 for attr, value in attrs: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
691 attrname = attr.localname |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
692 attrns = attr.namespace |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
693 if attrns: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
694 if attrns not in namespaces: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
695 prefix = _gen_prefix() |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
696 _push_ns(prefix, attrns) |
412
29cddd600245
Actually write xmlns declaratons for generated attribute namespace prefixes.
cmlenz
parents:
410
diff
changeset
|
697 _push_ns_attr(('xmlns:%s' % prefix, attrns)) |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
698 else: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
699 prefix = namespaces[attrns][-1] |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
700 if prefix: |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
701 attrname = '%s:%s' % (prefix, attrname) |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
702 new_attrs.append((attrname, value)) |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
703 |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
704 yield _emit(kind, data, (tagname, Attrs(ns_attrs + new_attrs)), pos) |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
705 del ns_attrs[:] |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
706 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
707 elif kind is END: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
708 tagname = data.localname |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
709 tagns = data.namespace |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
710 if tagns: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
711 prefix = namespaces[tagns][-1] |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
712 if prefix: |
852
04945cd67dad
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
831
diff
changeset
|
713 tagname = '%s:%s' % (prefix, tagname) |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
714 yield _emit(kind, data, tagname, pos) |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
715 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
716 elif kind is START_NS: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
717 prefix, uri = data |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
718 if uri not in namespaces: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
719 prefix = prefixes.get(uri, [prefix])[-1] |
437 | 720 _push_ns_attr(_make_ns_attr(prefix, uri)) |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
721 _push_ns(prefix, uri) |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
722 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
723 elif kind is END_NS: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
724 if data in prefixes: |
829
e1c6163c5077
Add caching in the serialization stage, which speeds up the serialization of markup that has a lot of repetitive elements.
cmlenz
parents:
822
diff
changeset
|
725 uri = _pop_ns(data) |
437 | 726 if ns_attrs: |
727 attr = _make_ns_attr(data, uri) | |
728 if attr in ns_attrs: | |
729 ns_attrs.remove(attr) | |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
730 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
731 else: |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
732 yield kind, data, pos |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
733 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
734 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
735 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
|
736 """A filter that removes extraneous ignorable white space from the |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
737 stream. |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
738 """ |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
739 |
305 | 740 def __init__(self, preserve=None, noescape=None): |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
741 """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
|
742 |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
743 :param preserve: a set or sequence of tag names for which white-space |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
744 should be preserved |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
745 :param noescape: a set or sequence of tag names for which text content |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
746 should not be escaped |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
747 |
346
2304e080ec07
Whitespace was not getting preserved in HTML `<pre>` elements that contained other HTML elements.
cmlenz
parents:
345
diff
changeset
|
748 The `noescape` set is expected to refer to elements that cannot contain |
425
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
749 further child elements (such as ``<style>`` or ``<script>`` in HTML |
5b248708bbed
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
412
diff
changeset
|
750 documents). |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
751 """ |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
752 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
|
753 preserve = [] |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
754 self.preserve = frozenset(preserve) |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
755 if noescape is None: |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
756 noescape = [] |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
757 self.noescape = frozenset(noescape) |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
758 |
219 | 759 def __call__(self, stream, ctxt=None, space=XML_NAMESPACE['space'], |
760 trim_trailing_space=re.compile('[ \t]+(?=\n)').sub, | |
761 collapse_lines=re.compile('\n{2,}').sub): | |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
762 mjoin = Markup('').join |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
763 preserve_elems = self.preserve |
346
2304e080ec07
Whitespace was not getting preserved in HTML `<pre>` elements that contained other HTML elements.
cmlenz
parents:
345
diff
changeset
|
764 preserve = 0 |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
765 noescape_elems = self.noescape |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
766 noescape = False |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
767 |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
768 textbuf = [] |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
769 push_text = textbuf.append |
136 | 770 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
|
771 for kind, data, pos in chain(stream, [(None, None, None)]): |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
408
diff
changeset
|
772 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
773 if kind is TEXT: |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
774 if noescape: |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
775 data = Markup(data) |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
776 push_text(data) |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
777 else: |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
778 if textbuf: |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
779 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
|
780 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
|
781 del textbuf[:] |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
782 else: |
136 | 783 text = escape(pop_text(), quotes=False) |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
784 if not preserve: |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
109
diff
changeset
|
785 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
|
786 yield TEXT, Markup(text), pos |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
787 |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
788 if kind is START: |
346
2304e080ec07
Whitespace was not getting preserved in HTML `<pre>` elements that contained other HTML elements.
cmlenz
parents:
345
diff
changeset
|
789 tag, attrs = data |
2304e080ec07
Whitespace was not getting preserved in HTML `<pre>` elements that contained other HTML elements.
cmlenz
parents:
345
diff
changeset
|
790 if preserve or (tag in preserve_elems or |
2304e080ec07
Whitespace was not getting preserved in HTML `<pre>` elements that contained other HTML elements.
cmlenz
parents:
345
diff
changeset
|
791 attrs.get(space) == 'preserve'): |
2304e080ec07
Whitespace was not getting preserved in HTML `<pre>` elements that contained other HTML elements.
cmlenz
parents:
345
diff
changeset
|
792 preserve += 1 |
219 | 793 if not noescape and tag in noescape_elems: |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
794 noescape = True |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
795 |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
796 elif kind is END: |
346
2304e080ec07
Whitespace was not getting preserved in HTML `<pre>` elements that contained other HTML elements.
cmlenz
parents:
345
diff
changeset
|
797 noescape = False |
2304e080ec07
Whitespace was not getting preserved in HTML `<pre>` elements that contained other HTML elements.
cmlenz
parents:
345
diff
changeset
|
798 if preserve: |
2304e080ec07
Whitespace was not getting preserved in HTML `<pre>` elements that contained other HTML elements.
cmlenz
parents:
345
diff
changeset
|
799 preserve -= 1 |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
800 |
305 | 801 elif kind is START_CDATA: |
143
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
802 noescape = True |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
803 |
305 | 804 elif kind is END_CDATA: |
143
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
805 noescape = False |
ef761afcedff
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
141
diff
changeset
|
806 |
136 | 807 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
|
808 yield kind, data, pos |
671
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
809 |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
810 |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
811 class DocTypeInserter(object): |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
812 """A filter that inserts the DOCTYPE declaration in the correct location, |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
813 after the XML declaration. |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
814 """ |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
815 def __init__(self, doctype): |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
816 """Initialize the filter. |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
817 |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
818 :param doctype: DOCTYPE as a string or DocType object. |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
819 """ |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
820 if isinstance(doctype, basestring): |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
821 doctype = DocType.get(doctype) |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
822 self.doctype_event = (DOCTYPE, doctype, (None, -1, -1)) |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
823 |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
824 def __call__(self, stream): |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
825 doctype_inserted = False |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
826 for kind, data, pos in stream: |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
827 if not doctype_inserted: |
672
e69d95e80231
XML_DECL must be the absolute first item, so don't bother buffering whitespace.
athomas
parents:
671
diff
changeset
|
828 doctype_inserted = True |
e69d95e80231
XML_DECL must be the absolute first item, so don't bother buffering whitespace.
athomas
parents:
671
diff
changeset
|
829 if kind is XML_DECL: |
e69d95e80231
XML_DECL must be the absolute first item, so don't bother buffering whitespace.
athomas
parents:
671
diff
changeset
|
830 yield (kind, data, pos) |
e69d95e80231
XML_DECL must be the absolute first item, so don't bother buffering whitespace.
athomas
parents:
671
diff
changeset
|
831 yield self.doctype_event |
671
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
832 continue |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
833 yield self.doctype_event |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
834 |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
835 yield (kind, data, pos) |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
836 |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
837 if not doctype_inserted: |
f2090347ba6c
Add a stream filter to insert the XML DOCTYPE in the correct location (ie.
athomas
parents:
669
diff
changeset
|
838 yield self.doctype_event |