annotate markup/core.py @ 170:6b265e02d099 trunk

Allow initialization of `Attributes` with keyword arguments.
author cmlenz
date Fri, 18 Aug 2006 12:40:55 +0000
parents 7b1f07496bf7
children 7fcf8e04514e
rev   line source
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
2 #
66
59eb24184e9c Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 54
diff changeset
3 # Copyright (C) 2006 Edgewall Software
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
5 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
66
59eb24184e9c 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
9 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
66
59eb24184e9c 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
13
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
14 """Core classes for markup processing."""
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
15
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
16 import htmlentitydefs
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
17 import re
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
18
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
19 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Namespace', 'QName']
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
20
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
21
17
74cc70129d04 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
23 """A kind of event on an XML stream."""
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
24
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
25
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
26 class Stream(object):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
27 """Represents a stream of markup events.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
28
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
29 This class is basically an iterator over the events.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
30
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
31 Also provided are ways to serialize the stream to text. The `serialize()`
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
32 method will return an iterator over generated strings, while `render()`
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
33 returns the complete generated text at once. Both accept various parameters
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
34 that impact the way the stream is serialized.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
35
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
36 Stream events are tuples of the form:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
37
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
38 (kind, data, position)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
39
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
40 where `kind` is the event kind (such as `START`, `END`, `TEXT`, etc), `data`
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
41 depends on the kind of event, and `position` is a `(line, offset)` tuple
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
42 that contains the location of the original element or text in the input.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
43 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
44 __slots__ = ['events']
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
45
17
74cc70129d04 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
74cc70129d04 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
74cc70129d04 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
74cc70129d04 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
3d4c214c979a 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
3d4c214c979a 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
3d4c214c979a 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
3d4c214c979a 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
74cc70129d04 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
74cc70129d04 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
56
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
57 def __init__(self, events):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
58 """Initialize the stream with a sequence of markup events.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
59
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
60 @param events: a sequence or iterable providing the events
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
61 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
62 self.events = events
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
63
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
64 def __iter__(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
65 return iter(self.events)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
66
123
10279d2eeec9 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):
10279d2eeec9 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
d10fbba1d5e0 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
10279d2eeec9 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
10279d2eeec9 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,
10279d2eeec9 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
d10fbba1d5e0 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
10279d2eeec9 Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 116
diff changeset
74 stream = self
10279d2eeec9 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:
10279d2eeec9 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))
10279d2eeec9 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
d10fbba1d5e0 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
10279d2eeec9 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
80 """Return a string representation of the stream.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
81
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
82 @param method: determines how the stream is serialized; can be either
96
fa08aef181a2 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`
fa08aef181a2 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
85 @param encoding: how the output string should be encoded; if set to
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
86 `None`, this method returns a `unicode` object
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
87
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
88 Any additional keyword arguments are passed to the serializer, and thus
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
89 depend on the `method` parameter value.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
90 """
123
10279d2eeec9 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
74cc70129d04 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
93 if encoding is not None:
147
a4a0ca41b6ad 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
3710e3d0d4a2 `Stream.render()` was masking `TypeError`s (fix based on suggestion by Matt Good).
cmlenz
parents: 6
diff changeset
95 return output
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
96
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
97 def select(self, path):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
98 """Return a new stream that contains the events matching the given
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
99 XPath expression.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
100
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
101 @param path: a string containing the XPath expression
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
102 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
103 from markup.path import Path
17
74cc70129d04 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
105
123
10279d2eeec9 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
107 """Generate strings corresponding to a specific serialization of the
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
108 stream.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
109
18
5420cfe42d36 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
111 the serialized output incrementally, as opposed to returning a single
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
112 string.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
113
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
114 @param method: determines how the stream is serialized; can be either
123
10279d2eeec9 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
a4a0ca41b6ad 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
a4a0ca41b6ad 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
a4a0ca41b6ad 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
119 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
120 from markup import output
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
121 cls = method
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
122 if isinstance(method, basestring):
96
fa08aef181a2 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,
fa08aef181a2 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,
fa08aef181a2 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
10279d2eeec9 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)
10279d2eeec9 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
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
128
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
129 def __str__(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
130 return self.render()
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
131
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
132 def __unicode__(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
133 return self.render(encoding=None)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
134
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
135
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
136 START = Stream.START
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
137 END = Stream.END
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
138 TEXT = Stream.TEXT
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
139 DOCTYPE = Stream.DOCTYPE
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
140 START_NS = Stream.START_NS
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
141 END_NS = Stream.END_NS
143
3d4c214c979a 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
3d4c214c979a 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
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
144 PI = Stream.PI
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
145 COMMENT = Stream.COMMENT
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
146
111
2368c3becc52 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
147 def _ensure(stream):
2368c3becc52 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."""
2368c3becc52 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
149 for event in stream:
145
47bbd9d2a5af * 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:
47bbd9d2a5af * 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'):
47bbd9d2a5af * 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()
47bbd9d2a5af * 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:
47bbd9d2a5af * 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)
47bbd9d2a5af * 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
2368c3becc52 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
156
69
c40a5dcd2b55 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
157
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
158 class Attributes(list):
18
5420cfe42d36 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.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
160
5420cfe42d36 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
5420cfe42d36 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.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
163
5420cfe42d36 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')])
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
165 >>> attrs
5420cfe42d36 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')]
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
167
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
168 >>> 'href' in attrs
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
169 True
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
170 >>> 'tabindex' in attrs
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
171 False
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
172
5420cfe42d36 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')
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
174 'Foo'
5420cfe42d36 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')
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
176 >>> attrs
5420cfe42d36 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')]
5420cfe42d36 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')
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
179 >>> attrs
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
180 [(u'href', '#')]
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
181
5420cfe42d36 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
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
183 the list:
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
184
5420cfe42d36 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')
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
186 >>> attrs
5420cfe42d36 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')]
170
6b265e02d099 Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents: 161
diff changeset
188
6b265e02d099 Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents: 161
diff changeset
189 An `Attributes` instance can also be initialized with keyword arguments:
6b265e02d099 Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents: 161
diff changeset
190
6b265e02d099 Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents: 161
diff changeset
191 >>> attrs = Attributes(href='#', title='Foo')
6b265e02d099 Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents: 161
diff changeset
192 >>> attrs.get('href')
6b265e02d099 Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents: 161
diff changeset
193 '#'
6b265e02d099 Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents: 161
diff changeset
194 >>> attrs.get('title')
6b265e02d099 Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents: 161
diff changeset
195 'Foo'
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
196 """
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
197 __slots__ = []
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
198
170
6b265e02d099 Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents: 161
diff changeset
199 def __init__(self, attrib=None, **kwargs):
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
200 """Create the `Attributes` instance.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
201
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
202 If the `attrib` parameter is provided, it is expected to be a sequence
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
203 of `(name, value)` tuples.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
204 """
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
205 if attrib is None:
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
206 attrib = []
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
207 list.__init__(self, [(QName(name), value) for name, value in attrib])
170
6b265e02d099 Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents: 161
diff changeset
208 for name, value in kwargs.items():
6b265e02d099 Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents: 161
diff changeset
209 self.set(name, value)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
210
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
211 def __contains__(self, name):
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
212 """Return whether the list includes an attribute with the specified
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
213 name.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
214 """
133
79f445396cd7 Minor cleanup and performance improvement for the builder module.
cmlenz
parents: 123
diff changeset
215 for attr, _ in self:
79f445396cd7 Minor cleanup and performance improvement for the builder module.
cmlenz
parents: 123
diff changeset
216 if attr == name:
79f445396cd7 Minor cleanup and performance improvement for the builder module.
cmlenz
parents: 123
diff changeset
217 return True
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
218
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
219 def get(self, name, default=None):
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
220 """Return the value of the attribute with the specified name, or the
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
221 value of the `default` parameter if no such attribute is found.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
222 """
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
223 for attr, value in self:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
224 if attr == name:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
225 return value
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
226 return default
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
227
5
dbb08edbc615 Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents: 1
diff changeset
228 def remove(self, name):
161
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 147
diff changeset
229 """Remove the attribute with the specified name.
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
230
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
231 If no such attribute is found, this method does nothing.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
232 """
5
dbb08edbc615 Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents: 1
diff changeset
233 for idx, (attr, _) in enumerate(self):
dbb08edbc615 Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents: 1
diff changeset
234 if attr == name:
dbb08edbc615 Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents: 1
diff changeset
235 del self[idx]
dbb08edbc615 Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents: 1
diff changeset
236 break
dbb08edbc615 Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents: 1
diff changeset
237
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
238 def set(self, name, value):
161
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 147
diff changeset
239 """Set the specified attribute to the given value.
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
240
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
241 If an attribute with the specified name is already in the list, the
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
242 value of the existing entry is updated. Otherwise, a new attribute is
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
243 appended to the end of the list.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
244 """
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
245 for idx, (attr, _) in enumerate(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
246 if attr == name:
170
6b265e02d099 Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents: 161
diff changeset
247 self[idx] = (QName(attr), value)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
248 break
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
249 else:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
250 self.append((QName(name), value))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
251
77
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 73
diff changeset
252 def totuple(self):
161
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 147
diff changeset
253 """Return the attributes as a markup event.
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 147
diff changeset
254
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 147
diff changeset
255 The returned event is a TEXT event, the data is the value of all
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 147
diff changeset
256 attributes joined together.
7b1f07496bf7 Various docstring additions and other cosmetic changes.
cmlenz
parents: 147
diff changeset
257 """
77
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 73
diff changeset
258 return TEXT, u''.join([x[1] for x in self]), (None, -1, -1)
f5ec6d4a61e4 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 73
diff changeset
259
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
260
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
261 def plaintext(text, keeplinebreaks=True):
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
262 """Returns the text as a `unicode` string with all entities and tags
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
263 removed.
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
264 """
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
265 text = stripentities(striptags(text))
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
266 if not keeplinebreaks:
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
267 text = text.replace(u'\n', u' ')
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
268 return text
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
269
113
d10fbba1d5e0 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 def stripentities(text, keepxmlentities=False):
d10fbba1d5e0 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 """Return a copy of the given text with any character or numeric entities
d10fbba1d5e0 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 replaced by the equivalent UTF-8 characters.
d10fbba1d5e0 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
d10fbba1d5e0 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 If the `keepxmlentities` parameter is provided and evaluates to `True`,
d10fbba1d5e0 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 the core XML entities (&amp;, &apos;, &gt;, &lt; and &quot;) are not
d10fbba1d5e0 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 stripped.
d10fbba1d5e0 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 """
d10fbba1d5e0 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 def _replace_entity(match):
d10fbba1d5e0 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 if match.group(1): # numeric entity
d10fbba1d5e0 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 ref = match.group(1)
d10fbba1d5e0 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 if ref.startswith('x'):
d10fbba1d5e0 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 ref = int(ref[1:], 16)
d10fbba1d5e0 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 else:
d10fbba1d5e0 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 ref = int(ref, 10)
d10fbba1d5e0 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 return unichr(ref)
d10fbba1d5e0 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 else: # character entity
d10fbba1d5e0 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 ref = match.group(2)
d10fbba1d5e0 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 if keepxmlentities and ref in ('amp', 'apos', 'gt', 'lt',
d10fbba1d5e0 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 'quot'):
d10fbba1d5e0 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 return '&%s;' % ref
d10fbba1d5e0 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 try:
d10fbba1d5e0 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
292 codepoint = htmlentitydefs.name2codepoint[ref]
d10fbba1d5e0 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
293 return unichr(codepoint)
d10fbba1d5e0 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
294 except KeyError:
d10fbba1d5e0 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
295 if keepxmlentities:
d10fbba1d5e0 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 return '&amp;%s;' % ref
d10fbba1d5e0 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
297 else:
d10fbba1d5e0 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
298 return ref
d10fbba1d5e0 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
299 return re.sub(r'&(?:#((?:\d+)|(?:[xX][0-9a-fA-F]+));?|(\w+);)',
d10fbba1d5e0 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
300 _replace_entity, text)
d10fbba1d5e0 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
301
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
302 def striptags(text):
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
303 """Return a copy of the text with all XML/HTML tags removed."""
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
304 return re.sub(r'<[^>]*?>', '', text)
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
305
113
d10fbba1d5e0 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
306
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
307 class Markup(unicode):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
308 """Marks a string as being safe for inclusion in HTML/XML output without
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
309 needing to be escaped.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
310 """
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
311 __slots__ = []
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
312
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
313 def __new__(cls, text='', *args):
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
314 if args:
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 133
diff changeset
315 text %= tuple(map(escape, args))
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
316 return unicode.__new__(cls, text)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
317
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
318 def __add__(self, other):
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
319 return Markup(unicode(self) + escape(other))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
320
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
321 def __mod__(self, args):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
322 if not isinstance(args, (list, tuple)):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
323 args = [args]
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 133
diff changeset
324 return Markup(unicode.__mod__(self, tuple(map(escape, args))))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
325
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
326 def __mul__(self, num):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
327 return Markup(unicode(self) * num)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
328
17
74cc70129d04 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
329 def __repr__(self):
74cc70129d04 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
330 return '<%s "%s">' % (self.__class__.__name__, self)
74cc70129d04 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
331
54
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 34
diff changeset
332 def join(self, seq, escape_quotes=True):
1f3cd91325d9 Fix a number of escaping problems:
cmlenz
parents: 34
diff changeset
333 return Markup(unicode(self).join([escape(item, quotes=escape_quotes)
34
3421dd98f015 quotes should not be escaped inside text nodes
mgood
parents: 27
diff changeset
334 for item in seq]))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
335
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
336 def escape(cls, text, quotes=True):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
337 """Create a Markup instance from a string and escape special characters
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
338 it may contain (<, >, & and \").
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
339
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
340 If the `quotes` parameter is set to `False`, the \" character is left
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
341 as is. Escaping quotes is generally only required for strings that are
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
342 to be used in attribute values.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
343 """
73
1da51d718391 Some more performance tweaks.
cmlenz
parents: 69
diff changeset
344 if not text:
1da51d718391 Some more performance tweaks.
cmlenz
parents: 69
diff changeset
345 return cls()
1da51d718391 Some more performance tweaks.
cmlenz
parents: 69
diff changeset
346 if type(text) is cls:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
347 return text
73
1da51d718391 Some more performance tweaks.
cmlenz
parents: 69
diff changeset
348 text = unicode(text).replace('&', '&amp;') \
1da51d718391 Some more performance tweaks.
cmlenz
parents: 69
diff changeset
349 .replace('<', '&lt;') \
1da51d718391 Some more performance tweaks.
cmlenz
parents: 69
diff changeset
350 .replace('>', '&gt;')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
351 if quotes:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
352 text = text.replace('"', '&#34;')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
353 return cls(text)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
354 escape = classmethod(escape)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
355
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
356 def unescape(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
357 """Reverse-escapes &, <, > and \" and returns a `unicode` object."""
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
358 if not self:
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
359 return u''
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
360 return unicode(self).replace('&#34;', '"') \
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
361 .replace('&gt;', '>') \
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
362 .replace('&lt;', '<') \
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
363 .replace('&amp;', '&')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
364
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
365 def stripentities(self, keepxmlentities=False):
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
366 """Return a copy of the text with any character or numeric entities
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
367 replaced by the equivalent UTF-8 characters.
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
368
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
369 If the `keepxmlentities` parameter is provided and evaluates to `True`,
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
370 the core XML entities (&amp;, &apos;, &gt;, &lt; and &quot;) are not
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
371 stripped.
6
71e8e645fe81 Simplified implementation of `py:content` directive.
cmlenz
parents: 5
diff changeset
372 """
116
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
373 return Markup(stripentities(self, keepxmlentities=keepxmlentities))
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
374
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
375 def striptags(self):
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
376 """Return a copy of the text with all XML/HTML tags removed."""
c77c113846d6 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
377 return Markup(striptags(self))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
378
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
379
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
380 escape = Markup.escape
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
381
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
382 def unescape(text):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
383 """Reverse-escapes &, <, > and \" and returns a `unicode` object."""
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
384 if not isinstance(text, Markup):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
385 return text
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
386 return text.unescape()
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
387
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
388
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
389 class Namespace(object):
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
390 """Utility class creating and testing elements with a namespace.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
391
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
392 Internally, namespace URIs are encoded in the `QName` of any element or
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
393 attribute, the namespace URI being enclosed in curly braces. This class
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
394 helps create and test these strings.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
395
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
396 A `Namespace` object is instantiated with the namespace URI.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
397
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
398 >>> html = Namespace('http://www.w3.org/1999/xhtml')
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
399 >>> html
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
400 <Namespace "http://www.w3.org/1999/xhtml">
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
401 >>> html.uri
5420cfe42d36 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'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
403
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
404 The `Namespace` object can than be used to generate `QName` objects with
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
405 that namespace:
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
406
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
407 >>> html.body
5420cfe42d36 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'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
409 >>> html.body.localname
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
410 u'body'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
411 >>> html.body.namespace
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
412 u'http://www.w3.org/1999/xhtml'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
413
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
414 The same works using item access notation, which is useful for element or
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
415 attribute names that are not valid Python identifiers:
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
416
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
417 >>> html['body']
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
418 u'{http://www.w3.org/1999/xhtml}body'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
419
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
420 A `Namespace` object can also be used to test whether a specific `QName`
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
421 belongs to that namespace using the `in` operator:
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
422
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
423 >>> qname = html.body
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
424 >>> qname in html
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
425 True
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
426 >>> qname in Namespace('http://www.w3.org/2002/06/xhtml2')
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
427 False
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
428 """
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
429 def __init__(self, uri):
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
430 self.uri = unicode(uri)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
431
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
432 def __contains__(self, qname):
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
433 return qname.namespace == self.uri
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
434
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
435 def __eq__(self, other):
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
436 if isinstance(other, Namespace):
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
437 return self.uri == other.uri
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
438 return self.uri == other
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
439
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
440 def __getitem__(self, name):
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
441 return QName(self.uri + u'}' + name)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
442 __getattr__ = __getitem__
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
443
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
444 def __repr__(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
445 return '<Namespace "%s">' % self.uri
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
446
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
447 def __str__(self):
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
448 return self.uri.encode('utf-8')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
449
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
450 def __unicode__(self):
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
451 return self.uri
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
452
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
453
147
a4a0ca41b6ad 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
454 # The namespace used by attributes such as xml:lang and xml:space
141
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
455 XML_NAMESPACE = Namespace('http://www.w3.org/XML/1998/namespace')
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
456
520a5b7dd6d2 * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
457
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
458 class QName(unicode):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
459 """A qualified element or attribute name.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
460
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
461 The unicode value of instances of this class contains the qualified name of
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
462 the element or attribute, in the form `{namespace}localname`. The namespace
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
463 URI can be obtained through the additional `namespace` attribute, while the
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
464 local name can be accessed through the `localname` attribute.
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
465
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
466 >>> qname = QName('foo')
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
467 >>> qname
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
468 u'foo'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
469 >>> qname.localname
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
470 u'foo'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
471 >>> qname.namespace
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
472
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
473 >>> qname = QName('http://www.w3.org/1999/xhtml}body')
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
474 >>> qname
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
475 u'{http://www.w3.org/1999/xhtml}body'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
476 >>> qname.localname
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
477 u'body'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
478 >>> qname.namespace
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
479 u'http://www.w3.org/1999/xhtml'
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
480 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
481 __slots__ = ['namespace', 'localname']
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
482
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
483 def __new__(cls, qname):
100
a519f581a1b1 Ported [111] to trunk.
cmlenz
parents: 96
diff changeset
484 if type(qname) is cls:
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
485 return qname
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
486
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
487 parts = qname.split(u'}', 1)
100
a519f581a1b1 Ported [111] to trunk.
cmlenz
parents: 96
diff changeset
488 if len(parts) > 1:
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 133
diff changeset
489 self = unicode.__new__(cls, u'{%s' % qname)
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 133
diff changeset
490 self.namespace, self.localname = map(unicode, parts)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
491 else:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
492 self = unicode.__new__(cls, qname)
136
b86f496f6035 Minor performance improvements in serialization.
cmlenz
parents: 133
diff changeset
493 self.namespace, self.localname = None, unicode(qname)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
494 return self
Copyright (C) 2012-2017 Edgewall Software