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