Mercurial > genshi > genshi-test
annotate genshi/core.py @ 397:d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
* Minor performance tweaks for the serializers
author | cmlenz |
---|---|
date | Tue, 02 Jan 2007 17:48:06 +0000 |
parents | d7da3fba7faf |
children | 32b283e1d310 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
66
822089ae65ce
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
54
diff
changeset
|
3 # Copyright (C) 2006 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 """Core classes for markup processing.""" | |
15 | |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
16 import operator |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
382
diff
changeset
|
17 |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
382
diff
changeset
|
18 from genshi.util import plaintext, stripentities, striptags |
1 | 19 |
377
79a7db5177e9
Add `Attrs` class to `genshi.core.__all__`, so that it can be imported directly from the `genshi` package.
cmlenz
parents:
345
diff
changeset
|
20 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Attrs', 'Namespace', |
79a7db5177e9
Add `Attrs` class to `genshi.core.__all__`, so that it can be imported directly from the `genshi` package.
cmlenz
parents:
345
diff
changeset
|
21 'QName'] |
1 | 22 |
23 | |
17
ad63ad459524
Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents:
10
diff
changeset
|
24 class StreamEventKind(str): |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
382
diff
changeset
|
25 """A kind of event on a markup stream.""" |
279
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
26 __slots__ = [] |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
27 _instances = {} |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
28 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
29 def __new__(cls, val): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
30 return cls._instances.setdefault(val, str.__new__(cls, val)) |
1 | 31 |
32 | |
33 class Stream(object): | |
34 """Represents a stream of markup events. | |
35 | |
36 This class is basically an iterator over the events. | |
37 | |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
382
diff
changeset
|
38 Stream events are tuples of the form: |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
382
diff
changeset
|
39 |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
382
diff
changeset
|
40 (kind, data, position) |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
382
diff
changeset
|
41 |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
382
diff
changeset
|
42 where `kind` is the event kind (such as `START`, `END`, `TEXT`, etc), `data` |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
382
diff
changeset
|
43 depends on the kind of event, and `position` is a `(filename, line, offset)` |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
382
diff
changeset
|
44 tuple that contains the location of the original element or text in the |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
382
diff
changeset
|
45 input. If the original location is unknown, `position` is `(None, -1, -1)`. |
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
382
diff
changeset
|
46 |
1 | 47 Also provided are ways to serialize the stream to text. The `serialize()` |
48 method will return an iterator over generated strings, while `render()` | |
49 returns the complete generated text at once. Both accept various parameters | |
50 that impact the way the stream is serialized. | |
51 """ | |
52 __slots__ = ['events'] | |
53 | |
17
ad63ad459524
Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents:
10
diff
changeset
|
54 START = StreamEventKind('START') # a start tag |
ad63ad459524
Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents:
10
diff
changeset
|
55 END = StreamEventKind('END') # an end tag |
ad63ad459524
Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents:
10
diff
changeset
|
56 TEXT = StreamEventKind('TEXT') # literal text |
ad63ad459524
Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents:
10
diff
changeset
|
57 DOCTYPE = StreamEventKind('DOCTYPE') # doctype declaration |
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
|
58 START_NS = StreamEventKind('START_NS') # start namespace mapping |
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
|
59 END_NS = StreamEventKind('END_NS') # end namespace mapping |
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
|
60 START_CDATA = StreamEventKind('START_CDATA') # start CDATA section |
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
|
61 END_CDATA = StreamEventKind('END_CDATA') # end CDATA section |
17
ad63ad459524
Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents:
10
diff
changeset
|
62 PI = StreamEventKind('PI') # processing instruction |
ad63ad459524
Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents:
10
diff
changeset
|
63 COMMENT = StreamEventKind('COMMENT') # comment |
1 | 64 |
65 def __init__(self, events): | |
66 """Initialize the stream with a sequence of markup events. | |
67 | |
27 | 68 @param events: a sequence or iterable providing the events |
1 | 69 """ |
70 self.events = events | |
71 | |
72 def __iter__(self): | |
73 return iter(self.events) | |
74 | |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
75 def __or__(self, function): |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
76 """Override the "bitwise or" operator to apply filters or serializers |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
77 to the stream, providing a syntax similar to pipes on Unix shells. |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
78 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
79 Assume the following stream produced by the `HTML` function: |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
80 |
230 | 81 >>> from genshi.input import HTML |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
82 >>> html = HTML('''<p onclick="alert('Whoa')">Hello, world!</p>''') |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
83 >>> print html |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
84 <p onclick="alert('Whoa')">Hello, world!</p> |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
85 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
86 A filter such as the HTML sanitizer can be applied to that stream using |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
87 the pipe notation as follows: |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
88 |
230 | 89 >>> from genshi.filters import HTMLSanitizer |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
90 >>> sanitizer = HTMLSanitizer() |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
91 >>> print html | sanitizer |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
92 <p>Hello, world!</p> |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
93 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
94 Filters can be any function that accepts and produces a stream (where |
397
d6e9170c5ccc
* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents:
382
diff
changeset
|
95 a stream is anything that iterates over events): |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
96 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
97 >>> def uppercase(stream): |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
98 ... for kind, data, pos in stream: |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
99 ... if kind is TEXT: |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
100 ... data = data.upper() |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
101 ... yield kind, data, pos |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
102 >>> print html | sanitizer | uppercase |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
103 <p>HELLO, WORLD!</p> |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
104 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
105 Serializers can also be used with this notation: |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
106 |
230 | 107 >>> from genshi.output import TextSerializer |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
108 >>> output = TextSerializer() |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
109 >>> print html | sanitizer | uppercase | output |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
110 HELLO, WORLD! |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
111 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
112 Commonly, serializers should be used at the end of the "pipeline"; |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
113 using them somewhere in the middle may produce unexpected results. |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
114 """ |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
115 return Stream(_ensure(function(self))) |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
116 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
117 def filter(self, *filters): |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
118 """Apply filters to the stream. |
113
e815c2c07572
Removed the `sanitize()` method from the `Markup` class, and migrate the existing unit tests to `markup.tests.filters`. Provide a `Stream.filter()` method instead which can be used to conveniently apply a filter to a stream.
cmlenz
parents:
111
diff
changeset
|
119 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
120 This method returns a new stream with the given filters applied. The |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
121 filters must be callables that accept the stream object as parameter, |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
122 and return the filtered stream. |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
123 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
124 The call: |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
125 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
126 stream.filter(filter1, filter2) |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
127 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
128 is equivalent to: |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
129 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
130 stream | filter1 | filter2 |
113
e815c2c07572
Removed the `sanitize()` method from the `Markup` class, and migrate the existing unit tests to `markup.tests.filters`. Provide a `Stream.filter()` method instead which can be used to conveniently apply a filter to a stream.
cmlenz
parents:
111
diff
changeset
|
131 """ |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
132 return reduce(operator.or_, (self,) + filters) |
113
e815c2c07572
Removed the `sanitize()` method from the `Markup` class, and migrate the existing unit tests to `markup.tests.filters`. Provide a `Stream.filter()` method instead which can be used to conveniently apply a filter to a stream.
cmlenz
parents:
111
diff
changeset
|
133 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
134 def render(self, method='xml', encoding='utf-8', **kwargs): |
1 | 135 """Return a string representation of the stream. |
136 | |
137 @param method: determines how the stream is serialized; can be either | |
200
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
182
diff
changeset
|
138 "xml", "xhtml", "html", "text", or a custom serializer |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
182
diff
changeset
|
139 class |
1 | 140 @param encoding: how the output string should be encoded; if set to |
141 `None`, this method returns a `unicode` object | |
142 | |
143 Any additional keyword arguments are passed to the serializer, and thus | |
144 depend on the `method` parameter value. | |
145 """ | |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
146 generator = self.serialize(method=method, **kwargs) |
17
ad63ad459524
Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents:
10
diff
changeset
|
147 output = u''.join(list(generator)) |
1 | 148 if encoding is not None: |
200
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
182
diff
changeset
|
149 errors = 'replace' |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
182
diff
changeset
|
150 if method != 'text': |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
182
diff
changeset
|
151 errors = 'xmlcharrefreplace' |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
182
diff
changeset
|
152 return output.encode(encoding, errors) |
8
ea47069a901c
`Stream.render()` was masking `TypeError`s (fix based on suggestion by Matt Good).
cmlenz
parents:
6
diff
changeset
|
153 return output |
1 | 154 |
278
8de2620504b9
Fix the handling of namespace context for match templates.
cmlenz
parents:
230
diff
changeset
|
155 def select(self, path, namespaces=None, variables=None): |
1 | 156 """Return a new stream that contains the events matching the given |
157 XPath expression. | |
158 | |
159 @param path: a string containing the XPath expression | |
160 """ | |
230 | 161 from genshi.path import Path |
278
8de2620504b9
Fix the handling of namespace context for match templates.
cmlenz
parents:
230
diff
changeset
|
162 return Path(path).select(self, namespaces, variables) |
1 | 163 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
164 def serialize(self, method='xml', **kwargs): |
1 | 165 """Generate strings corresponding to a specific serialization of the |
166 stream. | |
167 | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
168 Unlike the `render()` method, this method is a generator that returns |
1 | 169 the serialized output incrementally, as opposed to returning a single |
170 string. | |
171 | |
172 @param method: determines how the stream is serialized; can be either | |
200
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
182
diff
changeset
|
173 "xml", "xhtml", "html", "text", or a custom serializer |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
182
diff
changeset
|
174 class |
147
f7fb556f2678
Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents:
145
diff
changeset
|
175 |
f7fb556f2678
Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents:
145
diff
changeset
|
176 Any additional keyword arguments are passed to the serializer, and thus |
f7fb556f2678
Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents:
145
diff
changeset
|
177 depend on the `method` parameter value. |
1 | 178 """ |
230 | 179 from genshi import output |
1 | 180 cls = method |
181 if isinstance(method, basestring): | |
96
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
91
diff
changeset
|
182 cls = {'xml': output.XMLSerializer, |
35d681a94763
Add an XHTML serialization method. Now really need to get rid of some code duplication in the `markup.output` module.
cmlenz
parents:
91
diff
changeset
|
183 'xhtml': output.XHTMLSerializer, |
200
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
182
diff
changeset
|
184 'html': output.HTMLSerializer, |
50eab0469148
Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents:
182
diff
changeset
|
185 'text': output.TextSerializer}[method] |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
186 return cls(**kwargs)(_ensure(self)) |
1 | 187 |
188 def __str__(self): | |
189 return self.render() | |
190 | |
191 def __unicode__(self): | |
192 return self.render(encoding=None) | |
193 | |
194 | |
69 | 195 START = Stream.START |
196 END = Stream.END | |
197 TEXT = Stream.TEXT | |
198 DOCTYPE = Stream.DOCTYPE | |
199 START_NS = Stream.START_NS | |
200 END_NS = Stream.END_NS | |
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
|
201 START_CDATA = Stream.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
|
202 END_CDATA = Stream.END_CDATA |
69 | 203 PI = Stream.PI |
204 COMMENT = Stream.COMMENT | |
205 | |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
100
diff
changeset
|
206 def _ensure(stream): |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
100
diff
changeset
|
207 """Ensure that every item on the stream is actually a markup event.""" |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
100
diff
changeset
|
208 for event in stream: |
145
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
143
diff
changeset
|
209 if type(event) is not tuple: |
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
143
diff
changeset
|
210 if hasattr(event, 'totuple'): |
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
143
diff
changeset
|
211 event = event.totuple() |
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
143
diff
changeset
|
212 else: |
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
143
diff
changeset
|
213 event = TEXT, unicode(event), (None, -1, -1) |
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
143
diff
changeset
|
214 yield event |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
100
diff
changeset
|
215 |
69 | 216 |
345 | 217 class Attrs(tuple): |
218 """Immutable sequence type that stores the attributes of an element. | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
219 |
345 | 220 Ordering of the attributes is preserved, while accessing by name is also |
221 supported. | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
222 |
182
41db0260ebb1
Renamed `Attributes` to `Attrs` to reduce the verbosity.
cmlenz
parents:
172
diff
changeset
|
223 >>> attrs = Attrs([('href', '#'), ('title', 'Foo')]) |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
224 >>> attrs |
326
08ada6b4b767
Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents:
279
diff
changeset
|
225 Attrs([(QName(u'href'), '#'), (QName(u'title'), 'Foo')]) |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
226 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
227 >>> 'href' in attrs |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
228 True |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
229 >>> 'tabindex' in attrs |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
230 False |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
231 >>> attrs.get(u'title') |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
232 'Foo' |
345 | 233 |
234 Instances may not be manipulated directly. Instead, the operators `|` and | |
235 `-` can be used to produce new instances that have specific attributes | |
236 added, replaced or removed. | |
237 | |
238 To remove an attribute, use the `-` operator. The right hand side can be | |
239 either a string or a set/sequence of strings, identifying the name(s) of | |
240 the attribute(s) to remove: | |
241 | |
242 >>> attrs - 'title' | |
243 Attrs([(QName(u'href'), '#')]) | |
244 >>> attrs - ('title', 'href') | |
245 Attrs() | |
246 | |
247 The original instance is not modified, but the operator can of course be | |
248 used with an assignment: | |
249 | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
250 >>> attrs |
345 | 251 Attrs([(QName(u'href'), '#'), (QName(u'title'), 'Foo')]) |
252 >>> attrs -= 'title' | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
253 >>> attrs |
326
08ada6b4b767
Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents:
279
diff
changeset
|
254 Attrs([(QName(u'href'), '#')]) |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
255 |
345 | 256 To add a new attribute, use the `|` operator, where the right hand value |
257 is a sequence of `(name, value)` tuples (which includes `Attrs` instances): | |
170
f1ac0510d392
Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents:
161
diff
changeset
|
258 |
345 | 259 >>> attrs | [(u'title', 'Bar')] |
260 Attrs([(QName(u'href'), '#'), (QName(u'title'), 'Bar')]) | |
171
34e0455ada92
Follow-up to [214]: allow initializing `Attributes` with attribute names that contain dashes or conflict with a reserved word (such as ?class?.)
cmlenz
parents:
170
diff
changeset
|
261 |
345 | 262 If the attributes already contain an attribute with a given name, the value |
263 of that attribute is replaced: | |
171
34e0455ada92
Follow-up to [214]: allow initializing `Attributes` with attribute names that contain dashes or conflict with a reserved word (such as ?class?.)
cmlenz
parents:
170
diff
changeset
|
264 |
345 | 265 >>> attrs | [(u'href', 'http://example.org/')] |
266 Attrs([(QName(u'href'), 'http://example.org/')]) | |
171
34e0455ada92
Follow-up to [214]: allow initializing `Attributes` with attribute names that contain dashes or conflict with a reserved word (such as ?class?.)
cmlenz
parents:
170
diff
changeset
|
267 |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
268 """ |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
269 __slots__ = [] |
1 | 270 |
345 | 271 def __new__(cls, items=()): |
182
41db0260ebb1
Renamed `Attributes` to `Attrs` to reduce the verbosity.
cmlenz
parents:
172
diff
changeset
|
272 """Create the `Attrs` instance. |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
273 |
345 | 274 If the `items` parameter is provided, it is expected to be a sequence |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
275 of `(name, value)` tuples. |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
276 """ |
345 | 277 return tuple.__new__(cls, [(QName(name), val) for name, val in items]) |
1 | 278 |
279 def __contains__(self, name): | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
280 """Return whether the list includes an attribute with the specified |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
281 name. |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
282 """ |
133
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
123
diff
changeset
|
283 for attr, _ in self: |
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
123
diff
changeset
|
284 if attr == name: |
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
123
diff
changeset
|
285 return True |
1 | 286 |
345 | 287 def __getslice__(self, i, j): |
288 return Attrs(tuple.__getslice__(self, i, j)) | |
289 | |
290 def __or__(self, attrs): | |
291 """Return a new instance that contains the attributes in `attrs` in | |
292 addition to any already existing attributes. | |
293 """ | |
294 repl = dict([(an, av) for an, av in attrs if an in self]) | |
295 return Attrs([(sn, repl.get(sn, sv)) for sn, sv in self] + | |
296 [(an, av) for an, av in attrs if an not in self]) | |
297 | |
326
08ada6b4b767
Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents:
279
diff
changeset
|
298 def __repr__(self): |
08ada6b4b767
Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents:
279
diff
changeset
|
299 if not self: |
08ada6b4b767
Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents:
279
diff
changeset
|
300 return 'Attrs()' |
345 | 301 return 'Attrs([%s])' % ', '.join([repr(item) for item in self]) |
302 | |
303 def __sub__(self, names): | |
304 """Return a new instance with all attributes with a name in `names` are | |
305 removed. | |
306 """ | |
307 if isinstance(names, basestring): | |
308 names = (names,) | |
309 return Attrs([(name, val) for name, val in self if name not in names]) | |
326
08ada6b4b767
Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents:
279
diff
changeset
|
310 |
1 | 311 def get(self, name, default=None): |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
312 """Return the value of the attribute with the specified name, or the |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
313 value of the `default` parameter if no such attribute is found. |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
314 """ |
1 | 315 for attr, value in self: |
316 if attr == name: | |
317 return value | |
318 return default | |
319 | |
77
f1aa49c759b2
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
73
diff
changeset
|
320 def totuple(self): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
321 """Return the attributes as a markup event. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
322 |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
323 The returned event is a TEXT event, the data is the value of all |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
324 attributes joined together. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
325 """ |
77
f1aa49c759b2
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
73
diff
changeset
|
326 return TEXT, u''.join([x[1] for x in self]), (None, -1, -1) |
f1aa49c759b2
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
73
diff
changeset
|
327 |
1 | 328 |
329 class Markup(unicode): | |
330 """Marks a string as being safe for inclusion in HTML/XML output without | |
331 needing to be escaped. | |
332 """ | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
333 __slots__ = [] |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
334 |
27 | 335 def __new__(cls, text='', *args): |
1 | 336 if args: |
136 | 337 text %= tuple(map(escape, args)) |
27 | 338 return unicode.__new__(cls, text) |
1 | 339 |
340 def __add__(self, other): | |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
341 return Markup(unicode(self) + unicode(escape(other))) |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
342 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
343 def __radd__(self, other): |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
344 return Markup(unicode(escape(other)) + unicode(self)) |
1 | 345 |
346 def __mod__(self, args): | |
347 if not isinstance(args, (list, tuple)): | |
348 args = [args] | |
136 | 349 return Markup(unicode.__mod__(self, tuple(map(escape, args)))) |
1 | 350 |
351 def __mul__(self, num): | |
352 return Markup(unicode(self) * num) | |
353 | |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
354 def __rmul__(self, num): |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
355 return Markup(num * unicode(self)) |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
356 |
17
ad63ad459524
Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents:
10
diff
changeset
|
357 def __repr__(self): |
382
d7da3fba7faf
* Added documentation for the various stream event kinds.
cmlenz
parents:
377
diff
changeset
|
358 return '<%s %r>' % (self.__class__.__name__, unicode(self)) |
17
ad63ad459524
Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents:
10
diff
changeset
|
359 |
54 | 360 def join(self, seq, escape_quotes=True): |
361 return Markup(unicode(self).join([escape(item, quotes=escape_quotes) | |
34 | 362 for item in seq])) |
1 | 363 |
364 def escape(cls, text, quotes=True): | |
365 """Create a Markup instance from a string and escape special characters | |
366 it may contain (<, >, & and \"). | |
367 | |
368 If the `quotes` parameter is set to `False`, the \" character is left | |
369 as is. Escaping quotes is generally only required for strings that are | |
370 to be used in attribute values. | |
371 """ | |
73 | 372 if not text: |
373 return cls() | |
374 if type(text) is cls: | |
1 | 375 return text |
73 | 376 text = unicode(text).replace('&', '&') \ |
377 .replace('<', '<') \ | |
378 .replace('>', '>') | |
1 | 379 if quotes: |
380 text = text.replace('"', '"') | |
381 return cls(text) | |
382 escape = classmethod(escape) | |
383 | |
384 def unescape(self): | |
385 """Reverse-escapes &, <, > and \" and returns a `unicode` object.""" | |
386 if not self: | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
387 return u'' |
1 | 388 return unicode(self).replace('"', '"') \ |
389 .replace('>', '>') \ | |
390 .replace('<', '<') \ | |
391 .replace('&', '&') | |
392 | |
116 | 393 def stripentities(self, keepxmlentities=False): |
394 """Return a copy of the text with any character or numeric entities | |
395 replaced by the equivalent UTF-8 characters. | |
396 | |
397 If the `keepxmlentities` parameter is provided and evaluates to `True`, | |
398 the core XML entities (&, ', >, < and ") are not | |
399 stripped. | |
6 | 400 """ |
116 | 401 return Markup(stripentities(self, keepxmlentities=keepxmlentities)) |
402 | |
403 def striptags(self): | |
404 """Return a copy of the text with all XML/HTML tags removed.""" | |
405 return Markup(striptags(self)) | |
1 | 406 |
407 | |
408 escape = Markup.escape | |
409 | |
410 def unescape(text): | |
411 """Reverse-escapes &, <, > and \" and returns a `unicode` object.""" | |
412 if not isinstance(text, Markup): | |
413 return text | |
414 return text.unescape() | |
415 | |
416 | |
417 class Namespace(object): | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
418 """Utility class creating and testing elements with a namespace. |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
419 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
420 Internally, namespace URIs are encoded in the `QName` of any element or |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
421 attribute, the namespace URI being enclosed in curly braces. This class |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
422 helps create and test these strings. |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
423 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
424 A `Namespace` object is instantiated with the namespace URI. |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
425 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
426 >>> html = Namespace('http://www.w3.org/1999/xhtml') |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
427 >>> html |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
428 <Namespace "http://www.w3.org/1999/xhtml"> |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
429 >>> html.uri |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
430 u'http://www.w3.org/1999/xhtml' |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
431 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
432 The `Namespace` object can than be used to generate `QName` objects with |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
433 that namespace: |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
434 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
435 >>> html.body |
326
08ada6b4b767
Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents:
279
diff
changeset
|
436 QName(u'http://www.w3.org/1999/xhtml}body') |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
437 >>> html.body.localname |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
438 u'body' |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
439 >>> html.body.namespace |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
440 u'http://www.w3.org/1999/xhtml' |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
441 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
442 The same works using item access notation, which is useful for element or |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
443 attribute names that are not valid Python identifiers: |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
444 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
445 >>> html['body'] |
326
08ada6b4b767
Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents:
279
diff
changeset
|
446 QName(u'http://www.w3.org/1999/xhtml}body') |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
447 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
448 A `Namespace` object can also be used to test whether a specific `QName` |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
449 belongs to that namespace using the `in` operator: |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
450 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
451 >>> qname = html.body |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
452 >>> qname in html |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
453 True |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
454 >>> qname in Namespace('http://www.w3.org/2002/06/xhtml2') |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
455 False |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
456 """ |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
204
diff
changeset
|
457 def __new__(cls, uri): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
204
diff
changeset
|
458 if type(uri) is cls: |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
204
diff
changeset
|
459 return uri |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
204
diff
changeset
|
460 return object.__new__(cls, uri) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
204
diff
changeset
|
461 |
279
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
462 def __getnewargs__(self): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
463 return (self.uri,) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
464 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
465 def __getstate__(self): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
466 return self.uri |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
467 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
468 def __setstate__(self, uri): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
469 self.uri = uri |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
470 |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
471 def __init__(self, uri): |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
472 self.uri = unicode(uri) |
1 | 473 |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
474 def __contains__(self, qname): |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
475 return qname.namespace == self.uri |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
476 |
278
8de2620504b9
Fix the handling of namespace context for match templates.
cmlenz
parents:
230
diff
changeset
|
477 def __ne__(self, other): |
8de2620504b9
Fix the handling of namespace context for match templates.
cmlenz
parents:
230
diff
changeset
|
478 return not self == other |
8de2620504b9
Fix the handling of namespace context for match templates.
cmlenz
parents:
230
diff
changeset
|
479 |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
480 def __eq__(self, other): |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
481 if isinstance(other, Namespace): |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
482 return self.uri == other.uri |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
483 return self.uri == other |
1 | 484 |
485 def __getitem__(self, name): | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
486 return QName(self.uri + u'}' + name) |
1 | 487 __getattr__ = __getitem__ |
488 | |
489 def __repr__(self): | |
490 return '<Namespace "%s">' % self.uri | |
491 | |
492 def __str__(self): | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
493 return self.uri.encode('utf-8') |
1 | 494 |
495 def __unicode__(self): | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
496 return self.uri |
1 | 497 |
498 | |
147
f7fb556f2678
Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents:
145
diff
changeset
|
499 # The namespace used by attributes such as xml:lang and xml:space |
141
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
500 XML_NAMESPACE = Namespace('http://www.w3.org/XML/1998/namespace') |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
501 |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
502 |
1 | 503 class QName(unicode): |
504 """A qualified element or attribute name. | |
505 | |
506 The unicode value of instances of this class contains the qualified name of | |
507 the element or attribute, in the form `{namespace}localname`. The namespace | |
508 URI can be obtained through the additional `namespace` attribute, while the | |
509 local name can be accessed through the `localname` attribute. | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
510 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
511 >>> qname = QName('foo') |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
512 >>> qname |
326
08ada6b4b767
Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents:
279
diff
changeset
|
513 QName(u'foo') |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
514 >>> qname.localname |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
515 u'foo' |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
516 >>> qname.namespace |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
517 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
518 >>> qname = QName('http://www.w3.org/1999/xhtml}body') |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
519 >>> qname |
326
08ada6b4b767
Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents:
279
diff
changeset
|
520 QName(u'http://www.w3.org/1999/xhtml}body') |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
521 >>> qname.localname |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
522 u'body' |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
523 >>> qname.namespace |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
524 u'http://www.w3.org/1999/xhtml' |
1 | 525 """ |
526 __slots__ = ['namespace', 'localname'] | |
527 | |
528 def __new__(cls, qname): | |
100 | 529 if type(qname) is cls: |
1 | 530 return qname |
531 | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
532 parts = qname.split(u'}', 1) |
100 | 533 if len(parts) > 1: |
136 | 534 self = unicode.__new__(cls, u'{%s' % qname) |
535 self.namespace, self.localname = map(unicode, parts) | |
1 | 536 else: |
537 self = unicode.__new__(cls, qname) | |
136 | 538 self.namespace, self.localname = None, unicode(qname) |
1 | 539 return self |
279
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
540 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
541 def __getnewargs__(self): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
542 return (self.lstrip('{'),) |
326
08ada6b4b767
Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents:
279
diff
changeset
|
543 |
08ada6b4b767
Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents:
279
diff
changeset
|
544 def __repr__(self): |
08ada6b4b767
Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents:
279
diff
changeset
|
545 return 'QName(%s)' % unicode.__repr__(self.lstrip('{')) |