Mercurial > genshi > genshi-test
annotate markup/core.py @ 161:a25f9fc5787d
Various docstring additions and other cosmetic changes.
author | cmlenz |
---|---|
date | Wed, 16 Aug 2006 22:32:31 +0000 |
parents | f7fb556f2678 |
children | f1ac0510d392 |
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 | |
66
822089ae65ce
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
54
diff
changeset
|
8 # are also available at http://markup.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 | |
66
822089ae65ce
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
54
diff
changeset
|
12 # history and logs, available at http://markup.edgewall.org/log/. |
1 | 13 |
14 """Core classes for markup processing.""" | |
15 | |
16 import htmlentitydefs | |
17 import re | |
18 | |
19 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Namespace', 'QName'] | |
20 | |
21 | |
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
|
22 class StreamEventKind(str): |
1 | 23 """A kind of event on an XML stream.""" |
24 | |
25 | |
26 class Stream(object): | |
27 """Represents a stream of markup events. | |
28 | |
29 This class is basically an iterator over the events. | |
30 | |
31 Also provided are ways to serialize the stream to text. The `serialize()` | |
32 method will return an iterator over generated strings, while `render()` | |
33 returns the complete generated text at once. Both accept various parameters | |
34 that impact the way the stream is serialized. | |
35 | |
36 Stream events are tuples of the form: | |
37 | |
38 (kind, data, position) | |
39 | |
40 where `kind` is the event kind (such as `START`, `END`, `TEXT`, etc), `data` | |
41 depends on the kind of event, and `position` is a `(line, offset)` tuple | |
42 that contains the location of the original element or text in the input. | |
43 """ | |
44 __slots__ = ['events'] | |
45 | |
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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 COMMENT = StreamEventKind('COMMENT') # comment |
1 | 56 |
57 def __init__(self, events): | |
58 """Initialize the stream with a sequence of markup events. | |
59 | |
27 | 60 @param events: a sequence or iterable providing the events |
1 | 61 """ |
62 self.events = events | |
63 | |
64 def __iter__(self): | |
65 return iter(self.events) | |
66 | |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
67 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
|
68 """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
|
69 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
70 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
|
71 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
|
72 and return the filtered 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
|
73 """ |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
74 stream = self |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
75 for filter_ in filters: |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
76 stream = filter_(iter(stream)) |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
77 return Stream(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
|
78 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
79 def render(self, method='xml', encoding='utf-8', **kwargs): |
1 | 80 """Return a string representation of the stream. |
81 | |
82 @param method: determines how the stream is serialized; can be either | |
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
|
83 "xml", "xhtml", or "html", or a custom `Serializer` |
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
|
84 subclass |
1 | 85 @param encoding: how the output string should be encoded; if set to |
86 `None`, this method returns a `unicode` object | |
87 | |
88 Any additional keyword arguments are passed to the serializer, and thus | |
89 depend on the `method` parameter value. | |
90 """ | |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
91 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
|
92 output = u''.join(list(generator)) |
1 | 93 if encoding is not None: |
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
|
94 return output.encode(encoding, 'xmlcharrefreplace') |
8
ea47069a901c
`Stream.render()` was masking `TypeError`s (fix based on suggestion by Matt Good).
cmlenz
parents:
6
diff
changeset
|
95 return output |
1 | 96 |
97 def select(self, path): | |
98 """Return a new stream that contains the events matching the given | |
99 XPath expression. | |
100 | |
101 @param path: a string containing the XPath expression | |
102 """ | |
103 from markup.path import Path | |
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
|
104 return Path(path).select(self) |
1 | 105 |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
106 def serialize(self, method='xml', **kwargs): |
1 | 107 """Generate strings corresponding to a specific serialization of the |
108 stream. | |
109 | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
110 Unlike the `render()` method, this method is a generator that returns |
1 | 111 the serialized output incrementally, as opposed to returning a single |
112 string. | |
113 | |
114 @param method: determines how the stream is serialized; can be either | |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
115 "xml", "xhtml", or "html", or a custom serializer 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
|
116 |
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
|
117 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
|
118 depend on the `method` parameter value. |
1 | 119 """ |
120 from markup import output | |
121 cls = method | |
122 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
|
123 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
|
124 'xhtml': output.XHTMLSerializer, |
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
|
125 'html': output.HTMLSerializer}[method] |
123
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
126 serialize = cls(**kwargs) |
93bbdcf9428b
Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents:
116
diff
changeset
|
127 return serialize(_ensure(self)) |
1 | 128 |
129 def __str__(self): | |
130 return self.render() | |
131 | |
132 def __unicode__(self): | |
133 return self.render(encoding=None) | |
134 | |
135 | |
69 | 136 START = Stream.START |
137 END = Stream.END | |
138 TEXT = Stream.TEXT | |
139 DOCTYPE = Stream.DOCTYPE | |
140 START_NS = Stream.START_NS | |
141 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
|
142 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
|
143 END_CDATA = Stream.END_CDATA |
69 | 144 PI = Stream.PI |
145 COMMENT = Stream.COMMENT | |
146 | |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
100
diff
changeset
|
147 def _ensure(stream): |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
100
diff
changeset
|
148 """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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 yield event |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
100
diff
changeset
|
156 |
69 | 157 |
1 | 158 class Attributes(list): |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
159 """Sequence type that stores the attributes of an element. |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
160 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
161 The order of the attributes is preserved, while accessing and manipulating |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
162 attributes by name is also supported. |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
163 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
164 >>> attrs = Attributes([('href', '#'), ('title', 'Foo')]) |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
165 >>> attrs |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
166 [(u'href', '#'), (u'title', 'Foo')] |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
167 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
168 >>> 'href' in attrs |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
169 True |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
170 >>> 'tabindex' in attrs |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
171 False |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
172 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
173 >>> 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
|
174 'Foo' |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
175 >>> attrs.set(u'title', 'Bar') |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
176 >>> attrs |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
177 [(u'href', '#'), (u'title', 'Bar')] |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
178 >>> attrs.remove(u'title') |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
179 >>> attrs |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
180 [(u'href', '#')] |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
181 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
182 New attributes added using the `set()` method are appended to the end of |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
183 the list: |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
184 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
185 >>> attrs.set(u'accesskey', 'k') |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
186 >>> attrs |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
187 [(u'href', '#'), (u'accesskey', 'k')] |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
188 """ |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
189 __slots__ = [] |
1 | 190 |
191 def __init__(self, attrib=None): | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
192 """Create the `Attributes` instance. |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
193 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
194 If the `attrib` parameter is provided, it is expected to be a sequence |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
195 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
|
196 """ |
27 | 197 if attrib is None: |
198 attrib = [] | |
199 list.__init__(self, [(QName(name), value) for name, value in attrib]) | |
1 | 200 |
201 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
|
202 """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
|
203 name. |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
204 """ |
133
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
123
diff
changeset
|
205 for attr, _ in self: |
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
123
diff
changeset
|
206 if attr == name: |
b9a0031d4bbb
Minor cleanup and performance improvement for the builder module.
cmlenz
parents:
123
diff
changeset
|
207 return True |
1 | 208 |
209 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
|
210 """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
|
211 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
|
212 """ |
1 | 213 for attr, value in self: |
214 if attr == name: | |
215 return value | |
216 return default | |
217 | |
5
1add946decb8
Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents:
1
diff
changeset
|
218 def remove(self, name): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
219 """Remove the attribute with the specified name. |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
220 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
221 If no such attribute is found, this method does nothing. |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
222 """ |
5
1add946decb8
Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents:
1
diff
changeset
|
223 for idx, (attr, _) in enumerate(self): |
1add946decb8
Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents:
1
diff
changeset
|
224 if attr == name: |
1add946decb8
Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents:
1
diff
changeset
|
225 del self[idx] |
1add946decb8
Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents:
1
diff
changeset
|
226 break |
1add946decb8
Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents:
1
diff
changeset
|
227 |
1 | 228 def set(self, name, value): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
229 """Set the specified attribute to the given value. |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
230 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
231 If an attribute with the specified name is already in the list, the |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
232 value of the existing entry is updated. Otherwise, a new attribute is |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
233 appended to the end of the list. |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
234 """ |
1 | 235 for idx, (attr, _) in enumerate(self): |
236 if attr == name: | |
237 self[idx] = (attr, value) | |
238 break | |
239 else: | |
240 self.append((QName(name), value)) | |
241 | |
77
f1aa49c759b2
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
73
diff
changeset
|
242 def totuple(self): |
161
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
243 """Return the attributes as a markup event. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
244 |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
245 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
|
246 attributes joined together. |
a25f9fc5787d
Various docstring additions and other cosmetic changes.
cmlenz
parents:
147
diff
changeset
|
247 """ |
77
f1aa49c759b2
* Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents:
73
diff
changeset
|
248 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
|
249 |
1 | 250 |
116 | 251 def plaintext(text, keeplinebreaks=True): |
252 """Returns the text as a `unicode` string with all entities and tags | |
253 removed. | |
254 """ | |
255 text = stripentities(striptags(text)) | |
256 if not keeplinebreaks: | |
257 text = text.replace(u'\n', u' ') | |
258 return text | |
259 | |
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
|
260 def stripentities(text, keepxmlentities=False): |
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
|
261 """Return a copy of the given text with any character or numeric entities |
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
|
262 replaced by the equivalent UTF-8 characters. |
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
|
263 |
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
|
264 If the `keepxmlentities` parameter is provided and evaluates to `True`, |
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
|
265 the core XML entities (&, ', >, < and ") are not |
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
|
266 stripped. |
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
|
267 """ |
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
|
268 def _replace_entity(match): |
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
|
269 if match.group(1): # numeric entity |
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
|
270 ref = match.group(1) |
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
|
271 if ref.startswith('x'): |
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
|
272 ref = int(ref[1:], 16) |
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
|
273 else: |
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
|
274 ref = int(ref, 10) |
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
|
275 return unichr(ref) |
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
|
276 else: # character entity |
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
|
277 ref = match.group(2) |
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
|
278 if keepxmlentities and ref in ('amp', 'apos', 'gt', 'lt', |
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
|
279 'quot'): |
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
|
280 return '&%s;' % ref |
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
|
281 try: |
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
|
282 codepoint = htmlentitydefs.name2codepoint[ref] |
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
|
283 return unichr(codepoint) |
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
|
284 except KeyError: |
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
|
285 if keepxmlentities: |
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
|
286 return '&%s;' % ref |
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
|
287 else: |
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
|
288 return ref |
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
|
289 return re.sub(r'&(?:#((?:\d+)|(?:[xX][0-9a-fA-F]+));?|(\w+);)', |
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
|
290 _replace_entity, text) |
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
|
291 |
116 | 292 def striptags(text): |
293 """Return a copy of the text with all XML/HTML tags removed.""" | |
294 return re.sub(r'<[^>]*?>', '', text) | |
295 | |
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
|
296 |
1 | 297 class Markup(unicode): |
298 """Marks a string as being safe for inclusion in HTML/XML output without | |
299 needing to be escaped. | |
300 """ | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
301 __slots__ = [] |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
302 |
27 | 303 def __new__(cls, text='', *args): |
1 | 304 if args: |
136 | 305 text %= tuple(map(escape, args)) |
27 | 306 return unicode.__new__(cls, text) |
1 | 307 |
308 def __add__(self, other): | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
309 return Markup(unicode(self) + escape(other)) |
1 | 310 |
311 def __mod__(self, args): | |
312 if not isinstance(args, (list, tuple)): | |
313 args = [args] | |
136 | 314 return Markup(unicode.__mod__(self, tuple(map(escape, args)))) |
1 | 315 |
316 def __mul__(self, num): | |
317 return Markup(unicode(self) * num) | |
318 | |
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
|
319 def __repr__(self): |
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
|
320 return '<%s "%s">' % (self.__class__.__name__, self) |
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
|
321 |
54 | 322 def join(self, seq, escape_quotes=True): |
323 return Markup(unicode(self).join([escape(item, quotes=escape_quotes) | |
34 | 324 for item in seq])) |
1 | 325 |
326 def escape(cls, text, quotes=True): | |
327 """Create a Markup instance from a string and escape special characters | |
328 it may contain (<, >, & and \"). | |
329 | |
330 If the `quotes` parameter is set to `False`, the \" character is left | |
331 as is. Escaping quotes is generally only required for strings that are | |
332 to be used in attribute values. | |
333 """ | |
73 | 334 if not text: |
335 return cls() | |
336 if type(text) is cls: | |
1 | 337 return text |
73 | 338 text = unicode(text).replace('&', '&') \ |
339 .replace('<', '<') \ | |
340 .replace('>', '>') | |
1 | 341 if quotes: |
342 text = text.replace('"', '"') | |
343 return cls(text) | |
344 escape = classmethod(escape) | |
345 | |
346 def unescape(self): | |
347 """Reverse-escapes &, <, > and \" and returns a `unicode` object.""" | |
348 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
|
349 return u'' |
1 | 350 return unicode(self).replace('"', '"') \ |
351 .replace('>', '>') \ | |
352 .replace('<', '<') \ | |
353 .replace('&', '&') | |
354 | |
116 | 355 def stripentities(self, keepxmlentities=False): |
356 """Return a copy of the text with any character or numeric entities | |
357 replaced by the equivalent UTF-8 characters. | |
358 | |
359 If the `keepxmlentities` parameter is provided and evaluates to `True`, | |
360 the core XML entities (&, ', >, < and ") are not | |
361 stripped. | |
6 | 362 """ |
116 | 363 return Markup(stripentities(self, keepxmlentities=keepxmlentities)) |
364 | |
365 def striptags(self): | |
366 """Return a copy of the text with all XML/HTML tags removed.""" | |
367 return Markup(striptags(self)) | |
1 | 368 |
369 | |
370 escape = Markup.escape | |
371 | |
372 def unescape(text): | |
373 """Reverse-escapes &, <, > and \" and returns a `unicode` object.""" | |
374 if not isinstance(text, Markup): | |
375 return text | |
376 return text.unescape() | |
377 | |
378 | |
379 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
|
380 """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
|
381 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
382 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
|
383 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
|
384 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
|
385 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
386 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
|
387 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
388 >>> 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
|
389 >>> html |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
390 <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
|
391 >>> html.uri |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
392 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
|
393 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
394 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
|
395 that namespace: |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
396 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
397 >>> html.body |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
398 u'{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
|
399 >>> html.body.localname |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
400 u'body' |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
401 >>> html.body.namespace |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
402 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
|
403 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
404 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
|
405 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
|
406 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
407 >>> html['body'] |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
408 u'{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
|
409 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
410 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
|
411 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
|
412 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
413 >>> qname = html.body |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
414 >>> qname in html |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
415 True |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
416 >>> 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
|
417 False |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
418 """ |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
419 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
|
420 self.uri = unicode(uri) |
1 | 421 |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
422 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
|
423 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
|
424 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
425 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
|
426 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
|
427 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
|
428 return self.uri == other |
1 | 429 |
430 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
|
431 return QName(self.uri + u'}' + name) |
1 | 432 __getattr__ = __getitem__ |
433 | |
434 def __repr__(self): | |
435 return '<Namespace "%s">' % self.uri | |
436 | |
437 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
|
438 return self.uri.encode('utf-8') |
1 | 439 |
440 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
|
441 return self.uri |
1 | 442 |
443 | |
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
|
444 # 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
|
445 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
|
446 |
b3ceaa35fb6b
* No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents:
140
diff
changeset
|
447 |
1 | 448 class QName(unicode): |
449 """A qualified element or attribute name. | |
450 | |
451 The unicode value of instances of this class contains the qualified name of | |
452 the element or attribute, in the form `{namespace}localname`. The namespace | |
453 URI can be obtained through the additional `namespace` attribute, while the | |
454 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
|
455 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
456 >>> qname = QName('foo') |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
457 >>> qname |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
458 u'foo' |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
459 >>> qname.localname |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
460 u'foo' |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
461 >>> qname.namespace |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
462 |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
463 >>> 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
|
464 >>> qname |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
465 u'{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
|
466 >>> qname.localname |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
467 u'body' |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
468 >>> qname.namespace |
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
469 u'http://www.w3.org/1999/xhtml' |
1 | 470 """ |
471 __slots__ = ['namespace', 'localname'] | |
472 | |
473 def __new__(cls, qname): | |
100 | 474 if type(qname) is cls: |
1 | 475 return qname |
476 | |
18
4cbebb15a834
Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents:
17
diff
changeset
|
477 parts = qname.split(u'}', 1) |
100 | 478 if len(parts) > 1: |
136 | 479 self = unicode.__new__(cls, u'{%s' % qname) |
480 self.namespace, self.localname = map(unicode, parts) | |
1 | 481 else: |
482 self = unicode.__new__(cls, qname) | |
136 | 483 self.namespace, self.localname = None, unicode(qname) |
1 | 484 return self |