annotate markup/core.py @ 116:88ac4c680120

Merged [135:138/branches/experimental/cspeedups].
author cmlenz
date Tue, 01 Aug 2006 17:06:03 +0000
parents d4ea684655d9
children 93bbdcf9428b
rev   line source
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
5 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
9 #
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
13
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
14 """Core classes for markup processing."""
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
15
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
16 import htmlentitydefs
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
17 import re
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
18 from StringIO import StringIO
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
19
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
20 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Namespace', 'QName']
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
21
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
22
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
23 class StreamEventKind(str):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
24 """A kind of event on an XML stream."""
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
25
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
26
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
27 class Stream(object):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
28 """Represents a stream of markup events.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
29
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
30 This class is basically an iterator over the events.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
31
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
32 Also provided are ways to serialize the stream to text. The `serialize()`
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
33 method will return an iterator over generated strings, while `render()`
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
34 returns the complete generated text at once. Both accept various parameters
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
35 that impact the way the stream is serialized.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
36
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
37 Stream events are tuples of the form:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
38
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
39 (kind, data, position)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
40
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
41 where `kind` is the event kind (such as `START`, `END`, `TEXT`, etc), `data`
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
42 depends on the kind of event, and `position` is a `(line, offset)` tuple
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
43 that contains the location of the original element or text in the input.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
44 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
45 __slots__ = ['events']
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
46
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
47 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
48 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
49 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
50 PROLOG = StreamEventKind('PROLOG') # XML prolog
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
51 DOCTYPE = StreamEventKind('DOCTYPE') # doctype declaration
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
52 START_NS = StreamEventKind('START-NS') # start namespace mapping
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
53 END_NS = StreamEventKind('END-NS') # end namespace mapping
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
56
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
57 def __init__(self, events):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
58 """Initialize the stream with a sequence of markup events.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
59
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
60 @param events: a sequence or iterable providing the events
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
61 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
62 self.events = events
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
63
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
64 def __iter__(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
65 return iter(self.events)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
66
114
8f53c3ad385c Use constants for axes in XPath engine.
cmlenz
parents: 113
diff changeset
67 def filter(self, func):
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
68 """Apply a filter to the stream.
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
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
70 This method returns a new stream with the given filter applied. The
114
8f53c3ad385c Use constants for axes in XPath engine.
cmlenz
parents: 113
diff changeset
71 filter must be a callable that accepts the stream object as parameter,
8f53c3ad385c Use constants for axes in XPath engine.
cmlenz
parents: 113
diff changeset
72 and returns 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 """
115
d4ea684655d9 Various fixes for breakage introduced in [132].
cmlenz
parents: 114
diff changeset
74 return Stream(func(self))
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
75
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
76 def render(self, method='xml', encoding='utf-8', filters=None, **kwargs):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
77 """Return a string representation of the stream.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
78
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
79 @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
80 "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
81 subclass
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
82 @param encoding: how the output string should be encoded; if set to
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
83 `None`, this method returns a `unicode` object
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
84
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
85 Any additional keyword arguments are passed to the serializer, and thus
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
86 depend on the `method` parameter value.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
87 """
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
88 generator = self.serialize(method=method, filters=filters, **kwargs)
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
89 output = u''.join(list(generator))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
90 if encoding is not None:
9
3dc28e165273 Actually use the specified encoding in `Stream.render()`.
cmlenz
parents: 8
diff changeset
91 return output.encode(encoding)
8
ea47069a901c `Stream.render()` was masking `TypeError`s (fix based on suggestion by Matt Good).
cmlenz
parents: 6
diff changeset
92 return output
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
93
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
94 def select(self, path):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
95 """Return a new stream that contains the events matching the given
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
96 XPath expression.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
97
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
98 @param path: a string containing the XPath expression
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
99 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
100 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
101 return Path(path).select(self)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
102
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
103 def serialize(self, method='xml', filters=None, **kwargs):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
104 """Generate strings corresponding to a specific serialization of the
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
105 stream.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
106
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
107 Unlike the `render()` method, this method is a generator that returns
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
108 the serialized output incrementally, as opposed to returning a single
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
109 string.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
110
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
111 @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
112 "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
113 subclass
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
114 @param filters: list of filters to apply to the stream before
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
115 serialization. The default is to apply whitespace
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
116 reduction using `markup.filters.WhitespaceFilter`.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
117 """
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
118 from markup.filters import WhitespaceFilter
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
119 from markup import output
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
120 cls = method
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
121 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
122 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
123 '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
124 'html': output.HTMLSerializer}[method]
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
125 else:
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
126 assert issubclass(cls, output.Serializer)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
127 serializer = cls(**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
128
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
129 stream = _ensure(self)
17
ad63ad459524 Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents: 10
diff changeset
130 if filters is None:
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
131 filters = [WhitespaceFilter()]
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
132 for filter_ in filters:
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
133 stream = filter_(iter(stream))
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
134
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
135 return serializer.serialize(stream)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
136
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
137 def __str__(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
138 return self.render()
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
139
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
140 def __unicode__(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
141 return self.render(encoding=None)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
142
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
143
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
144 START = Stream.START
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
145 END = Stream.END
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
146 TEXT = Stream.TEXT
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
147 PROLOG = Stream.PROLOG
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
148 DOCTYPE = Stream.DOCTYPE
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
149 START_NS = Stream.START_NS
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
150 END_NS = Stream.END_NS
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
151 PI = Stream.PI
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
152 COMMENT = Stream.COMMENT
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
153
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
154 def _ensure(stream):
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
155 """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
156 for event in stream:
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
157 try:
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
158 kind, data, pos = event
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
159 except ValueError:
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
160 kind, data, pos = event.totuple()
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
161 yield kind, data, pos
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
162
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
163
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
164 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
165 """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
166
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
167 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
168 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
169
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
170 >>> 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
171 >>> attrs
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
172 [(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
173
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
174 >>> 'href' in attrs
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
175 True
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
176 >>> 'tabindex' in attrs
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
177 False
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
178
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
179 >>> 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
180 'Foo'
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
181 >>> 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
182 >>> attrs
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
183 [(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
184 >>> 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
185 >>> attrs
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
186 [(u'href', '#')]
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
187
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
188 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
189 the list:
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
190
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
191 >>> 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
192 >>> attrs
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
193 [(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
194 """
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
195 __slots__ = []
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
196
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
197 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
198 """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
199
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
200 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
201 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
202 """
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
203 if attrib is None:
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
204 attrib = []
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
205 list.__init__(self, [(QName(name), value) for name, value in attrib])
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
206
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
207 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
208 """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
209 name.
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
210 """
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
211 return name in [attr for attr, _ in self]
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
212
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
213 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
214 """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
215 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
216 """
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
217 for attr, value in self:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
218 if attr == name:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
219 return value
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
220 return default
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
221
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
222 def remove(self, name):
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
223 """Removes the attribute with the specified name.
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
224
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
225 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
226 """
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
227 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
228 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
229 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
230 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
231
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
232 def set(self, name, value):
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
233 """Sets the specified attribute to the given value.
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
234
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
235 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
236 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
237 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
238 """
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
239 for idx, (attr, _) in enumerate(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
240 if attr == name:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
241 self[idx] = (attr, value)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
242 break
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
243 else:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
244 self.append((QName(name), value))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
245
77
f1aa49c759b2 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 73
diff changeset
246 def totuple(self):
f1aa49c759b2 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 73
diff changeset
247 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
248
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
249
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
250 def plaintext(text, keeplinebreaks=True):
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
251 """Returns the text as a `unicode` string with all entities and tags
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
252 removed.
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
253 """
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
254 text = stripentities(striptags(text))
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
255 if not keeplinebreaks:
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
256 text = text.replace(u'\n', u' ')
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
257 return text
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
258
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
259 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
260 """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
261 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
262
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 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
264 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
265 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
266 """
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 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
268 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
269 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
270 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
271 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
272 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
273 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
274 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
275 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
276 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
277 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
278 '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
279 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
280 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
281 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
282 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
283 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
284 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
285 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
286 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
287 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
288 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
289 _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
290
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
291 def striptags(text):
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
292 """Return a copy of the text with all XML/HTML tags removed."""
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
293 return re.sub(r'<[^>]*?>', '', text)
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
294
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
295
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
296 class Markup(unicode):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
297 """Marks a string as being safe for inclusion in HTML/XML output without
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
298 needing to be escaped.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
299 """
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
300 __slots__ = []
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
301
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
302 def __new__(cls, text='', *args):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
303 if args:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
304 text %= tuple([escape(arg) for arg in args])
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
305 return unicode.__new__(cls, text)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
306
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
307 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
308 return Markup(unicode(self) + escape(other))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
309
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
310 def __mod__(self, args):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
311 if not isinstance(args, (list, tuple)):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
312 args = [args]
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
313 return Markup(unicode.__mod__(self,
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
314 tuple([escape(arg) for arg in args])))
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
315
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
316 def __mul__(self, num):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
317 return Markup(unicode(self) * num)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 34
diff changeset
322 def join(self, seq, escape_quotes=True):
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 34
diff changeset
323 return Markup(unicode(self).join([escape(item, quotes=escape_quotes)
34
ae293292cbb1 quotes should not be escaped inside text nodes
mgood
parents: 27
diff changeset
324 for item in seq]))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
325
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
326 def escape(cls, text, quotes=True):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
327 """Create a Markup instance from a string and escape special characters
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
328 it may contain (<, >, & and \").
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
329
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
330 If the `quotes` parameter is set to `False`, the \" character is left
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
331 as is. Escaping quotes is generally only required for strings that are
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
332 to be used in attribute values.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
333 """
73
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
334 if not text:
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
335 return cls()
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
336 if type(text) is cls:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
337 return text
73
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
338 text = unicode(text).replace('&', '&amp;') \
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
339 .replace('<', '&lt;') \
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
340 .replace('>', '&gt;')
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
341 if quotes:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
342 text = text.replace('"', '&#34;')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
343 return cls(text)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
344 escape = classmethod(escape)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
345
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
346 def unescape(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
347 """Reverse-escapes &, <, > and \" and returns a `unicode` object."""
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
350 return unicode(self).replace('&#34;', '"') \
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
351 .replace('&gt;', '>') \
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
352 .replace('&lt;', '<') \
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
353 .replace('&amp;', '&')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
354
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
355 def stripentities(self, keepxmlentities=False):
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
356 """Return a copy of the text with any character or numeric entities
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
357 replaced by the equivalent UTF-8 characters.
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
358
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
359 If the `keepxmlentities` parameter is provided and evaluates to `True`,
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
360 the core XML entities (&amp;, &apos;, &gt;, &lt; and &quot;) are not
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
361 stripped.
6
5da45906dda7 Simplified implementation of `py:content` directive.
cmlenz
parents: 5
diff changeset
362 """
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
363 return Markup(stripentities(self, keepxmlentities=keepxmlentities))
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
364
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
365 def striptags(self):
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
366 """Return a copy of the text with all XML/HTML tags removed."""
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
367 return Markup(striptags(self))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
368
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
369
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
370 escape = Markup.escape
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
371
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
372 def unescape(text):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
373 """Reverse-escapes &, <, > and \" and returns a `unicode` object."""
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
374 if not isinstance(text, Markup):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
375 return text
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
376 return text.unescape()
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
377
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
378
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
429
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
432 __getattr__ = __getitem__
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
433
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
434 def __repr__(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
435 return '<Namespace "%s">' % self.uri
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
436
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
439
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
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
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
442
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
443
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
444 class QName(unicode):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
445 """A qualified element or attribute name.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
446
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
447 The unicode value of instances of this class contains the qualified name of
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
448 the element or attribute, in the form `{namespace}localname`. The namespace
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
449 URI can be obtained through the additional `namespace` attribute, while the
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
450 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
451
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
452 >>> qname = QName('foo')
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
453 >>> qname
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
454 u'foo'
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
455 >>> qname.localname
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
456 u'foo'
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
457 >>> qname.namespace
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
458
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
459 >>> 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
460 >>> qname
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
461 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
462 >>> qname.localname
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
463 u'body'
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
464 >>> qname.namespace
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'
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
466 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
467 __slots__ = ['namespace', 'localname']
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
468
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
469 def __new__(cls, qname):
100
5e9987f34e6c Ported [111] to trunk.
cmlenz
parents: 96
diff changeset
470 if type(qname) is cls:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
471 return qname
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
472
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
473 parts = qname.split(u'}', 1)
100
5e9987f34e6c Ported [111] to trunk.
cmlenz
parents: 96
diff changeset
474 if len(parts) > 1:
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
475 self = unicode.__new__(cls, u'{' + qname)
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
476 self.namespace = unicode(parts[0])
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
477 self.localname = unicode(parts[1])
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
478 else:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
479 self = unicode.__new__(cls, qname)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
480 self.namespace = None
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
481 self.localname = unicode(qname)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
482 return self
Copyright (C) 2012-2017 Edgewall Software