Mercurial > genshi > genshi-test
annotate genshi/core.py @ 920:f4bf11c716cc experimental-py3k
Fix handling of QNames with leading open curly braces. See Genshi ticket #413.
author | hodgestar |
---|---|
date | Mon, 29 Nov 2010 20:20:56 +0000 |
parents | 9fafb35032a1 |
children |
rev | line source |
---|---|
860
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
1 # -*- coding: utf-8 -*- |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
2 # |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
3 # Copyright (C) 2006-2009 Edgewall Software |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
4 # All rights reserved. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
5 # |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
6 # This software is licensed as described in the file COPYING, which |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
7 # you should have received as part of this distribution. The terms |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
8 # are also available at http://genshi.edgewall.org/wiki/License. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
9 # |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
10 # This software consists of voluntary contributions made by many |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
11 # individuals. For the exact contribution history, see the revision |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
12 # history and logs, available at http://genshi.edgewall.org/log/. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
13 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
14 """Core classes for markup processing.""" |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
15 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
16 try: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
17 reduce # builtin in Python < 3 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
18 except NameError: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
19 from functools import reduce |
915
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
20 import sys |
860
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
21 from itertools import chain |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
22 import operator |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
23 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
24 from genshi.util import plaintext, stripentities, striptags, stringrepr |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
25 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
26 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Attrs', 'Namespace', |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
27 'QName'] |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
28 __docformat__ = 'restructuredtext en' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
29 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
30 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
31 class StreamEventKind(str): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
32 """A kind of event on a markup stream.""" |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
33 __slots__ = [] |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
34 _instances = {} |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
35 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
36 def __new__(cls, val): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
37 return cls._instances.setdefault(val, str.__new__(cls, val)) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
38 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
39 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
40 class Stream(object): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
41 """Represents a stream of markup events. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
42 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
43 This class is basically an iterator over the events. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
44 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
45 Stream events are tuples of the form:: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
46 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
47 (kind, data, position) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
48 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
49 where ``kind`` is the event kind (such as `START`, `END`, `TEXT`, etc), |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
50 ``data`` depends on the kind of event, and ``position`` is a |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
51 ``(filename, line, offset)`` tuple that contains the location of the |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
52 original element or text in the input. If the original location is unknown, |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
53 ``position`` is ``(None, -1, -1)``. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
54 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
55 Also provided are ways to serialize the stream to text. The `serialize()` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
56 method will return an iterator over generated strings, while `render()` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
57 returns the complete generated text at once. Both accept various parameters |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
58 that impact the way the stream is serialized. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
59 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
60 __slots__ = ['events', 'serializer'] |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
61 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
62 START = StreamEventKind('START') #: a start tag |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
63 END = StreamEventKind('END') #: an end tag |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
64 TEXT = StreamEventKind('TEXT') #: literal text |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
65 XML_DECL = StreamEventKind('XML_DECL') #: XML declaration |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
66 DOCTYPE = StreamEventKind('DOCTYPE') #: doctype declaration |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
67 START_NS = StreamEventKind('START_NS') #: start namespace mapping |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
68 END_NS = StreamEventKind('END_NS') #: end namespace mapping |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
69 START_CDATA = StreamEventKind('START_CDATA') #: start CDATA section |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
70 END_CDATA = StreamEventKind('END_CDATA') #: end CDATA section |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
71 PI = StreamEventKind('PI') #: processing instruction |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
72 COMMENT = StreamEventKind('COMMENT') #: comment |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
73 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
74 def __init__(self, events, serializer=None): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
75 """Initialize the stream with a sequence of markup events. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
76 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
77 :param events: a sequence or iterable providing the events |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
78 :param serializer: the default serialization method to use for this |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
79 stream |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
80 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
81 :note: Changed in 0.5: added the `serializer` argument |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
82 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
83 self.events = events #: The underlying iterable producing the events |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
84 self.serializer = serializer #: The default serializion method |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
85 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
86 def __iter__(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
87 return iter(self.events) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
88 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
89 def __or__(self, function): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
90 """Override the "bitwise or" operator to apply filters or serializers |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
91 to the stream, providing a syntax similar to pipes on Unix shells. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
92 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
93 Assume the following stream produced by the `HTML` function: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
94 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
95 >>> from genshi.input import HTML |
915
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
96 >>> html = HTML('''<p onclick="alert('Whoa')">Hello, world!</p>''', encoding='utf-8') |
860
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
97 >>> print(html) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
98 <p onclick="alert('Whoa')">Hello, world!</p> |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
99 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
100 A filter such as the HTML sanitizer can be applied to that stream using |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
101 the pipe notation as follows: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
102 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
103 >>> from genshi.filters import HTMLSanitizer |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
104 >>> sanitizer = HTMLSanitizer() |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
105 >>> print(html | sanitizer) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
106 <p>Hello, world!</p> |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
107 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
108 Filters can be any function that accepts and produces a stream (where |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
109 a stream is anything that iterates over events): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
110 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
111 >>> def uppercase(stream): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
112 ... for kind, data, pos in stream: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
113 ... if kind is TEXT: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
114 ... data = data.upper() |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
115 ... yield kind, data, pos |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
116 >>> print(html | sanitizer | uppercase) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
117 <p>HELLO, WORLD!</p> |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
118 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
119 Serializers can also be used with this notation: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
120 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
121 >>> from genshi.output import TextSerializer |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
122 >>> output = TextSerializer() |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
123 >>> print(html | sanitizer | uppercase | output) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
124 HELLO, WORLD! |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
125 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
126 Commonly, serializers should be used at the end of the "pipeline"; |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
127 using them somewhere in the middle may produce unexpected results. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
128 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
129 :param function: the callable object that should be applied as a filter |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
130 :return: the filtered stream |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
131 :rtype: `Stream` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
132 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
133 return Stream(_ensure(function(self)), serializer=self.serializer) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
134 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
135 def filter(self, *filters): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
136 """Apply filters to the stream. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
137 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
138 This method returns a new stream with the given filters applied. The |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
139 filters must be callables that accept the stream object as parameter, |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
140 and return the filtered stream. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
141 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
142 The call:: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
143 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
144 stream.filter(filter1, filter2) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
145 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
146 is equivalent to:: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
147 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
148 stream | filter1 | filter2 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
149 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
150 :param filters: one or more callable objects that should be applied as |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
151 filters |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
152 :return: the filtered stream |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
153 :rtype: `Stream` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
154 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
155 return reduce(operator.or_, (self,) + filters) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
156 |
915
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
157 def render(self, method=None, encoding=None, out=None, **kwargs): |
860
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
158 """Return a string representation of the stream. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
159 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
160 Any additional keyword arguments are passed to the serializer, and thus |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
161 depend on the `method` parameter value. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
162 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
163 :param method: determines how the stream is serialized; can be either |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
164 "xml", "xhtml", "html", "text", or a custom serializer |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
165 class; if `None`, the default serialization method of |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
166 the stream is used |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
167 :param encoding: how the output string should be encoded; if set to |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
168 `None`, this method returns a `unicode` object |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
169 :param out: a file-like object that the output should be written to |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
170 instead of being returned as one big string; note that if |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
171 this is a file or socket (or similar), the `encoding` must |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
172 not be `None` (that is, the output must be encoded) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
173 :return: a `str` or `unicode` object (depending on the `encoding` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
174 parameter), or `None` if the `out` parameter is provided |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
175 :rtype: `basestring` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
176 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
177 :see: XMLSerializer, XHTMLSerializer, HTMLSerializer, TextSerializer |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
178 :note: Changed in 0.5: added the `out` parameter |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
179 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
180 from genshi.output import encode |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
181 if method is None: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
182 method = self.serializer or 'xml' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
183 generator = self.serialize(method=method, **kwargs) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
184 return encode(generator, method=method, encoding=encoding, out=out) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
185 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
186 def select(self, path, namespaces=None, variables=None): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
187 """Return a new stream that contains the events matching the given |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
188 XPath expression. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
189 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
190 >>> from genshi import HTML |
915
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
191 >>> stream = HTML('<doc><elem>foo</elem><elem>bar</elem></doc>', encoding='utf-8') |
860
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
192 >>> print(stream.select('elem')) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
193 <elem>foo</elem><elem>bar</elem> |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
194 >>> print(stream.select('elem/text()')) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
195 foobar |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
196 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
197 Note that the outermost element of the stream becomes the *context |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
198 node* for the XPath test. That means that the expression "doc" would |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
199 not match anything in the example above, because it only tests against |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
200 child elements of the outermost element: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
201 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
202 >>> print(stream.select('doc')) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
203 <BLANKLINE> |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
204 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
205 You can use the "." expression to match the context node itself |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
206 (although that usually makes little sense): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
207 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
208 >>> print(stream.select('.')) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
209 <doc><elem>foo</elem><elem>bar</elem></doc> |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
210 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
211 :param path: a string containing the XPath expression |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
212 :param namespaces: mapping of namespace prefixes used in the path |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
213 :param variables: mapping of variable names to values |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
214 :return: the selected substream |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
215 :rtype: `Stream` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
216 :raises PathSyntaxError: if the given path expression is invalid or not |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
217 supported |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
218 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
219 from genshi.path import Path |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
220 return Path(path).select(self, namespaces, variables) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
221 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
222 def serialize(self, method='xml', **kwargs): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
223 """Generate strings corresponding to a specific serialization of the |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
224 stream. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
225 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
226 Unlike the `render()` method, this method is a generator that returns |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
227 the serialized output incrementally, as opposed to returning a single |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
228 string. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
229 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
230 Any additional keyword arguments are passed to the serializer, and thus |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
231 depend on the `method` parameter value. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
232 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
233 :param method: determines how the stream is serialized; can be either |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
234 "xml", "xhtml", "html", "text", or a custom serializer |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
235 class; if `None`, the default serialization method of |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
236 the stream is used |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
237 :return: an iterator over the serialization results (`Markup` or |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
238 `unicode` objects, depending on the serialization method) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
239 :rtype: ``iterator`` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
240 :see: XMLSerializer, XHTMLSerializer, HTMLSerializer, TextSerializer |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
241 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
242 from genshi.output import get_serializer |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
243 if method is None: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
244 method = self.serializer or 'xml' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
245 return get_serializer(method, **kwargs)(_ensure(self)) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
246 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
247 def __str__(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
248 return self.render() |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
249 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
250 def __unicode__(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
251 return self.render(encoding=None) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
252 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
253 def __html__(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
254 return self |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
255 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
256 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
257 START = Stream.START |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
258 END = Stream.END |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
259 TEXT = Stream.TEXT |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
260 XML_DECL = Stream.XML_DECL |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
261 DOCTYPE = Stream.DOCTYPE |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
262 START_NS = Stream.START_NS |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
263 END_NS = Stream.END_NS |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
264 START_CDATA = Stream.START_CDATA |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
265 END_CDATA = Stream.END_CDATA |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
266 PI = Stream.PI |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
267 COMMENT = Stream.COMMENT |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
268 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
269 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
270 def _ensure(stream): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
271 """Ensure that every item on the stream is actually a markup event.""" |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
272 stream = iter(stream) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
273 event = stream.next() |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
274 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
275 # Check whether the iterable is a real markup event stream by examining the |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
276 # first item it yields; if it's not we'll need to do some conversion |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
277 if type(event) is not tuple or len(event) != 3: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
278 for event in chain([event], stream): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
279 if hasattr(event, 'totuple'): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
280 event = event.totuple() |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
281 else: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
282 event = TEXT, unicode(event), (None, -1, -1) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
283 yield event |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
284 return |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
285 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
286 # This looks like a markup event stream, so we'll just pass it through |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
287 # unchanged |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
288 yield event |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
289 for event in stream: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
290 yield event |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
291 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
292 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
293 class Attrs(tuple): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
294 """Immutable sequence type that stores the attributes of an element. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
295 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
296 Ordering of the attributes is preserved, while access by name is also |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
297 supported. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
298 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
299 >>> attrs = Attrs([('href', '#'), ('title', 'Foo')]) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
300 >>> attrs |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
301 Attrs([('href', '#'), ('title', 'Foo')]) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
302 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
303 >>> 'href' in attrs |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
304 True |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
305 >>> 'tabindex' in attrs |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
306 False |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
307 >>> attrs.get('title') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
308 'Foo' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
309 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
310 Instances may not be manipulated directly. Instead, the operators ``|`` and |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
311 ``-`` can be used to produce new instances that have specific attributes |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
312 added, replaced or removed. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
313 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
314 To remove an attribute, use the ``-`` operator. The right hand side can be |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
315 either a string or a set/sequence of strings, identifying the name(s) of |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
316 the attribute(s) to remove: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
317 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
318 >>> attrs - 'title' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
319 Attrs([('href', '#')]) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
320 >>> attrs - ('title', 'href') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
321 Attrs() |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
322 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
323 The original instance is not modified, but the operator can of course be |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
324 used with an assignment: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
325 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
326 >>> attrs |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
327 Attrs([('href', '#'), ('title', 'Foo')]) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
328 >>> attrs -= 'title' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
329 >>> attrs |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
330 Attrs([('href', '#')]) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
331 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
332 To add a new attribute, use the ``|`` operator, where the right hand value |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
333 is a sequence of ``(name, value)`` tuples (which includes `Attrs` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
334 instances): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
335 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
336 >>> attrs | [('title', 'Bar')] |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
337 Attrs([('href', '#'), ('title', 'Bar')]) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
338 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
339 If the attributes already contain an attribute with a given name, the value |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
340 of that attribute is replaced: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
341 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
342 >>> attrs | [('href', 'http://example.org/')] |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
343 Attrs([('href', 'http://example.org/')]) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
344 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
345 __slots__ = [] |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
346 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
347 def __contains__(self, name): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
348 """Return whether the list includes an attribute with the specified |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
349 name. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
350 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
351 :return: `True` if the list includes the attribute |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
352 :rtype: `bool` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
353 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
354 for attr, _ in self: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
355 if attr == name: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
356 return True |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
357 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
358 def __getitem__(self, i): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
359 """Return an item or slice of the attributes list. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
360 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
361 >>> attrs = Attrs([('href', '#'), ('title', 'Foo')]) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
362 >>> attrs[1] |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
363 ('title', 'Foo') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
364 >>> attrs[1:] |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
365 Attrs([('title', 'Foo')]) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
366 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
367 items = tuple.__getitem__(self, i) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
368 if type(i) is slice: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
369 return Attrs(items) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
370 return items |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
371 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
372 def __getslice__(self, i, j): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
373 """Return a slice of the attributes list. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
374 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
375 >>> attrs = Attrs([('href', '#'), ('title', 'Foo')]) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
376 >>> attrs[1:] |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
377 Attrs([('title', 'Foo')]) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
378 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
379 return Attrs(tuple.__getslice__(self, i, j)) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
380 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
381 def __or__(self, attrs): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
382 """Return a new instance that contains the attributes in `attrs` in |
904
2772fa7c10a3
The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents:
862
diff
changeset
|
383 addition to any already existing attributes. Any attributes in the new |
2772fa7c10a3
The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents:
862
diff
changeset
|
384 set that have a value of `None` are removed. |
860
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
385 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
386 :return: a new instance with the merged attributes |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
387 :rtype: `Attrs` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
388 """ |
904
2772fa7c10a3
The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents:
862
diff
changeset
|
389 remove = set([an for an, av in attrs if av is None]) |
2772fa7c10a3
The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents:
862
diff
changeset
|
390 replace = dict([(an, av) for an, av in attrs |
2772fa7c10a3
The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents:
862
diff
changeset
|
391 if an in self and av is not None]) |
2772fa7c10a3
The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents:
862
diff
changeset
|
392 return Attrs([(sn, replace.get(sn, sv)) for sn, sv in self |
2772fa7c10a3
The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents:
862
diff
changeset
|
393 if sn not in remove] + |
2772fa7c10a3
The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents:
862
diff
changeset
|
394 [(an, av) for an, av in attrs |
2772fa7c10a3
The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents:
862
diff
changeset
|
395 if an not in self and an not in remove]) |
860
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
396 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
397 def __repr__(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
398 if not self: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
399 return 'Attrs()' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
400 return 'Attrs([%s])' % ', '.join([repr(item) for item in self]) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
401 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
402 def __sub__(self, names): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
403 """Return a new instance with all attributes with a name in `names` are |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
404 removed. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
405 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
406 :param names: the names of the attributes to remove |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
407 :return: a new instance with the attribute removed |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
408 :rtype: `Attrs` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
409 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
410 if isinstance(names, basestring): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
411 names = (names,) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
412 return Attrs([(name, val) for name, val in self if name not in names]) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
413 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
414 def get(self, name, default=None): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
415 """Return the value of the attribute with the specified name, or the |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
416 value of the `default` parameter if no such attribute is found. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
417 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
418 :param name: the name of the attribute |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
419 :param default: the value to return when the attribute does not exist |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
420 :return: the attribute value, or the `default` value if that attribute |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
421 does not exist |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
422 :rtype: `object` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
423 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
424 for attr, value in self: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
425 if attr == name: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
426 return value |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
427 return default |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
428 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
429 def totuple(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
430 """Return the attributes as a markup event. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
431 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
432 The returned event is a `TEXT` event, the data is the value of all |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
433 attributes joined together. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
434 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
435 >>> Attrs([('href', '#'), ('title', 'Foo')]).totuple() |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
436 ('TEXT', '#Foo', (None, -1, -1)) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
437 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
438 :return: a `TEXT` event |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
439 :rtype: `tuple` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
440 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
441 return TEXT, ''.join([x[1] for x in self]), (None, -1, -1) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
442 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
443 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
444 class Markup(unicode): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
445 """Marks a string as being safe for inclusion in HTML/XML output without |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
446 needing to be escaped. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
447 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
448 __slots__ = [] |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
449 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
450 def __add__(self, other): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
451 return Markup(unicode.__add__(self, escape(other))) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
452 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
453 def __radd__(self, other): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
454 return Markup(unicode.__add__(escape(other), self)) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
455 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
456 def __mod__(self, args): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
457 if isinstance(args, dict): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
458 args = dict(zip(args.keys(), map(escape, args.values()))) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
459 elif isinstance(args, (list, tuple)): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
460 args = tuple(map(escape, args)) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
461 else: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
462 args = escape(args) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
463 return Markup(unicode.__mod__(self, args)) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
464 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
465 def __mul__(self, num): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
466 return Markup(unicode.__mul__(self, num)) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
467 __rmul__ = __mul__ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
468 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
469 def __repr__(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
470 return "<%s %s>" % (type(self).__name__, unicode.__repr__(self)) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
471 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
472 def join(self, seq, escape_quotes=True): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
473 """Return a `Markup` object which is the concatenation of the strings |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
474 in the given sequence, where this `Markup` object is the separator |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
475 between the joined elements. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
476 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
477 Any element in the sequence that is not a `Markup` instance is |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
478 automatically escaped. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
479 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
480 :param seq: the sequence of strings to join |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
481 :param escape_quotes: whether double quote characters in the elements |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
482 should be escaped |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
483 :return: the joined `Markup` object |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
484 :rtype: `Markup` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
485 :see: `escape` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
486 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
487 return Markup(unicode.join(self, [escape(item, quotes=escape_quotes) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
488 for item in seq])) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
489 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
490 @classmethod |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
491 def escape(cls, text, quotes=True): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
492 """Create a Markup instance from a string and escape special characters |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
493 it may contain (<, >, & and \"). |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
494 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
495 >>> escape('"1 < 2"') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
496 <Markup u'"1 < 2"'> |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
497 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
498 If the `quotes` parameter is set to `False`, the \" character is left |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
499 as is. Escaping quotes is generally only required for strings that are |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
500 to be used in attribute values. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
501 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
502 >>> escape('"1 < 2"', quotes=False) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
503 <Markup u'"1 < 2"'> |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
504 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
505 :param text: the text to escape |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
506 :param quotes: if ``True``, double quote characters are escaped in |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
507 addition to the other special characters |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
508 :return: the escaped `Markup` string |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
509 :rtype: `Markup` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
510 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
511 if not text: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
512 return cls() |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
513 if type(text) is cls: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
514 return text |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
515 if hasattr(text, '__html__'): |
904
2772fa7c10a3
The `|` operator on `Attrs` objects now removes any existing attributes if the value is `None`.
cmlenz
parents:
862
diff
changeset
|
516 return cls(text.__html__()) |
860
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
517 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
518 text = text.replace('&', '&') \ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
519 .replace('<', '<') \ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
520 .replace('>', '>') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
521 if quotes: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
522 text = text.replace('"', '"') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
523 return cls(text) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
524 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
525 def unescape(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
526 """Reverse-escapes &, <, >, and \" and returns a `unicode` object. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
527 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
528 >>> Markup('1 < 2').unescape() |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
529 u'1 < 2' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
530 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
531 :return: the unescaped string |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
532 :rtype: `unicode` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
533 :see: `genshi.core.unescape` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
534 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
535 if not self: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
536 return '' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
537 return unicode(self).replace('"', '"') \ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
538 .replace('>', '>') \ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
539 .replace('<', '<') \ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
540 .replace('&', '&') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
541 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
542 def stripentities(self, keepxmlentities=False): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
543 """Return a copy of the text with any character or numeric entities |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
544 replaced by the equivalent UTF-8 characters. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
545 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
546 If the `keepxmlentities` parameter is provided and evaluates to `True`, |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
547 the core XML entities (``&``, ``'``, ``>``, ``<`` and |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
548 ``"``) are not stripped. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
549 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
550 :return: a `Markup` instance with entities removed |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
551 :rtype: `Markup` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
552 :see: `genshi.util.stripentities` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
553 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
554 return Markup(stripentities(self, keepxmlentities=keepxmlentities)) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
555 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
556 def striptags(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
557 """Return a copy of the text with all XML/HTML tags removed. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
558 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
559 :return: a `Markup` instance with all tags removed |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
560 :rtype: `Markup` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
561 :see: `genshi.util.striptags` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
562 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
563 return Markup(striptags(self)) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
564 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
565 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
566 try: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
567 from genshi._speedups import Markup |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
568 except ImportError: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
569 pass # just use the Python implementation |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
570 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
571 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
572 escape = Markup.escape |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
573 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
574 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
575 def unescape(text): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
576 """Reverse-escapes &, <, >, and \" and returns a `unicode` object. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
577 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
578 >>> unescape(Markup('1 < 2')) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
579 u'1 < 2' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
580 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
581 If the provided `text` object is not a `Markup` instance, it is returned |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
582 unchanged. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
583 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
584 >>> unescape('1 < 2') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
585 '1 < 2' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
586 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
587 :param text: the text to unescape |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
588 :return: the unescsaped string |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
589 :rtype: `unicode` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
590 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
591 if not isinstance(text, Markup): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
592 return text |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
593 return text.unescape() |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
594 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
595 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
596 class Namespace(object): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
597 """Utility class creating and testing elements with a namespace. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
598 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
599 Internally, namespace URIs are encoded in the `QName` of any element or |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
600 attribute, the namespace URI being enclosed in curly braces. This class |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
601 helps create and test these strings. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
602 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
603 A `Namespace` object is instantiated with the namespace URI. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
604 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
605 >>> html = Namespace('http://www.w3.org/1999/xhtml') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
606 >>> html |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
607 Namespace('http://www.w3.org/1999/xhtml') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
608 >>> html.uri |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
609 u'http://www.w3.org/1999/xhtml' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
610 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
611 The `Namespace` object can than be used to generate `QName` objects with |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
612 that namespace: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
613 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
614 >>> html.body |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
615 QName('http://www.w3.org/1999/xhtml}body') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
616 >>> html.body.localname |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
617 u'body' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
618 >>> html.body.namespace |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
619 u'http://www.w3.org/1999/xhtml' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
620 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
621 The same works using item access notation, which is useful for element or |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
622 attribute names that are not valid Python identifiers: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
623 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
624 >>> html['body'] |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
625 QName('http://www.w3.org/1999/xhtml}body') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
626 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
627 A `Namespace` object can also be used to test whether a specific `QName` |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
628 belongs to that namespace using the ``in`` operator: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
629 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
630 >>> qname = html.body |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
631 >>> qname in html |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
632 True |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
633 >>> qname in Namespace('http://www.w3.org/2002/06/xhtml2') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
634 False |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
635 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
636 def __new__(cls, uri): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
637 if type(uri) is cls: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
638 return uri |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
639 return object.__new__(cls) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
640 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
641 def __getnewargs__(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
642 return (self.uri,) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
643 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
644 def __getstate__(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
645 return self.uri |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
646 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
647 def __setstate__(self, uri): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
648 self.uri = uri |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
649 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
650 def __init__(self, uri): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
651 self.uri = unicode(uri) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
652 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
653 def __contains__(self, qname): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
654 return qname.namespace == self.uri |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
655 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
656 def __ne__(self, other): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
657 return not self == other |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
658 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
659 def __eq__(self, other): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
660 if isinstance(other, Namespace): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
661 return self.uri == other.uri |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
662 return self.uri == other |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
663 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
664 def __getitem__(self, name): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
665 return QName(self.uri + '}' + name) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
666 __getattr__ = __getitem__ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
667 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
668 def __hash__(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
669 return hash(self.uri) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
670 |
915
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
671 if sys.version_info[0] == 2: |
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
672 # Only use stringrepr in python 2 |
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
673 def __repr__(self): |
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
674 return '%s(%s)' % (type(self).__name__, stringrepr(self.uri)) |
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
675 else: |
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
676 def __repr__(self): |
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
677 return '%s(%r)' % (type(self).__name__, self.uri) |
860
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
678 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
679 def __str__(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
680 return self.uri.encode('utf-8') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
681 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
682 def __unicode__(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
683 return self.uri |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
684 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
685 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
686 # The namespace used by attributes such as xml:lang and xml:space |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
687 XML_NAMESPACE = Namespace('http://www.w3.org/XML/1998/namespace') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
688 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
689 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
690 class QName(unicode): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
691 """A qualified element or attribute name. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
692 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
693 The unicode value of instances of this class contains the qualified name of |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
694 the element or attribute, in the form ``{namespace-uri}local-name``. The |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
695 namespace URI can be obtained through the additional `namespace` attribute, |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
696 while the local name can be accessed through the `localname` attribute. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
697 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
698 >>> qname = QName('foo') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
699 >>> qname |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
700 QName('foo') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
701 >>> qname.localname |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
702 u'foo' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
703 >>> qname.namespace |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
704 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
705 >>> qname = QName('http://www.w3.org/1999/xhtml}body') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
706 >>> qname |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
707 QName('http://www.w3.org/1999/xhtml}body') |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
708 >>> qname.localname |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
709 u'body' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
710 >>> qname.namespace |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
711 u'http://www.w3.org/1999/xhtml' |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
712 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
713 __slots__ = ['namespace', 'localname'] |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
714 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
715 def __new__(cls, qname): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
716 """Create the `QName` instance. |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
717 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
718 :param qname: the qualified name as a string of the form |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
719 ``{namespace-uri}local-name``, where the leading curly |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
720 brace is optional |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
721 """ |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
722 if type(qname) is cls: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
723 return qname |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
724 |
920
f4bf11c716cc
Fix handling of QNames with leading open curly braces. See Genshi ticket #413.
hodgestar
parents:
915
diff
changeset
|
725 qname = qname.lstrip('{') |
f4bf11c716cc
Fix handling of QNames with leading open curly braces. See Genshi ticket #413.
hodgestar
parents:
915
diff
changeset
|
726 parts = qname.split('}', 1) |
860
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
727 if len(parts) > 1: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
728 self = unicode.__new__(cls, '{%s' % qname) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
729 self.namespace, self.localname = map(unicode, parts) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
730 else: |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
731 self = unicode.__new__(cls, qname) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
732 self.namespace, self.localname = None, unicode(qname) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
733 return self |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
734 |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
735 def __getnewargs__(self): |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
736 return (self.lstrip('{'),) |
61d37796da98
A bit of cleanup of the `Markup` Python implementation.
cmlenz
parents:
857
diff
changeset
|
737 |
915
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
738 if sys.version_info[0] == 2: |
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
739 # Only use stringrepr in python 2 |
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
740 def __repr__(self): |
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
741 return '%s(%s)' % (type(self).__name__, stringrepr(self.lstrip('{'))) |
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
742 else: |
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
743 def __repr__(self): |
9fafb35032a1
add support for python 3 to core genshi components (genshi.core, genshi.input and genshi.output):
hodgestar
parents:
904
diff
changeset
|
744 return '%s(%r)' % (type(self).__name__, self.lstrip('{')) |