Mercurial > genshi > mirror
annotate markup/core.py @ 14:c7d33e0c9839 trunk
The `<py:match>` directive now protects itself against simple infinite recursion (see MatchDirective), while still allowing recursion in general.
author | cmlenz |
---|---|
date | Tue, 13 Jun 2006 17:56:42 +0000 |
parents | f77f7a91aa46 |
children | 74cc70129d04 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
3 # Copyright (C) 2006 Christopher Lenz | |
4 # All rights reserved. | |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://trac.edgewall.com/license.html. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://projects.edgewall.com/trac/. | |
13 | |
14 """Core classes for markup processing.""" | |
15 | |
16 import htmlentitydefs | |
17 import re | |
18 from StringIO import StringIO | |
19 | |
20 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Namespace', 'QName'] | |
21 | |
22 | |
23 class StreamEventKind(object): | |
24 """A kind of event on an XML stream.""" | |
25 | |
26 __slots__ = ['name'] | |
27 | |
28 def __init__(self, name): | |
29 self.name = name | |
30 | |
31 def __repr__(self): | |
32 return self.name | |
33 | |
34 | |
35 class Stream(object): | |
36 """Represents a stream of markup events. | |
37 | |
38 This class is basically an iterator over the events. | |
39 | |
40 Also provided are ways to serialize the stream to text. The `serialize()` | |
41 method will return an iterator over generated strings, while `render()` | |
42 returns the complete generated text at once. Both accept various parameters | |
43 that impact the way the stream is serialized. | |
44 | |
45 Stream events are tuples of the form: | |
46 | |
47 (kind, data, position) | |
48 | |
49 where `kind` is the event kind (such as `START`, `END`, `TEXT`, etc), `data` | |
50 depends on the kind of event, and `position` is a `(line, offset)` tuple | |
51 that contains the location of the original element or text in the input. | |
52 """ | |
53 __slots__ = ['events'] | |
54 | |
55 START = StreamEventKind('start') # a start tag | |
56 END = StreamEventKind('end') # an end tag | |
57 TEXT = StreamEventKind('text') # literal text | |
58 PROLOG = StreamEventKind('prolog') # XML prolog | |
59 DOCTYPE = StreamEventKind('doctype') # doctype declaration | |
60 START_NS = StreamEventKind('start-ns') # start namespace mapping | |
61 END_NS = StreamEventKind('end-ns') # end namespace mapping | |
62 PI = StreamEventKind('pi') # processing instruction | |
63 COMMENT = StreamEventKind('comment') # comment | |
64 | |
65 def __init__(self, events): | |
66 """Initialize the stream with a sequence of markup events. | |
67 | |
68 @oaram events: a sequence or iterable providing the events | |
69 """ | |
70 self.events = events | |
71 | |
72 def __iter__(self): | |
73 return iter(self.events) | |
74 | |
75 def render(self, method='xml', encoding='utf-8', **kwargs): | |
76 """Return a string representation of the stream. | |
77 | |
78 @param method: determines how the stream is serialized; can be either | |
79 'xml' or 'html', or a custom `Serializer` subclass | |
80 @param encoding: how the output string should be encoded; if set to | |
81 `None`, this method returns a `unicode` object | |
82 | |
83 Any additional keyword arguments are passed to the serializer, and thus | |
84 depend on the `method` parameter value. | |
85 """ | |
8
3710e3d0d4a2
`Stream.render()` was masking `TypeError`s (fix based on suggestion by Matt Good).
cmlenz
parents:
6
diff
changeset
|
86 output = u''.join(list(self.serialize(method=method, **kwargs))) |
1 | 87 if encoding is not None: |
9
5dc4bfe67c20
Actually use the specified encoding in `Stream.render()`.
cmlenz
parents:
8
diff
changeset
|
88 return output.encode(encoding) |
8
3710e3d0d4a2
`Stream.render()` was masking `TypeError`s (fix based on suggestion by Matt Good).
cmlenz
parents:
6
diff
changeset
|
89 return output |
1 | 90 |
91 def select(self, path): | |
92 """Return a new stream that contains the events matching the given | |
93 XPath expression. | |
94 | |
95 @param path: a string containing the XPath expression | |
96 """ | |
97 from markup.path import Path | |
98 path = Path(path) | |
99 return path.select(self) | |
100 | |
101 def serialize(self, method='xml', **kwargs): | |
102 """Generate strings corresponding to a specific serialization of the | |
103 stream. | |
104 | |
105 Unlike the `render()` method, this method is a generator this returns | |
106 the serialized output incrementally, as opposed to returning a single | |
107 string. | |
108 | |
109 @param method: determines how the stream is serialized; can be either | |
110 'xml' or 'html', or a custom `Serializer` subclass | |
111 """ | |
112 from markup import output | |
113 cls = method | |
114 if isinstance(method, basestring): | |
115 cls = {'xml': output.XMLSerializer, | |
116 'html': output.HTMLSerializer}[method] | |
117 else: | |
118 assert issubclass(cls, serializers.Serializer) | |
119 serializer = cls(**kwargs) | |
120 return serializer.serialize(self) | |
121 | |
122 def __str__(self): | |
123 return self.render() | |
124 | |
125 def __unicode__(self): | |
126 return self.render(encoding=None) | |
127 | |
128 | |
129 class Attributes(list): | |
130 | |
131 def __init__(self, attrib=None): | |
132 list.__init__(self, map(lambda (k, v): (QName(k), v), attrib or [])) | |
133 | |
134 def __contains__(self, name): | |
135 return name in [attr for attr, value in self] | |
136 | |
137 def get(self, name, default=None): | |
138 for attr, value in self: | |
139 if attr == name: | |
140 return value | |
141 return default | |
142 | |
5
dbb08edbc615
Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents:
1
diff
changeset
|
143 def remove(self, name): |
dbb08edbc615
Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents:
1
diff
changeset
|
144 for idx, (attr, _) in enumerate(self): |
dbb08edbc615
Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents:
1
diff
changeset
|
145 if attr == name: |
dbb08edbc615
Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents:
1
diff
changeset
|
146 del self[idx] |
dbb08edbc615
Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents:
1
diff
changeset
|
147 break |
dbb08edbc615
Improved `py:attrs` directive so that it removes existing attributes if they evaluate to `None` (AFAICT matching Kid behavior).
cmlenz
parents:
1
diff
changeset
|
148 |
1 | 149 def set(self, name, value): |
150 for idx, (attr, _) in enumerate(self): | |
151 if attr == name: | |
152 self[idx] = (attr, value) | |
153 break | |
154 else: | |
155 self.append((QName(name), value)) | |
156 | |
157 | |
158 class Markup(unicode): | |
159 """Marks a string as being safe for inclusion in HTML/XML output without | |
160 needing to be escaped. | |
161 """ | |
162 def __new__(self, text='', *args): | |
163 if args: | |
164 text %= tuple([escape(arg) for arg in args]) | |
165 return unicode.__new__(self, text) | |
166 | |
167 def __add__(self, other): | |
168 return Markup(unicode(self) + Markup.escape(other)) | |
169 | |
170 def __mod__(self, args): | |
171 if not isinstance(args, (list, tuple)): | |
172 args = [args] | |
173 return Markup(unicode.__mod__(self, | |
174 tuple([escape(arg) for arg in args]))) | |
175 | |
176 def __mul__(self, num): | |
177 return Markup(unicode(self) * num) | |
178 | |
179 def join(self, seq): | |
180 return Markup(unicode(self).join([Markup.escape(item) for item in seq])) | |
181 | |
182 def stripentities(self, keepxmlentities=False): | |
183 """Return a copy of the text with any character or numeric entities | |
184 replaced by the equivalent UTF-8 characters. | |
185 | |
186 If the `keepxmlentities` parameter is provided and evaluates to `True`, | |
187 the core XML entities (&, ', >, < and "). | |
188 """ | |
189 def _replace_entity(match): | |
190 if match.group(1): # numeric entity | |
191 ref = match.group(1) | |
192 if ref.startswith('x'): | |
193 ref = int(ref[1:], 16) | |
194 else: | |
195 ref = int(ref, 10) | |
196 return unichr(ref) | |
197 else: # character entity | |
198 ref = match.group(2) | |
199 if keepxmlentities and ref in ('amp', 'apos', 'gt', 'lt', 'quot'): | |
200 return '&%s;' % ref | |
201 try: | |
202 codepoint = htmlentitydefs.name2codepoint[ref] | |
203 return unichr(codepoint) | |
204 except KeyError: | |
205 if keepxmlentities: | |
206 return '&%s;' % ref | |
207 else: | |
208 return ref | |
209 return Markup(re.sub(r'&(?:#((?:\d+)|(?:[xX][0-9a-fA-F]+));?|(\w+);)', | |
210 _replace_entity, self)) | |
211 | |
212 def striptags(self): | |
213 """Return a copy of the text with all XML/HTML tags removed.""" | |
214 return Markup(re.sub(r'<[^>]*?>', '', self)) | |
215 | |
216 def escape(cls, text, quotes=True): | |
217 """Create a Markup instance from a string and escape special characters | |
218 it may contain (<, >, & and \"). | |
219 | |
220 If the `quotes` parameter is set to `False`, the \" character is left | |
221 as is. Escaping quotes is generally only required for strings that are | |
222 to be used in attribute values. | |
223 """ | |
224 if isinstance(text, cls): | |
225 return text | |
226 text = unicode(text) | |
227 if not text: | |
228 return cls() | |
229 text = text.replace('&', '&') \ | |
230 .replace('<', '<') \ | |
231 .replace('>', '>') | |
232 if quotes: | |
233 text = text.replace('"', '"') | |
234 return cls(text) | |
235 escape = classmethod(escape) | |
236 | |
237 def unescape(self): | |
238 """Reverse-escapes &, <, > and \" and returns a `unicode` object.""" | |
239 if not self: | |
240 return '' | |
241 return unicode(self).replace('"', '"') \ | |
242 .replace('>', '>') \ | |
243 .replace('<', '<') \ | |
244 .replace('&', '&') | |
245 | |
246 def plaintext(self, keeplinebreaks=True): | |
6 | 247 """Returns the text as a `unicode` string with all entities and tags |
248 removed. | |
249 """ | |
1 | 250 text = unicode(self.striptags().stripentities()) |
251 if not keeplinebreaks: | |
252 text = text.replace('\n', ' ') | |
253 return text | |
254 | |
255 def sanitize(self): | |
256 from markup.filters import HTMLSanitizer | |
257 from markup.input import HTMLParser | |
258 sanitize = HTMLSanitizer() | |
259 text = self.stripentities(keepxmlentities=True) | |
260 return Stream(sanitize(HTMLParser(StringIO(text)), None)) | |
261 | |
262 | |
263 escape = Markup.escape | |
264 | |
265 def unescape(text): | |
266 """Reverse-escapes &, <, > and \" and returns a `unicode` object.""" | |
267 if not isinstance(text, Markup): | |
268 return text | |
269 return text.unescape() | |
270 | |
271 | |
272 class Namespace(object): | |
273 | |
274 def __init__(self, uri): | |
275 self.uri = uri | |
276 | |
277 def __getitem__(self, name): | |
278 return QName(self.uri + '}' + name) | |
279 | |
280 __getattr__ = __getitem__ | |
281 | |
282 def __repr__(self): | |
283 return '<Namespace "%s">' % self.uri | |
284 | |
285 def __str__(self): | |
286 return self.uri | |
287 | |
288 def __unicode__(self): | |
289 return unicode(self.uri) | |
290 | |
291 | |
292 class QName(unicode): | |
293 """A qualified element or attribute name. | |
294 | |
295 The unicode value of instances of this class contains the qualified name of | |
296 the element or attribute, in the form `{namespace}localname`. The namespace | |
297 URI can be obtained through the additional `namespace` attribute, while the | |
298 local name can be accessed through the `localname` attribute. | |
299 """ | |
300 __slots__ = ['namespace', 'localname'] | |
301 | |
302 def __new__(cls, qname): | |
303 if isinstance(qname, QName): | |
304 return qname | |
305 | |
306 parts = qname.split('}', 1) | |
307 if qname.find('}') > 0: | |
308 self = unicode.__new__(cls, '{' + qname) | |
309 self.namespace = parts[0] | |
310 self.localname = parts[1] | |
311 else: | |
312 self = unicode.__new__(cls, qname) | |
313 self.namespace = None | |
314 self.localname = qname | |
315 return self |