Mercurial > genshi > genshi-test
annotate genshi/core.py @ 907:bb813ef5fe25 experimental-inline
inline branch: merged r1129 from trunk.
author | cmlenz |
---|---|
date | Wed, 28 Apr 2010 21:36:59 +0000 |
parents | 09cc3627654c |
children |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
3 # Copyright (C) 2006-2009 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
230 | 8 # are also available at http://genshi.edgewall.org/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
230 | 12 # history and logs, available at http://genshi.edgewall.org/log/. |
1 | 13 |
14 """Core classes for markup processing.""" | |
15 | |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
16 try: |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
17 reduce # builtin in Python < 3 |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
18 except NameError: |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
19 from functools import reduce |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
20 from itertools import chain |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
21 import operator |
398 | 22 |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
23 from genshi.util import plaintext, stripentities, striptags, stringrepr |
1 | 24 |
395 | 25 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Attrs', 'Namespace', |
26 'QName'] | |
500 | 27 __docformat__ = 'restructuredtext en' |
1 | 28 |
29 | |
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
|
30 class StreamEventKind(str): |
398 | 31 """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
|
32 __slots__ = [] |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
33 _instances = {} |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
34 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
35 def __new__(cls, val): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
36 return cls._instances.setdefault(val, str.__new__(cls, val)) |
1 | 37 |
38 | |
39 class Stream(object): | |
40 """Represents a stream of markup events. | |
41 | |
42 This class is basically an iterator over the events. | |
43 | |
500 | 44 Stream events are tuples of the form:: |
398 | 45 |
46 (kind, data, position) | |
47 | |
500 | 48 where ``kind`` is the event kind (such as `START`, `END`, `TEXT`, etc), |
49 ``data`` depends on the kind of event, and ``position`` is a | |
50 ``(filename, line, offset)`` tuple that contains the location of the | |
51 original element or text in the input. If the original location is unknown, | |
52 ``position`` is ``(None, -1, -1)``. | |
398 | 53 |
1 | 54 Also provided are ways to serialize the stream to text. The `serialize()` |
55 method will return an iterator over generated strings, while `render()` | |
56 returns the complete generated text at once. Both accept various parameters | |
57 that impact the way the stream is serialized. | |
58 """ | |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
59 __slots__ = ['events', 'serializer'] |
1 | 60 |
500 | 61 START = StreamEventKind('START') #: a start tag |
62 END = StreamEventKind('END') #: an end tag | |
63 TEXT = StreamEventKind('TEXT') #: literal text | |
64 XML_DECL = StreamEventKind('XML_DECL') #: XML declaration | |
65 DOCTYPE = StreamEventKind('DOCTYPE') #: doctype declaration | |
66 START_NS = StreamEventKind('START_NS') #: start namespace mapping | |
67 END_NS = StreamEventKind('END_NS') #: end namespace mapping | |
68 START_CDATA = StreamEventKind('START_CDATA') #: start CDATA section | |
69 END_CDATA = StreamEventKind('END_CDATA') #: end CDATA section | |
70 PI = StreamEventKind('PI') #: processing instruction | |
71 COMMENT = StreamEventKind('COMMENT') #: comment | |
1 | 72 |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
73 def __init__(self, events, serializer=None): |
1 | 74 """Initialize the stream with a sequence of markup events. |
75 | |
500 | 76 :param events: a sequence or iterable providing the events |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
77 :param serializer: the default serialization method to use for this |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
78 stream |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
79 |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
80 :note: Changed in 0.5: added the `serializer` argument |
1 | 81 """ |
500 | 82 self.events = events #: The underlying iterable producing the events |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
83 self.serializer = serializer #: The default serializion method |
1 | 84 |
85 def __iter__(self): | |
86 return iter(self.events) | |
87 | |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
88 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
|
89 """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
|
90 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
|
91 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
92 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
|
93 |
230 | 94 >>> 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
|
95 >>> html = HTML('''<p onclick="alert('Whoa')">Hello, world!</p>''') |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
96 >>> print(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
|
97 <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
|
98 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
99 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
|
100 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
|
101 |
230 | 102 >>> 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
|
103 >>> sanitizer = HTMLSanitizer() |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
104 >>> print(html | sanitizer) |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
105 <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
|
106 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
107 Filters can be any function that accepts and produces a stream (where |
398 | 108 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
|
109 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
110 >>> 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
|
111 ... 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
|
112 ... 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
|
113 ... 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
|
114 ... yield kind, data, pos |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
115 >>> print(html | sanitizer | uppercase) |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
116 <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
|
117 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
118 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
|
119 |
230 | 120 >>> 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
|
121 >>> output = TextSerializer() |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
122 >>> print(html | sanitizer | uppercase | output) |
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 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
|
124 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
125 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
|
126 using them somewhere in the middle may produce unexpected results. |
500 | 127 |
128 :param function: the callable object that should be applied as a filter | |
129 :return: the filtered stream | |
130 :rtype: `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
|
131 """ |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
132 return Stream(_ensure(function(self)), serializer=self.serializer) |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
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 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
|
135 """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
|
136 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
137 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
|
138 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
|
139 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
|
140 |
500 | 141 The call:: |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
142 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
143 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
|
144 |
500 | 145 is equivalent to:: |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
146 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
147 stream | filter1 | filter2 |
500 | 148 |
149 :param filters: one or more callable objects that should be applied as | |
150 filters | |
151 :return: the filtered stream | |
152 :rtype: `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
|
153 """ |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
154 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
|
155 |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
156 def render(self, method=None, encoding='utf-8', out=None, **kwargs): |
1 | 157 """Return a string representation of the stream. |
158 | |
159 Any additional keyword arguments are passed to the serializer, and thus | |
160 depend on the `method` parameter value. | |
500 | 161 |
162 :param method: determines how the stream is serialized; can be either | |
163 "xml", "xhtml", "html", "text", or a custom serializer | |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
164 class; if `None`, the default serialization method of |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
165 the stream is used |
500 | 166 :param encoding: how the output string should be encoded; if set to |
167 `None`, this method returns a `unicode` object | |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
168 :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
|
169 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
|
170 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
|
171 not be `None` (that is, the output must be encoded) |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
172 :return: a `str` or `unicode` object (depending on the `encoding` |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
173 parameter), or `None` if the `out` parameter is provided |
500 | 174 :rtype: `basestring` |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
175 |
500 | 176 :see: XMLSerializer, XHTMLSerializer, HTMLSerializer, TextSerializer |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
177 :note: Changed in 0.5: added the `out` parameter |
1 | 178 """ |
500 | 179 from genshi.output import encode |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
180 if method is None: |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
181 method = self.serializer or 'xml' |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
182 generator = self.serialize(method=method, **kwargs) |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
183 return encode(generator, method=method, encoding=encoding, out=out) |
1 | 184 |
278
8de2620504b9
Fix the handling of namespace context for match templates.
cmlenz
parents:
230
diff
changeset
|
185 def select(self, path, namespaces=None, variables=None): |
1 | 186 """Return a new stream that contains the events matching the given |
187 XPath expression. | |
188 | |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
189 >>> from genshi import HTML |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
190 >>> stream = HTML('<doc><elem>foo</elem><elem>bar</elem></doc>') |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
191 >>> print(stream.select('elem')) |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
192 <elem>foo</elem><elem>bar</elem> |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
193 >>> print(stream.select('elem/text()')) |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
194 foobar |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
195 |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
196 Note that the outermost element of the stream becomes the *context |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
197 node* for the XPath test. That means that the expression "doc" would |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
198 not match anything in the example above, because it only tests against |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
199 child elements of the outermost element: |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
200 |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
201 >>> print(stream.select('doc')) |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
202 <BLANKLINE> |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
203 |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
204 You can use the "." expression to match the context node itself |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
205 (although that usually makes little sense): |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
206 |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
207 >>> print(stream.select('.')) |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
208 <doc><elem>foo</elem><elem>bar</elem></doc> |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
209 |
500 | 210 :param path: a string containing the XPath expression |
211 :param namespaces: mapping of namespace prefixes used in the path | |
212 :param variables: mapping of variable names to values | |
213 :return: the selected substream | |
214 :rtype: `Stream` | |
215 :raises PathSyntaxError: if the given path expression is invalid or not | |
216 supported | |
1 | 217 """ |
230 | 218 from genshi.path import Path |
278
8de2620504b9
Fix the handling of namespace context for match templates.
cmlenz
parents:
230
diff
changeset
|
219 return Path(path).select(self, namespaces, variables) |
1 | 220 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
221 def serialize(self, method='xml', **kwargs): |
1 | 222 """Generate strings corresponding to a specific serialization of the |
223 stream. | |
224 | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
225 Unlike the `render()` method, this method is a generator that returns |
1 | 226 the serialized output incrementally, as opposed to returning a single |
227 string. | |
228 | |
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
|
229 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
|
230 depend on the `method` parameter value. |
500 | 231 |
232 :param method: determines how the stream is serialized; can be either | |
233 "xml", "xhtml", "html", "text", or a custom serializer | |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
234 class; if `None`, the default serialization method of |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
235 the stream is used |
500 | 236 :return: an iterator over the serialization results (`Markup` or |
237 `unicode` objects, depending on the serialization method) | |
238 :rtype: ``iterator`` | |
239 :see: XMLSerializer, XHTMLSerializer, HTMLSerializer, TextSerializer | |
1 | 240 """ |
500 | 241 from genshi.output import get_serializer |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
242 if method is None: |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
243 method = self.serializer or 'xml' |
500 | 244 return get_serializer(method, **kwargs)(_ensure(self)) |
1 | 245 |
246 def __str__(self): | |
247 return self.render() | |
248 | |
249 def __unicode__(self): | |
250 return self.render(encoding=None) | |
251 | |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
252 def __html__(self): |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
253 return self |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
254 |
1 | 255 |
69 | 256 START = Stream.START |
257 END = Stream.END | |
258 TEXT = Stream.TEXT | |
500 | 259 XML_DECL = Stream.XML_DECL |
69 | 260 DOCTYPE = Stream.DOCTYPE |
261 START_NS = Stream.START_NS | |
262 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
|
263 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
|
264 END_CDATA = Stream.END_CDATA |
69 | 265 PI = Stream.PI |
266 COMMENT = Stream.COMMENT | |
267 | |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
268 |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
100
diff
changeset
|
269 def _ensure(stream): |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
100
diff
changeset
|
270 """Ensure that every item on the stream is actually a markup event.""" |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
271 stream = iter(stream) |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
272 event = stream.next() |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
273 |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
274 # Check whether the iterable is a real markup event stream by examining the |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
275 # first item it yields; if it's not we'll need to do some conversion |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
276 if type(event) is not tuple or len(event) != 3: |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
277 for event in chain([event], 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
|
278 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
|
279 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
|
280 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
|
281 event = TEXT, unicode(event), (None, -1, -1) |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
282 yield event |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
283 return |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
284 |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
285 # This looks like a markup event stream, so we'll just pass it through |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
286 # unchanged |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
287 yield event |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
288 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
|
289 yield event |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
100
diff
changeset
|
290 |
69 | 291 |
347 | 292 class Attrs(tuple): |
293 """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
|
294 |
500 | 295 Ordering of the attributes is preserved, while access by name is also |
347 | 296 supported. |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
297 |
182
41db0260ebb1
Renamed `Attributes` to `Attrs` to reduce the verbosity.
cmlenz
parents:
172
diff
changeset
|
298 >>> 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
|
299 >>> attrs |
500 | 300 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
|
301 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
302 >>> 'href' in attrs |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
303 True |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
304 >>> 'tabindex' in attrs |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
305 False |
500 | 306 >>> attrs.get('title') |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
307 'Foo' |
347 | 308 |
500 | 309 Instances may not be manipulated directly. Instead, the operators ``|`` and |
310 ``-`` can be used to produce new instances that have specific attributes | |
347 | 311 added, replaced or removed. |
312 | |
500 | 313 To remove an attribute, use the ``-`` operator. The right hand side can be |
347 | 314 either a string or a set/sequence of strings, identifying the name(s) of |
315 the attribute(s) to remove: | |
316 | |
317 >>> attrs - 'title' | |
500 | 318 Attrs([('href', '#')]) |
347 | 319 >>> attrs - ('title', 'href') |
320 Attrs() | |
321 | |
322 The original instance is not modified, but the operator can of course be | |
323 used with an assignment: | |
324 | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
325 >>> attrs |
500 | 326 Attrs([('href', '#'), ('title', 'Foo')]) |
347 | 327 >>> attrs -= 'title' |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
328 >>> attrs |
500 | 329 Attrs([('href', '#')]) |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
330 |
500 | 331 To add a new attribute, use the ``|`` operator, where the right hand value |
332 is a sequence of ``(name, value)`` tuples (which includes `Attrs` | |
333 instances): | |
170
f1ac0510d392
Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents:
161
diff
changeset
|
334 |
500 | 335 >>> attrs | [('title', 'Bar')] |
336 Attrs([('href', '#'), ('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
|
337 |
347 | 338 If the attributes already contain an attribute with a given name, the value |
339 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
|
340 |
500 | 341 >>> attrs | [('href', 'http://example.org/')] |
342 Attrs([('href', 'http://example.org/')]) | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
343 """ |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
344 __slots__ = [] |
1 | 345 |
346 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
|
347 """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
|
348 name. |
500 | 349 |
350 :return: `True` if the list includes the attribute | |
351 :rtype: `bool` | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
352 """ |
133
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
123
diff
changeset
|
353 for attr, _ in self: |
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
123
diff
changeset
|
354 if attr == name: |
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
123
diff
changeset
|
355 return True |
1 | 356 |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
357 def __getitem__(self, i): |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
358 """Return an item or slice of the attributes list. |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
359 |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
360 >>> attrs = Attrs([('href', '#'), ('title', 'Foo')]) |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
361 >>> attrs[1] |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
362 ('title', 'Foo') |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
363 >>> attrs[1:] |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
364 Attrs([('title', 'Foo')]) |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
365 """ |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
366 items = tuple.__getitem__(self, i) |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
367 if type(i) is slice: |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
368 return Attrs(items) |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
369 return items |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
370 |
347 | 371 def __getslice__(self, i, j): |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
372 """Return a slice of the attributes list. |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
373 |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
374 >>> attrs = Attrs([('href', '#'), ('title', 'Foo')]) |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
375 >>> attrs[1:] |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
376 Attrs([('title', 'Foo')]) |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
377 """ |
347 | 378 return Attrs(tuple.__getslice__(self, i, j)) |
379 | |
380 def __or__(self, attrs): | |
381 """Return a new instance that contains the attributes in `attrs` in | |
907 | 382 addition to any already existing attributes. Any attributes in the new |
383 set that have a value of `None` are removed. | |
500 | 384 |
385 :return: a new instance with the merged attributes | |
386 :rtype: `Attrs` | |
347 | 387 """ |
907 | 388 remove = set([an for an, av in attrs if av is None]) |
389 replace = dict([(an, av) for an, av in attrs | |
390 if an in self and av is not None]) | |
391 return Attrs([(sn, replace.get(sn, sv)) for sn, sv in self | |
392 if sn not in remove] + | |
393 [(an, av) for an, av in attrs | |
394 if an not in self and an not in remove]) | |
347 | 395 |
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
|
396 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
|
397 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
|
398 return 'Attrs()' |
347 | 399 return 'Attrs([%s])' % ', '.join([repr(item) for item in self]) |
400 | |
401 def __sub__(self, names): | |
402 """Return a new instance with all attributes with a name in `names` are | |
403 removed. | |
500 | 404 |
405 :param names: the names of the attributes to remove | |
406 :return: a new instance with the attribute removed | |
407 :rtype: `Attrs` | |
347 | 408 """ |
409 if isinstance(names, basestring): | |
410 names = (names,) | |
411 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
|
412 |
1 | 413 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
|
414 """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
|
415 value of the `default` parameter if no such attribute is found. |
500 | 416 |
417 :param name: the name of the attribute | |
418 :param default: the value to return when the attribute does not exist | |
419 :return: the attribute value, or the `default` value if that attribute | |
420 does not exist | |
421 :rtype: `object` | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
422 """ |
1 | 423 for attr, value in self: |
424 if attr == name: | |
425 return value | |
426 return default | |
427 | |
77
f1aa49c759b2
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
73
diff
changeset
|
428 def totuple(self): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
429 """Return the attributes as a markup event. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
430 |
500 | 431 The returned event is a `TEXT` event, the data is the value of all |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
432 attributes joined together. |
500 | 433 |
434 >>> Attrs([('href', '#'), ('title', 'Foo')]).totuple() | |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
435 ('TEXT', '#Foo', (None, -1, -1)) |
500 | 436 |
437 :return: a `TEXT` event | |
438 :rtype: `tuple` | |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
439 """ |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
440 return TEXT, ''.join([x[1] for x in self]), (None, -1, -1) |
77
f1aa49c759b2
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
73
diff
changeset
|
441 |
1 | 442 |
443 class Markup(unicode): | |
444 """Marks a string as being safe for inclusion in HTML/XML output without | |
445 needing to be escaped. | |
446 """ | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
447 __slots__ = [] |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
448 |
1 | 449 def __add__(self, other): |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
450 return Markup(unicode.__add__(self, escape(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
|
451 |
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
452 def __radd__(self, other): |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
453 return Markup(unicode.__add__(escape(other), self)) |
1 | 454 |
455 def __mod__(self, args): | |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
456 if isinstance(args, dict): |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
457 args = dict(zip(args.keys(), map(escape, args.values()))) |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
458 elif isinstance(args, (list, tuple)): |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
459 args = tuple(map(escape, args)) |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
460 else: |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
461 args = escape(args) |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
462 return Markup(unicode.__mod__(self, args)) |
1 | 463 |
464 def __mul__(self, num): | |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
465 return Markup(unicode.__mul__(self, num)) |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
466 __rmul__ = __mul__ |
204
516a6cae0aa8
* Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents:
200
diff
changeset
|
467 |
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
|
468 def __repr__(self): |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
469 return "<%s %s>" % (type(self).__name__, unicode.__repr__(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
|
470 |
54 | 471 def join(self, seq, escape_quotes=True): |
500 | 472 """Return a `Markup` object which is the concatenation of the strings |
473 in the given sequence, where this `Markup` object is the separator | |
474 between the joined elements. | |
475 | |
476 Any element in the sequence that is not a `Markup` instance is | |
477 automatically escaped. | |
478 | |
479 :param seq: the sequence of strings to join | |
480 :param escape_quotes: whether double quote characters in the elements | |
481 should be escaped | |
482 :return: the joined `Markup` object | |
483 :rtype: `Markup` | |
484 :see: `escape` | |
485 """ | |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
486 return Markup(unicode.join(self, [escape(item, quotes=escape_quotes) |
34 | 487 for item in seq])) |
1 | 488 |
830 | 489 @classmethod |
1 | 490 def escape(cls, text, quotes=True): |
491 """Create a Markup instance from a string and escape special characters | |
492 it may contain (<, >, & and \"). | |
493 | |
500 | 494 >>> escape('"1 < 2"') |
495 <Markup u'"1 < 2"'> | |
496 | |
1 | 497 If the `quotes` parameter is set to `False`, the \" character is left |
498 as is. Escaping quotes is generally only required for strings that are | |
499 to be used in attribute values. | |
500 | 500 |
501 >>> escape('"1 < 2"', quotes=False) | |
502 <Markup u'"1 < 2"'> | |
503 | |
504 :param text: the text to escape | |
505 :param quotes: if ``True``, double quote characters are escaped in | |
506 addition to the other special characters | |
507 :return: the escaped `Markup` string | |
508 :rtype: `Markup` | |
1 | 509 """ |
73 | 510 if not text: |
511 return cls() | |
512 if type(text) is cls: | |
1 | 513 return text |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
514 if hasattr(text, '__html__'): |
907 | 515 return cls(text.__html__()) |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
516 |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
517 text = text.replace('&', '&') \ |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
518 .replace('<', '<') \ |
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
519 .replace('>', '>') |
1 | 520 if quotes: |
521 text = text.replace('"', '"') | |
522 return cls(text) | |
523 | |
524 def unescape(self): | |
500 | 525 """Reverse-escapes &, <, >, and \" and returns a `unicode` object. |
526 | |
527 >>> Markup('1 < 2').unescape() | |
528 u'1 < 2' | |
529 | |
530 :return: the unescaped string | |
531 :rtype: `unicode` | |
532 :see: `genshi.core.unescape` | |
533 """ | |
1 | 534 if not self: |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
535 return '' |
1 | 536 return unicode(self).replace('"', '"') \ |
537 .replace('>', '>') \ | |
538 .replace('<', '<') \ | |
539 .replace('&', '&') | |
540 | |
116 | 541 def stripentities(self, keepxmlentities=False): |
542 """Return a copy of the text with any character or numeric entities | |
543 replaced by the equivalent UTF-8 characters. | |
544 | |
545 If the `keepxmlentities` parameter is provided and evaluates to `True`, | |
500 | 546 the core XML entities (``&``, ``'``, ``>``, ``<`` and |
547 ``"``) are not stripped. | |
548 | |
549 :return: a `Markup` instance with entities removed | |
550 :rtype: `Markup` | |
551 :see: `genshi.util.stripentities` | |
6 | 552 """ |
116 | 553 return Markup(stripentities(self, keepxmlentities=keepxmlentities)) |
554 | |
555 def striptags(self): | |
500 | 556 """Return a copy of the text with all XML/HTML tags removed. |
557 | |
558 :return: a `Markup` instance with all tags removed | |
559 :rtype: `Markup` | |
560 :see: `genshi.util.striptags` | |
561 """ | |
116 | 562 return Markup(striptags(self)) |
1 | 563 |
564 | |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
565 try: |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
566 from genshi._speedups import Markup |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
567 except ImportError: |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
568 pass # just use the Python implementation |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
569 |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
570 |
1 | 571 escape = Markup.escape |
572 | |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
573 |
1 | 574 def unescape(text): |
500 | 575 """Reverse-escapes &, <, >, and \" and returns a `unicode` object. |
576 | |
577 >>> unescape(Markup('1 < 2')) | |
578 u'1 < 2' | |
579 | |
580 If the provided `text` object is not a `Markup` instance, it is returned | |
581 unchanged. | |
582 | |
583 >>> unescape('1 < 2') | |
584 '1 < 2' | |
585 | |
586 :param text: the text to unescape | |
587 :return: the unescsaped string | |
588 :rtype: `unicode` | |
589 """ | |
1 | 590 if not isinstance(text, Markup): |
591 return text | |
592 return text.unescape() | |
593 | |
594 | |
595 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
|
596 """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
|
597 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
598 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
|
599 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
|
600 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
|
601 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
602 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
|
603 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
604 >>> 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
|
605 >>> html |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
606 Namespace('http://www.w3.org/1999/xhtml') |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
607 >>> html.uri |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
608 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
|
609 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
610 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
|
611 that namespace: |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
612 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
613 >>> html.body |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
614 QName('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
|
615 >>> html.body.localname |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
616 u'body' |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
617 >>> html.body.namespace |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
618 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
|
619 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
620 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
|
621 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
|
622 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
623 >>> html['body'] |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
624 QName('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
|
625 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
626 A `Namespace` object can also be used to test whether a specific `QName` |
500 | 627 belongs to that namespace using the ``in`` operator: |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
628 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
629 >>> qname = html.body |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
630 >>> qname in html |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
631 True |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
632 >>> 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
|
633 False |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
634 """ |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
204
diff
changeset
|
635 def __new__(cls, uri): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
204
diff
changeset
|
636 if type(uri) is cls: |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
204
diff
changeset
|
637 return uri |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
638 return object.__new__(cls) |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
204
diff
changeset
|
639 |
279
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
640 def __getnewargs__(self): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
641 return (self.uri,) |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
642 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
643 def __getstate__(self): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
644 return self.uri |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
645 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
646 def __setstate__(self, uri): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
647 self.uri = uri |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
648 |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
649 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
|
650 self.uri = unicode(uri) |
1 | 651 |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
652 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
|
653 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
|
654 |
278
8de2620504b9
Fix the handling of namespace context for match templates.
cmlenz
parents:
230
diff
changeset
|
655 def __ne__(self, other): |
8de2620504b9
Fix the handling of namespace context for match templates.
cmlenz
parents:
230
diff
changeset
|
656 return not self == other |
8de2620504b9
Fix the handling of namespace context for match templates.
cmlenz
parents:
230
diff
changeset
|
657 |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
658 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
|
659 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
|
660 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
|
661 return self.uri == other |
1 | 662 |
663 def __getitem__(self, name): | |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
664 return QName(self.uri + '}' + name) |
1 | 665 __getattr__ = __getitem__ |
666 | |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
667 def __hash__(self): |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
668 return hash(self.uri) |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
669 |
1 | 670 def __repr__(self): |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
671 return '%s(%s)' % (type(self).__name__, stringrepr(self.uri)) |
1 | 672 |
673 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
|
674 return self.uri.encode('utf-8') |
1 | 675 |
676 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
|
677 return self.uri |
1 | 678 |
679 | |
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
|
680 # 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
|
681 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
|
682 |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
683 |
1 | 684 class QName(unicode): |
685 """A qualified element or attribute name. | |
686 | |
687 The unicode value of instances of this class contains the qualified name of | |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
688 the element or attribute, in the form ``{namespace-uri}local-name``. The |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
689 namespace URI can be obtained through the additional `namespace` attribute, |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
690 while the 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
|
691 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
692 >>> qname = QName('foo') |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
693 >>> qname |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
694 QName('foo') |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
695 >>> qname.localname |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
696 u'foo' |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
697 >>> qname.namespace |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
698 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
699 >>> 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
|
700 >>> qname |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
701 QName('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
|
702 >>> qname.localname |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
703 u'body' |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
704 >>> qname.namespace |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
705 u'http://www.w3.org/1999/xhtml' |
1 | 706 """ |
707 __slots__ = ['namespace', 'localname'] | |
708 | |
709 def __new__(cls, qname): | |
820
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
710 """Create the `QName` instance. |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
711 |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
712 :param qname: the qualified name as a string of the form |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
713 ``{namespace-uri}local-name``, where the leading curly |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
714 brace is optional |
1837f39efd6f
Sync (old) experimental inline branch with trunk@1027.
cmlenz
parents:
500
diff
changeset
|
715 """ |
100 | 716 if type(qname) is cls: |
1 | 717 return qname |
718 | |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
719 parts = qname.lstrip('{').split('}', 1) |
100 | 720 if len(parts) > 1: |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
721 self = unicode.__new__(cls, '{%s' % qname) |
136 | 722 self.namespace, self.localname = map(unicode, parts) |
1 | 723 else: |
724 self = unicode.__new__(cls, qname) | |
136 | 725 self.namespace, self.localname = None, unicode(qname) |
1 | 726 return self |
279
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
727 |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
728 def __getnewargs__(self): |
4b5b4653d41e
Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents:
278
diff
changeset
|
729 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
|
730 |
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
|
731 def __repr__(self): |
902
09cc3627654c
Sync `experimental/inline` branch with [source:trunk@1126].
cmlenz
parents:
830
diff
changeset
|
732 return '%s(%s)' % (type(self).__name__, stringrepr(self.lstrip('{'))) |