annotate markup/core.py @ 27:b4f78c05e5c9 trunk

* Fix the boilerplate in the Python source files. * Some more docstrings and cosmetic fixes.
author cmlenz
date Wed, 28 Jun 2006 09:28:09 +0000
parents 5420cfe42d36
children 3421dd98f015
rev   line source
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
2 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2006 Christopher Lenz
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
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
8 # are also available at http://markup.cmlenz.net/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
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
12 # history and logs, available at http://markup.cmlenz.net/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 from StringIO import StringIO
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
19
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
20 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Namespace', 'QName']
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
21
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
22
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
23 class StreamEventKind(str):
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
24 """A kind of event on an XML stream."""
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
25
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
26
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
27 class Stream(object):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
28 """Represents a stream of markup events.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
29
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
30 This class is basically an iterator over the events.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
31
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
32 Also provided are ways to serialize the stream to text. The `serialize()`
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
33 method will return an iterator over generated strings, while `render()`
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
34 returns the complete generated text at once. Both accept various parameters
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
35 that impact the way the stream is serialized.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
36
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
37 Stream events are tuples of the form:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
38
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
39 (kind, data, position)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
40
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
41 where `kind` is the event kind (such as `START`, `END`, `TEXT`, etc), `data`
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
42 depends on the kind of event, and `position` is a `(line, offset)` tuple
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
43 that contains the location of the original element or text in the input.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
44 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
45 __slots__ = ['events']
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
46
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
47 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
48 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
49 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
50 PROLOG = StreamEventKind('PROLOG') # XML prolog
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
51 DOCTYPE = StreamEventKind('DOCTYPE') # doctype declaration
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
52 START_NS = StreamEventKind('START-NS') # start namespace mapping
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
53 END_NS = StreamEventKind('END-NS') # end namespace mapping
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
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
67 def render(self, method='xml', encoding='utf-8', filters=None, **kwargs):
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
68 """Return a string representation of the stream.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
69
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
70 @param method: determines how the stream is serialized; can be either
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
71 'xml' or 'html', or a custom `Serializer` subclass
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
72 @param encoding: how the output string should be encoded; if set to
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
73 `None`, this method returns a `unicode` object
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
74
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
75 Any additional keyword arguments are passed to the serializer, and thus
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
76 depend on the `method` parameter value.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
77 """
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
78 generator = self.serialize(method=method, filters=filters, **kwargs)
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
79 output = u''.join(list(generator))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
80 if encoding is not None:
9
5dc4bfe67c20 Actually use the specified encoding in `Stream.render()`.
cmlenz
parents: 8
diff changeset
81 return output.encode(encoding)
8
3710e3d0d4a2 `Stream.render()` was masking `TypeError`s (fix based on suggestion by Matt Good).
cmlenz
parents: 6
diff changeset
82 return output
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
83
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
84 def select(self, path):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
85 """Return a new stream that contains the events matching the given
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
86 XPath expression.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
87
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
88 @param path: a string containing the XPath expression
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
89 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
90 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
91 return Path(path).select(self)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
92
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
93 def serialize(self, method='xml', filters=None, **kwargs):
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
94 """Generate strings corresponding to a specific serialization of the
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
95 stream.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
96
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
97 Unlike the `render()` method, this method is a generator that returns
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
98 the serialized output incrementally, as opposed to returning a single
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
99 string.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
100
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
101 @param method: determines how the stream is serialized; can be either
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
102 'xml' or 'html', or a custom `Serializer` subclass
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
103 @param filters: list of filters to apply to the stream before
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
104 serialization. The default is to apply whitespace
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
105 reduction using `markup.filters.WhitespaceFilter`.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
106 """
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
107 from markup.filters import WhitespaceFilter
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
108 from markup import output
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
109 cls = method
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
110 if isinstance(method, basestring):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
111 cls = {'xml': output.XMLSerializer,
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
112 'html': output.HTMLSerializer}[method]
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
113 else:
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
114 assert issubclass(cls, output.Serializer)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
115 serializer = cls(**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
116
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
117 stream = 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
118 if filters is None:
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
119 filters = [WhitespaceFilter()]
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
120 for filter_ in filters:
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
121 stream = filter_(iter(stream))
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
122
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
123 return serializer.serialize(stream)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
124
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
125 def __str__(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
126 return self.render()
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
127
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
128 def __unicode__(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
129 return self.render(encoding=None)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
130
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
131
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
132 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
133 """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
134
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
135 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
136 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
137
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
138 >>> 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
139 >>> attrs
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
140 [(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
141
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
142 >>> 'href' in attrs
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
143 True
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
144 >>> 'tabindex' in attrs
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
145 False
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
146
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
147 >>> 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
148 'Foo'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
149 >>> 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
150 >>> attrs
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
151 [(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
152 >>> 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
153 >>> attrs
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
154 [(u'href', '#')]
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
155
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
156 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
157 the list:
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
158
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
159 >>> 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
160 >>> attrs
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
161 [(u'href', '#'), (u'accesskey', 'k')]
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
162 """
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
163 __slots__ = []
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
164
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
165 def __init__(self, attrib=None):
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
166 """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
167
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
168 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
169 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
170 """
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
171 if attrib is None:
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
172 attrib = []
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
173 list.__init__(self, [(QName(name), value) for name, value in attrib])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
174
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
175 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
176 """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
177 name.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
178 """
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
179 return name in [attr for attr, _ in self]
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
180
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
181 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
182 """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
183 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
184 """
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
185 for attr, value in self:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
186 if attr == name:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
187 return value
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
188 return default
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
189
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
190 def remove(self, name):
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
191 """Removes the attribute with the specified name.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
192
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
193 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
194 """
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
195 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
196 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
197 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
198 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
199
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
200 def set(self, name, value):
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
201 """Sets the specified attribute to the given value.
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
202
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
203 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
204 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
205 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
206 """
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
207 for idx, (attr, _) in enumerate(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
208 if attr == name:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
209 self[idx] = (attr, value)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
210 break
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
211 else:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
212 self.append((QName(name), value))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
213
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
214
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
215 class Markup(unicode):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
216 """Marks a string as being safe for inclusion in HTML/XML output without
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
217 needing to be escaped.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
218 """
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
219 __slots__ = []
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
220
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
221 def __new__(cls, text='', *args):
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
222 if args:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
223 text %= tuple([escape(arg) for arg in args])
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
224 return unicode.__new__(cls, text)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
225
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
226 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
227 return Markup(unicode(self) + escape(other))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
228
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
229 def __mod__(self, args):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
230 if not isinstance(args, (list, tuple)):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
231 args = [args]
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
232 return Markup(unicode.__mod__(self,
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
233 tuple([escape(arg) for arg in args])))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
234
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
235 def __mul__(self, num):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
236 return Markup(unicode(self) * num)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
237
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
238 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
239 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
240
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
241 def join(self, seq):
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
242 return Markup(unicode(self).join([escape(item) for item in seq]))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
243
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
244 def stripentities(self, keepxmlentities=False):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
245 """Return a copy of the text with any character or numeric entities
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
246 replaced by the equivalent UTF-8 characters.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
247
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
248 If the `keepxmlentities` parameter is provided and evaluates to `True`,
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
249 the core XML entities (&amp;, &apos;, &gt;, &lt; and &quot;) are not
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
250 stripped.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
251 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
252 def _replace_entity(match):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
253 if match.group(1): # numeric entity
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
254 ref = match.group(1)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
255 if ref.startswith('x'):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
256 ref = int(ref[1:], 16)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
257 else:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
258 ref = int(ref, 10)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
259 return unichr(ref)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
260 else: # character entity
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
261 ref = match.group(2)
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
262 if keepxmlentities and ref in ('amp', 'apos', 'gt', 'lt',
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
263 'quot'):
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
264 return '&%s;' % ref
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
265 try:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
266 codepoint = htmlentitydefs.name2codepoint[ref]
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
267 return unichr(codepoint)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
268 except KeyError:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
269 if keepxmlentities:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
270 return '&amp;%s;' % ref
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
271 else:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
272 return ref
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
273 return Markup(re.sub(r'&(?:#((?:\d+)|(?:[xX][0-9a-fA-F]+));?|(\w+);)',
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
274 _replace_entity, self))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
275
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
276 def striptags(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
277 """Return a copy of the text with all XML/HTML tags removed."""
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
278 return Markup(re.sub(r'<[^>]*?>', '', self))
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
279
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
280 def escape(cls, text, quotes=True):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
281 """Create a Markup instance from a string and escape special characters
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
282 it may contain (<, >, & and \").
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
283
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
284 If the `quotes` parameter is set to `False`, the \" character is left
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
285 as is. Escaping quotes is generally only required for strings that are
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
286 to be used in attribute values.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
287 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
288 if isinstance(text, cls):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
289 return text
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
290 text = unicode(text)
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
291 if not text or isinstance(text, cls):
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
292 return cls()
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
293 text = text.replace('&', '&amp;') \
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
294 .replace('<', '&lt;') \
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
295 .replace('>', '&gt;')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
296 if quotes:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
297 text = text.replace('"', '&#34;')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
298 return cls(text)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
299 escape = classmethod(escape)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
300
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
301 def unescape(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
302 """Reverse-escapes &, <, > and \" and returns a `unicode` object."""
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
303 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
304 return u''
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
305 return unicode(self).replace('&#34;', '"') \
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
306 .replace('&gt;', '>') \
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
307 .replace('&lt;', '<') \
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
308 .replace('&amp;', '&')
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
309
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
310 def plaintext(self, keeplinebreaks=True):
6
71e8e645fe81 Simplified implementation of `py:content` directive.
cmlenz
parents: 5
diff changeset
311 """Returns the text as a `unicode` string with all entities and tags
71e8e645fe81 Simplified implementation of `py:content` directive.
cmlenz
parents: 5
diff changeset
312 removed.
71e8e645fe81 Simplified implementation of `py:content` directive.
cmlenz
parents: 5
diff changeset
313 """
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
314 text = unicode(self.striptags().stripentities())
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
315 if not keeplinebreaks:
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
316 text = text.replace(u'\n', u' ')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
317 return text
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
318
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
319 def sanitize(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
320 from markup.filters import HTMLSanitizer
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
321 from markup.input import HTMLParser
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
322 text = StringIO(self.stripentities(keepxmlentities=True))
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
323 return Stream(HTMLSanitizer()(HTMLParser(text)))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
324
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
325
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
326 escape = Markup.escape
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
327
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
328 def unescape(text):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
329 """Reverse-escapes &, <, > and \" and returns a `unicode` object."""
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
330 if not isinstance(text, Markup):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
331 return text
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
332 return text.unescape()
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
333
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
334
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
335 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
336 """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
337
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
338 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
339 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
340 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
341
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
342 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
343
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
344 >>> 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
345 >>> html
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
346 <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
347 >>> html.uri
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
348 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
349
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
350 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
351 that namespace:
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
352
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
353 >>> html.body
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
354 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
355 >>> html.body.localname
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
356 u'body'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
357 >>> html.body.namespace
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
358 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
359
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
360 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
361 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
362
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
363 >>> html['body']
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
364 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
365
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
366 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
367 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
368
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
369 >>> qname = html.body
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
370 >>> qname in html
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
371 True
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
372 >>> 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
373 False
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
374 """
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
375 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
376 self.uri = unicode(uri)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
377
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
378 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
379 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
380
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
381 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
382 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
383 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
384 return self.uri == other
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
385
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
386 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
387 return QName(self.uri + u'}' + name)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
388
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
389 __getattr__ = __getitem__
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
390
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
391 def __repr__(self):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
392 return '<Namespace "%s">' % self.uri
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
393
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
394 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
395 return self.uri.encode('utf-8')
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
396
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
397 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
398 return self.uri
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
399
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
400
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
401 class QName(unicode):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
402 """A qualified element or attribute name.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
403
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
404 The unicode value of instances of this class contains the qualified name of
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
405 the element or attribute, in the form `{namespace}localname`. The namespace
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
406 URI can be obtained through the additional `namespace` attribute, while the
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
407 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
408
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
409 >>> qname = QName('foo')
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
410 >>> qname
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
411 u'foo'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
412 >>> qname.localname
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
413 u'foo'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
414 >>> qname.namespace
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
415
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
416 >>> 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
417 >>> qname
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 >>> qname.localname
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
420 u'body'
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
421 >>> qname.namespace
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
422 u'http://www.w3.org/1999/xhtml'
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
423 """
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
424 __slots__ = ['namespace', 'localname']
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
425
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
426 def __new__(cls, qname):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
427 if isinstance(qname, QName):
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
428 return qname
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
429
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
430 parts = qname.split(u'}', 1)
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
431 if qname.find(u'}') > 0:
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
432 self = unicode.__new__(cls, u'{' + qname)
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
433 self.namespace = unicode(parts[0])
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
434 self.localname = unicode(parts[1])
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
435 else:
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
436 self = unicode.__new__(cls, qname)
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
437 self.namespace = None
18
5420cfe42d36 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
438 self.localname = unicode(qname)
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
439 return self
Copyright (C) 2012-2017 Edgewall Software