annotate genshi/core.py @ 397:d6e9170c5ccc

* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports) * Minor performance tweaks for the serializers
author cmlenz
date Tue, 02 Jan 2007 17:48:06 +0000
parents d7da3fba7faf
children 32b283e1d310
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
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 224
diff changeset
8 # are also available at http://genshi.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
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 224
diff changeset
12 # history and logs, available at http://genshi.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
204
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
16 import operator
397
d6e9170c5ccc * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 382
diff changeset
17
d6e9170c5ccc * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 382
diff changeset
18 from genshi.util import plaintext, stripentities, striptags
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
19
377
79a7db5177e9 Add `Attrs` class to `genshi.core.__all__`, so that it can be imported directly from the `genshi` package.
cmlenz
parents: 345
diff changeset
20 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Attrs', 'Namespace',
79a7db5177e9 Add `Attrs` class to `genshi.core.__all__`, so that it can be imported directly from the `genshi` package.
cmlenz
parents: 345
diff changeset
21 'QName']
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
22
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
23
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
24 class StreamEventKind(str):
397
d6e9170c5ccc * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 382
diff changeset
25 """A kind of event on a markup stream."""
279
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
26 __slots__ = []
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
27 _instances = {}
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
28
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
29 def __new__(cls, val):
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
30 return cls._instances.setdefault(val, str.__new__(cls, val))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
31
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
32
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
33 class Stream(object):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
34 """Represents a stream of markup events.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
35
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
36 This class is basically an iterator over the events.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
37
397
d6e9170c5ccc * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 382
diff changeset
38 Stream events are tuples of the form:
d6e9170c5ccc * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 382
diff changeset
39
d6e9170c5ccc * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 382
diff changeset
40 (kind, data, position)
d6e9170c5ccc * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 382
diff changeset
41
d6e9170c5ccc * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 382
diff changeset
42 where `kind` is the event kind (such as `START`, `END`, `TEXT`, etc), `data`
d6e9170c5ccc * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 382
diff changeset
43 depends on the kind of event, and `position` is a `(filename, line, offset)`
d6e9170c5ccc * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 382
diff changeset
44 tuple that contains the location of the original element or text in the
d6e9170c5ccc * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 382
diff changeset
45 input. If the original location is unknown, `position` is `(None, -1, -1)`.
d6e9170c5ccc * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 382
diff changeset
46
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
47 Also provided are ways to serialize the stream to text. The `serialize()`
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
48 method will return an iterator over generated strings, while `render()`
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
49 returns the complete generated text at once. Both accept various parameters
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
50 that impact the way the stream is serialized.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
51 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
52 __slots__ = ['events']
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
53
17
ad63ad459524 Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents: 10
diff changeset
54 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
55 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
56 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
57 DOCTYPE = StreamEventKind('DOCTYPE') # doctype declaration
143
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
58 START_NS = StreamEventKind('START_NS') # start namespace mapping
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
59 END_NS = StreamEventKind('END_NS') # end namespace mapping
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
60 START_CDATA = StreamEventKind('START_CDATA') # start CDATA section
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
61 END_CDATA = StreamEventKind('END_CDATA') # end CDATA section
17
ad63ad459524 Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents: 10
diff changeset
62 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
63 COMMENT = StreamEventKind('COMMENT') # comment
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
64
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
65 def __init__(self, events):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
66 """Initialize the stream with a sequence of markup events.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
67
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
68 @param events: a sequence or iterable providing the events
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
69 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
70 self.events = events
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
71
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
72 def __iter__(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
73 return iter(self.events)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
74
204
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
75 def __or__(self, function):
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
76 """Override the "bitwise or" operator to apply filters or serializers
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
77 to the stream, providing a syntax similar to pipes on Unix shells.
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
78
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
79 Assume the following stream produced by the `HTML` function:
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
80
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 224
diff changeset
81 >>> from genshi.input import HTML
204
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
82 >>> html = HTML('''<p onclick="alert('Whoa')">Hello, world!</p>''')
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
83 >>> print html
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
84 <p onclick="alert('Whoa')">Hello, world!</p>
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
85
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
86 A filter such as the HTML sanitizer can be applied to that stream using
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
87 the pipe notation as follows:
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
88
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 224
diff changeset
89 >>> from genshi.filters import HTMLSanitizer
204
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
90 >>> sanitizer = HTMLSanitizer()
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
91 >>> print html | sanitizer
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
92 <p>Hello, world!</p>
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
93
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
94 Filters can be any function that accepts and produces a stream (where
397
d6e9170c5ccc * Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports)
cmlenz
parents: 382
diff changeset
95 a stream is anything that iterates over events):
204
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
96
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
97 >>> def uppercase(stream):
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
98 ... for kind, data, pos in stream:
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
99 ... if kind is TEXT:
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
100 ... data = data.upper()
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
101 ... yield kind, data, pos
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
102 >>> print html | sanitizer | uppercase
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
103 <p>HELLO, WORLD!</p>
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
104
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
105 Serializers can also be used with this notation:
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
106
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 224
diff changeset
107 >>> from genshi.output import TextSerializer
204
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
108 >>> output = TextSerializer()
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
109 >>> print html | sanitizer | uppercase | output
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
110 HELLO, WORLD!
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
111
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
112 Commonly, serializers should be used at the end of the "pipeline";
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
113 using them somewhere in the middle may produce unexpected results.
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
114 """
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
115 return Stream(_ensure(function(self)))
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
116
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 116
diff changeset
117 def filter(self, *filters):
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 116
diff changeset
118 """Apply filters to the stream.
113
e815c2c07572 Removed the `sanitize()` method from the `Markup` class, and migrate the existing unit tests to `markup.tests.filters`. Provide a `Stream.filter()` method instead which can be used to conveniently apply a filter to a stream.
cmlenz
parents: 111
diff changeset
119
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 116
diff changeset
120 This method returns a new stream with the given filters applied. The
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 116
diff changeset
121 filters must be callables that accept the stream object as parameter,
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 116
diff changeset
122 and return the filtered stream.
204
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
123
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
124 The call:
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
125
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
126 stream.filter(filter1, filter2)
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
127
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
128 is equivalent to:
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
129
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
130 stream | filter1 | filter2
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
131 """
204
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
132 return reduce(operator.or_, (self,) + filters)
113
e815c2c07572 Removed the `sanitize()` method from the `Markup` class, and migrate the existing unit tests to `markup.tests.filters`. Provide a `Stream.filter()` method instead which can be used to conveniently apply a filter to a stream.
cmlenz
parents: 111
diff changeset
133
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 116
diff changeset
134 def render(self, method='xml', encoding='utf-8', **kwargs):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
135 """Return a string representation of the stream.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
136
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
137 @param method: determines how the stream is serialized; can be either
200
50eab0469148 Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents: 182
diff changeset
138 "xml", "xhtml", "html", "text", or a custom serializer
50eab0469148 Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents: 182
diff changeset
139 class
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
140 @param encoding: how the output string should be encoded; if set to
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
141 `None`, this method returns a `unicode` object
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
142
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
143 Any additional keyword arguments are passed to the serializer, and thus
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
144 depend on the `method` parameter value.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
145 """
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 116
diff changeset
146 generator = self.serialize(method=method, **kwargs)
17
ad63ad459524 Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents: 10
diff changeset
147 output = u''.join(list(generator))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
148 if encoding is not None:
200
50eab0469148 Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents: 182
diff changeset
149 errors = 'replace'
50eab0469148 Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents: 182
diff changeset
150 if method != 'text':
50eab0469148 Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents: 182
diff changeset
151 errors = 'xmlcharrefreplace'
50eab0469148 Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents: 182
diff changeset
152 return output.encode(encoding, errors)
8
ea47069a901c `Stream.render()` was masking `TypeError`s (fix based on suggestion by Matt Good).
cmlenz
parents: 6
diff changeset
153 return output
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
154
278
8de2620504b9 Fix the handling of namespace context for match templates.
cmlenz
parents: 230
diff changeset
155 def select(self, path, namespaces=None, variables=None):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
156 """Return a new stream that contains the events matching the given
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
157 XPath expression.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
158
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
159 @param path: a string containing the XPath expression
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
160 """
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 224
diff changeset
161 from genshi.path import Path
278
8de2620504b9 Fix the handling of namespace context for match templates.
cmlenz
parents: 230
diff changeset
162 return Path(path).select(self, namespaces, variables)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
163
123
93bbdcf9428b Fix for #18: whitespace in space-sensitive elements such as `<pre>` and `<textarea>` is now preserved.
cmlenz
parents: 116
diff changeset
164 def serialize(self, method='xml', **kwargs):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
165 """Generate strings corresponding to a specific serialization of the
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
166 stream.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
167
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
168 Unlike the `render()` method, this method is a generator that returns
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
169 the serialized output incrementally, as opposed to returning a single
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
170 string.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
171
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
172 @param method: determines how the stream is serialized; can be either
200
50eab0469148 Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents: 182
diff changeset
173 "xml", "xhtml", "html", "text", or a custom serializer
50eab0469148 Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents: 182
diff changeset
174 class
147
f7fb556f2678 Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 145
diff changeset
175
f7fb556f2678 Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 145
diff changeset
176 Any additional keyword arguments are passed to the serializer, and thus
f7fb556f2678 Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 145
diff changeset
177 depend on the `method` parameter value.
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
178 """
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 224
diff changeset
179 from genshi import output
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
180 cls = method
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
181 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
182 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
183 'xhtml': output.XHTMLSerializer,
200
50eab0469148 Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents: 182
diff changeset
184 'html': output.HTMLSerializer,
50eab0469148 Add serialization to plain text, based on cboos' patch. Closes #41.
cmlenz
parents: 182
diff changeset
185 'text': output.TextSerializer}[method]
204
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
186 return cls(**kwargs)(_ensure(self))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
187
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
188 def __str__(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
189 return self.render()
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
190
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
191 def __unicode__(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
192 return self.render(encoding=None)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
193
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
194
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
195 START = Stream.START
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
196 END = Stream.END
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
197 TEXT = Stream.TEXT
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
198 DOCTYPE = Stream.DOCTYPE
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
199 START_NS = Stream.START_NS
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
200 END_NS = Stream.END_NS
143
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
201 START_CDATA = Stream.START_CDATA
ef761afcedff CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents: 141
diff changeset
202 END_CDATA = Stream.END_CDATA
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
203 PI = Stream.PI
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
204 COMMENT = Stream.COMMENT
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
205
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
206 def _ensure(stream):
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
207 """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
208 for event in stream:
145
56d534eb53f9 * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 143
diff changeset
209 if type(event) is not tuple:
56d534eb53f9 * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 143
diff changeset
210 if hasattr(event, 'totuple'):
56d534eb53f9 * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 143
diff changeset
211 event = event.totuple()
56d534eb53f9 * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 143
diff changeset
212 else:
56d534eb53f9 * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 143
diff changeset
213 event = TEXT, unicode(event), (None, -1, -1)
56d534eb53f9 * Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents: 143
diff changeset
214 yield event
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 100
diff changeset
215
69
e9a3930f8823 A couple of minor performance improvements.
cmlenz
parents: 66
diff changeset
216
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
217 class Attrs(tuple):
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
218 """Immutable sequence type that stores the attributes of an element.
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
219
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
220 Ordering of the attributes is preserved, while accessing by name is also
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
221 supported.
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
222
182
41db0260ebb1 Renamed `Attributes` to `Attrs` to reduce the verbosity.
cmlenz
parents: 172
diff changeset
223 >>> attrs = Attrs([('href', '#'), ('title', 'Foo')])
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
224 >>> attrs
326
08ada6b4b767 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
225 Attrs([(QName(u'href'), '#'), (QName(u'title'), 'Foo')])
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
226
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
227 >>> 'href' in attrs
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
228 True
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
229 >>> 'tabindex' in attrs
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
230 False
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
231 >>> 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
232 'Foo'
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
233
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
234 Instances may not be manipulated directly. Instead, the operators `|` and
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
235 `-` can be used to produce new instances that have specific attributes
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
236 added, replaced or removed.
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
237
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
238 To remove an attribute, use the `-` operator. The right hand side can be
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
239 either a string or a set/sequence of strings, identifying the name(s) of
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
240 the attribute(s) to remove:
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
241
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
242 >>> attrs - 'title'
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
243 Attrs([(QName(u'href'), '#')])
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
244 >>> attrs - ('title', 'href')
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
245 Attrs()
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
246
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
247 The original instance is not modified, but the operator can of course be
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
248 used with an assignment:
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
249
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
250 >>> attrs
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
251 Attrs([(QName(u'href'), '#'), (QName(u'title'), 'Foo')])
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
252 >>> attrs -= 'title'
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
253 >>> attrs
326
08ada6b4b767 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
254 Attrs([(QName(u'href'), '#')])
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
255
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
256 To add a new attribute, use the `|` operator, where the right hand value
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
257 is a sequence of `(name, value)` tuples (which includes `Attrs` instances):
170
f1ac0510d392 Allow initialization of `Attributes` with keyword arguments.
cmlenz
parents: 161
diff changeset
258
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
259 >>> attrs | [(u'title', 'Bar')]
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
260 Attrs([(QName(u'href'), '#'), (QName(u'title'), 'Bar')])
171
34e0455ada92 Follow-up to [214]: allow initializing `Attributes` with attribute names that contain dashes or conflict with a reserved word (such as ?class?.)
cmlenz
parents: 170
diff changeset
261
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
262 If the attributes already contain an attribute with a given name, the value
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
263 of that attribute is replaced:
171
34e0455ada92 Follow-up to [214]: allow initializing `Attributes` with attribute names that contain dashes or conflict with a reserved word (such as ?class?.)
cmlenz
parents: 170
diff changeset
264
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
265 >>> attrs | [(u'href', 'http://example.org/')]
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
266 Attrs([(QName(u'href'), 'http://example.org/')])
171
34e0455ada92 Follow-up to [214]: allow initializing `Attributes` with attribute names that contain dashes or conflict with a reserved word (such as ?class?.)
cmlenz
parents: 170
diff changeset
267
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
268 """
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
269 __slots__ = []
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
270
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
271 def __new__(cls, items=()):
182
41db0260ebb1 Renamed `Attributes` to `Attrs` to reduce the verbosity.
cmlenz
parents: 172
diff changeset
272 """Create the `Attrs` instance.
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
273
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
274 If the `items` parameter is provided, it is expected to be a sequence
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
275 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
276 """
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
277 return tuple.__new__(cls, [(QName(name), val) for name, val in items])
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
278
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
279 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
280 """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
281 name.
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
282 """
133
b9a0031d4bbb Minor cleanup and performance improvement for the builder module.
cmlenz
parents: 123
diff changeset
283 for attr, _ in self:
b9a0031d4bbb Minor cleanup and performance improvement for the builder module.
cmlenz
parents: 123
diff changeset
284 if attr == name:
b9a0031d4bbb Minor cleanup and performance improvement for the builder module.
cmlenz
parents: 123
diff changeset
285 return True
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
286
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
287 def __getslice__(self, i, j):
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
288 return Attrs(tuple.__getslice__(self, i, j))
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
289
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
290 def __or__(self, attrs):
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
291 """Return a new instance that contains the attributes in `attrs` in
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
292 addition to any already existing attributes.
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
293 """
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
294 repl = dict([(an, av) for an, av in attrs if an in self])
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
295 return Attrs([(sn, repl.get(sn, sv)) for sn, sv in self] +
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
296 [(an, av) for an, av in attrs if an not in self])
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
297
326
08ada6b4b767 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
298 def __repr__(self):
08ada6b4b767 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
299 if not self:
08ada6b4b767 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
300 return 'Attrs()'
345
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
301 return 'Attrs([%s])' % ', '.join([repr(item) for item in self])
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
302
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
303 def __sub__(self, names):
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
304 """Return a new instance with all attributes with a name in `names` are
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
305 removed.
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
306 """
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
307 if isinstance(names, basestring):
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
308 names = (names,)
8e75b83d3e71 Make `Attrs` instances immutable.
cmlenz
parents: 326
diff changeset
309 return Attrs([(name, val) for name, val in self if name not in names])
326
08ada6b4b767 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
310
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
311 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
312 """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
313 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
314 """
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
315 for attr, value in self:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
316 if attr == name:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
317 return value
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
318 return default
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
319
77
f1aa49c759b2 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 73
diff changeset
320 def totuple(self):
161
a25f9fc5787d Various docstring additions and other cosmetic changes.
cmlenz
parents: 147
diff changeset
321 """Return the attributes as a markup event.
a25f9fc5787d Various docstring additions and other cosmetic changes.
cmlenz
parents: 147
diff changeset
322
a25f9fc5787d Various docstring additions and other cosmetic changes.
cmlenz
parents: 147
diff changeset
323 The returned event is a TEXT event, the data is the value of all
a25f9fc5787d Various docstring additions and other cosmetic changes.
cmlenz
parents: 147
diff changeset
324 attributes joined together.
a25f9fc5787d Various docstring additions and other cosmetic changes.
cmlenz
parents: 147
diff changeset
325 """
77
f1aa49c759b2 * Simplify implementation of the individual XPath tests (use closures instead of callable classes)
cmlenz
parents: 73
diff changeset
326 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
327
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
328
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
329 class Markup(unicode):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
330 """Marks a string as being safe for inclusion in HTML/XML output without
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
331 needing to be escaped.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
332 """
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
333 __slots__ = []
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
334
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
335 def __new__(cls, text='', *args):
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
336 if args:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 133
diff changeset
337 text %= tuple(map(escape, args))
27
b8456279c444 * Fix the boilerplate in the Python source files.
cmlenz
parents: 18
diff changeset
338 return unicode.__new__(cls, text)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
339
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
340 def __add__(self, other):
204
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
341 return Markup(unicode(self) + unicode(escape(other)))
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
342
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
343 def __radd__(self, other):
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
344 return Markup(unicode(escape(other)) + unicode(self))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
345
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
346 def __mod__(self, args):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
347 if not isinstance(args, (list, tuple)):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
348 args = [args]
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 133
diff changeset
349 return Markup(unicode.__mod__(self, tuple(map(escape, args))))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
350
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
351 def __mul__(self, num):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
352 return Markup(unicode(self) * num)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
353
204
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
354 def __rmul__(self, num):
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
355 return Markup(num * unicode(self))
516a6cae0aa8 * Implement reverse add/mul operators for `Markup` class, so that the result is also a `Markup` instance.
cmlenz
parents: 200
diff changeset
356
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
357 def __repr__(self):
382
d7da3fba7faf * Added documentation for the various stream event kinds.
cmlenz
parents: 377
diff changeset
358 return '<%s %r>' % (self.__class__.__name__, unicode(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
359
54
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 34
diff changeset
360 def join(self, seq, escape_quotes=True):
01981cbc7575 Fix a number of escaping problems:
cmlenz
parents: 34
diff changeset
361 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
362 for item in seq]))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
363
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
364 def escape(cls, text, quotes=True):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
365 """Create a Markup instance from a string and escape special characters
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
366 it may contain (<, >, & and \").
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
367
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
368 If the `quotes` parameter is set to `False`, the \" character is left
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
369 as is. Escaping quotes is generally only required for strings that are
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
370 to be used in attribute values.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
371 """
73
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
372 if not text:
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
373 return cls()
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
374 if type(text) is cls:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
375 return text
73
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
376 text = unicode(text).replace('&', '&amp;') \
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
377 .replace('<', '&lt;') \
b0fd16111f2e Some more performance tweaks.
cmlenz
parents: 69
diff changeset
378 .replace('>', '&gt;')
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
379 if quotes:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
380 text = text.replace('"', '&#34;')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
381 return cls(text)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
382 escape = classmethod(escape)
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
383
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
384 def unescape(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
385 """Reverse-escapes &, <, > and \" and returns a `unicode` object."""
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
386 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
387 return u''
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
388 return unicode(self).replace('&#34;', '"') \
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
389 .replace('&gt;', '>') \
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
390 .replace('&lt;', '<') \
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
391 .replace('&amp;', '&')
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
392
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
393 def stripentities(self, keepxmlentities=False):
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
394 """Return a copy of the text with any character or numeric entities
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
395 replaced by the equivalent UTF-8 characters.
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
396
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
397 If the `keepxmlentities` parameter is provided and evaluates to `True`,
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
398 the core XML entities (&amp;, &apos;, &gt;, &lt; and &quot;) are not
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
399 stripped.
6
5da45906dda7 Simplified implementation of `py:content` directive.
cmlenz
parents: 5
diff changeset
400 """
116
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
401 return Markup(stripentities(self, keepxmlentities=keepxmlentities))
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
402
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
403 def striptags(self):
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
404 """Return a copy of the text with all XML/HTML tags removed."""
88ac4c680120 Merged [135:138/branches/experimental/cspeedups].
cmlenz
parents: 115
diff changeset
405 return Markup(striptags(self))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
406
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
407
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
408 escape = Markup.escape
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
409
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
410 def unescape(text):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
411 """Reverse-escapes &, <, > and \" and returns a `unicode` object."""
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
412 if not isinstance(text, Markup):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
413 return text
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
414 return text.unescape()
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
415
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
416
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
417 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
418 """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
419
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
420 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
421 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
422 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
423
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
424 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
425
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
426 >>> 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
427 >>> html
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
428 <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
429 >>> html.uri
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
430 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
431
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
432 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
433 that namespace:
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
434
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
435 >>> html.body
326
08ada6b4b767 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
436 QName(u'http://www.w3.org/1999/xhtml}body')
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
437 >>> html.body.localname
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
438 u'body'
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
439 >>> html.body.namespace
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
440 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
441
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
442 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
443 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
444
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
445 >>> html['body']
326
08ada6b4b767 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
446 QName(u'http://www.w3.org/1999/xhtml}body')
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
447
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
448 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
449 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
450
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
451 >>> qname = html.body
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
452 >>> qname in html
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
453 True
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
454 >>> 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
455 False
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
456 """
224
e4dad1145f84 Implement support for namespace prefixes in XPath expressions.
cmlenz
parents: 204
diff changeset
457 def __new__(cls, uri):
e4dad1145f84 Implement support for namespace prefixes in XPath expressions.
cmlenz
parents: 204
diff changeset
458 if type(uri) is cls:
e4dad1145f84 Implement support for namespace prefixes in XPath expressions.
cmlenz
parents: 204
diff changeset
459 return uri
e4dad1145f84 Implement support for namespace prefixes in XPath expressions.
cmlenz
parents: 204
diff changeset
460 return object.__new__(cls, uri)
e4dad1145f84 Implement support for namespace prefixes in XPath expressions.
cmlenz
parents: 204
diff changeset
461
279
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
462 def __getnewargs__(self):
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
463 return (self.uri,)
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
464
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
465 def __getstate__(self):
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
466 return self.uri
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
467
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
468 def __setstate__(self, uri):
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
469 self.uri = uri
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
470
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
471 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
472 self.uri = unicode(uri)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
473
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
474 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
475 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
476
278
8de2620504b9 Fix the handling of namespace context for match templates.
cmlenz
parents: 230
diff changeset
477 def __ne__(self, other):
8de2620504b9 Fix the handling of namespace context for match templates.
cmlenz
parents: 230
diff changeset
478 return not self == other
8de2620504b9 Fix the handling of namespace context for match templates.
cmlenz
parents: 230
diff changeset
479
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
480 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
481 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
482 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
483 return self.uri == other
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
484
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
485 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
486 return QName(self.uri + u'}' + name)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
487 __getattr__ = __getitem__
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
488
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
489 def __repr__(self):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
490 return '<Namespace "%s">' % self.uri
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
491
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
492 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
493 return self.uri.encode('utf-8')
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
494
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
495 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
496 return self.uri
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
497
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
498
147
f7fb556f2678 Use `xmlcharrefreplace` when encoding the output in `Stream.render()`, so that encoding the output to legacy encodings such as ASCII or ISO-8859-1 should always work.
cmlenz
parents: 145
diff changeset
499 # The namespace used by attributes such as xml:lang and xml:space
141
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
500 XML_NAMESPACE = Namespace('http://www.w3.org/XML/1998/namespace')
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
501
b3ceaa35fb6b * No escaping of `<script>` or `<style>` tags in HTML output (see #24)
cmlenz
parents: 140
diff changeset
502
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
503 class QName(unicode):
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
504 """A qualified element or attribute name.
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
505
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
506 The unicode value of instances of this class contains the qualified name of
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
507 the element or attribute, in the form `{namespace}localname`. The namespace
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
508 URI can be obtained through the additional `namespace` attribute, while the
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
509 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
510
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
511 >>> qname = QName('foo')
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
512 >>> qname
326
08ada6b4b767 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
513 QName(u'foo')
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
514 >>> qname.localname
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
515 u'foo'
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
516 >>> qname.namespace
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
517
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
518 >>> 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
519 >>> qname
326
08ada6b4b767 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
520 QName(u'http://www.w3.org/1999/xhtml}body')
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
521 >>> qname.localname
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
522 u'body'
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
523 >>> qname.namespace
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
524 u'http://www.w3.org/1999/xhtml'
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
525 """
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
526 __slots__ = ['namespace', 'localname']
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
527
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
528 def __new__(cls, qname):
100
5e9987f34e6c Ported [111] to trunk.
cmlenz
parents: 96
diff changeset
529 if type(qname) is cls:
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
530 return qname
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
531
18
4cbebb15a834 Actually make use of the `markup.core.Namespace` class, and add a couple of doctests.
cmlenz
parents: 17
diff changeset
532 parts = qname.split(u'}', 1)
100
5e9987f34e6c Ported [111] to trunk.
cmlenz
parents: 96
diff changeset
533 if len(parts) > 1:
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 133
diff changeset
534 self = unicode.__new__(cls, u'{%s' % qname)
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 133
diff changeset
535 self.namespace, self.localname = map(unicode, parts)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
536 else:
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
537 self = unicode.__new__(cls, qname)
136
636e0100fcaf Minor performance improvements in serialization.
cmlenz
parents: 133
diff changeset
538 self.namespace, self.localname = None, unicode(qname)
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
539 return self
279
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
540
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
541 def __getnewargs__(self):
4b5b4653d41e Some adjustments to make core data structures picklable (requires protocol 2).
cmlenz
parents: 278
diff changeset
542 return (self.lstrip('{'),)
326
08ada6b4b767 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
543
08ada6b4b767 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
544 def __repr__(self):
08ada6b4b767 Fixed `__repr__` of the `QName`, `Attrs`, and `Expression` classes so that the output can be used as code to instantiate the object again.
cmlenz
parents: 279
diff changeset
545 return 'QName(%s)' % unicode.__repr__(self.lstrip('{'))
Copyright (C) 2012-2017 Edgewall Software