Mercurial > genshi > mirror
comparison markup/core.py @ 27:b4f78c05e5c9 trunk
* Fix the boilerplate in the Python source files.
* Some more docstrings and cosmetic fixes.
author | cmlenz |
---|---|
date | Wed, 28 Jun 2006 09:28:09 +0000 |
parents | 5420cfe42d36 |
children | 3421dd98f015 |
comparison
equal
deleted
inserted
replaced
26:3c1a022be04c | 27:b4f78c05e5c9 |
---|---|
3 # Copyright (C) 2006 Christopher Lenz | 3 # Copyright (C) 2006 Christopher Lenz |
4 # All rights reserved. | 4 # All rights reserved. |
5 # | 5 # |
6 # This software is licensed as described in the file COPYING, which | 6 # This software is licensed as described in the file COPYING, which |
7 # you should have received as part of this distribution. The terms | 7 # you should have received as part of this distribution. The terms |
8 # are also available at http://trac.edgewall.com/license.html. | 8 # are also available at http://markup.cmlenz.net/wiki/License. |
9 # | 9 # |
10 # This software consists of voluntary contributions made by many | 10 # This software consists of voluntary contributions made by many |
11 # individuals. For the exact contribution history, see the revision | 11 # individuals. For the exact contribution history, see the revision |
12 # history and logs, available at http://projects.edgewall.com/trac/. | 12 # history and logs, available at http://markup.cmlenz.net/log/. |
13 | 13 |
14 """Core classes for markup processing.""" | 14 """Core classes for markup processing.""" |
15 | 15 |
16 import htmlentitydefs | 16 import htmlentitydefs |
17 import re | 17 import re |
55 COMMENT = StreamEventKind('COMMENT') # comment | 55 COMMENT = StreamEventKind('COMMENT') # comment |
56 | 56 |
57 def __init__(self, events): | 57 def __init__(self, events): |
58 """Initialize the stream with a sequence of markup events. | 58 """Initialize the stream with a sequence of markup events. |
59 | 59 |
60 @oaram events: a sequence or iterable providing the events | 60 @param events: a sequence or iterable providing the events |
61 """ | 61 """ |
62 self.events = events | 62 self.events = events |
63 | 63 |
64 def __iter__(self): | 64 def __iter__(self): |
65 return iter(self.events) | 65 return iter(self.events) |
109 cls = method | 109 cls = method |
110 if isinstance(method, basestring): | 110 if isinstance(method, basestring): |
111 cls = {'xml': output.XMLSerializer, | 111 cls = {'xml': output.XMLSerializer, |
112 'html': output.HTMLSerializer}[method] | 112 'html': output.HTMLSerializer}[method] |
113 else: | 113 else: |
114 assert issubclass(cls, serializers.Serializer) | 114 assert issubclass(cls, output.Serializer) |
115 serializer = cls(**kwargs) | 115 serializer = cls(**kwargs) |
116 | 116 |
117 stream = self | 117 stream = self |
118 if filters is None: | 118 if filters is None: |
119 filters = [WhitespaceFilter()] | 119 filters = [WhitespaceFilter()] |
166 """Create the `Attributes` instance. | 166 """Create the `Attributes` instance. |
167 | 167 |
168 If the `attrib` parameter is provided, it is expected to be a sequence | 168 If the `attrib` parameter is provided, it is expected to be a sequence |
169 of `(name, value)` tuples. | 169 of `(name, value)` tuples. |
170 """ | 170 """ |
171 list.__init__(self, map(lambda (k, v): (QName(k), v), attrib or [])) | 171 if attrib is None: |
172 attrib = [] | |
173 list.__init__(self, [(QName(name), value) for name, value in attrib]) | |
172 | 174 |
173 def __contains__(self, name): | 175 def __contains__(self, name): |
174 """Return whether the list includes an attribute with the specified | 176 """Return whether the list includes an attribute with the specified |
175 name. | 177 name. |
176 """ | 178 """ |
177 return name in [attr for attr, value in self] | 179 return name in [attr for attr, _ in self] |
178 | 180 |
179 def get(self, name, default=None): | 181 def get(self, name, default=None): |
180 """Return the value of the attribute with the specified name, or the | 182 """Return the value of the attribute with the specified name, or the |
181 value of the `default` parameter if no such attribute is found. | 183 value of the `default` parameter if no such attribute is found. |
182 """ | 184 """ |
214 """Marks a string as being safe for inclusion in HTML/XML output without | 216 """Marks a string as being safe for inclusion in HTML/XML output without |
215 needing to be escaped. | 217 needing to be escaped. |
216 """ | 218 """ |
217 __slots__ = [] | 219 __slots__ = [] |
218 | 220 |
219 def __new__(self, text='', *args): | 221 def __new__(cls, text='', *args): |
220 if args: | 222 if args: |
221 text %= tuple([escape(arg) for arg in args]) | 223 text %= tuple([escape(arg) for arg in args]) |
222 return unicode.__new__(self, text) | 224 return unicode.__new__(cls, text) |
223 | 225 |
224 def __add__(self, other): | 226 def __add__(self, other): |
225 return Markup(unicode(self) + escape(other)) | 227 return Markup(unicode(self) + escape(other)) |
226 | 228 |
227 def __mod__(self, args): | 229 def __mod__(self, args): |
255 else: | 257 else: |
256 ref = int(ref, 10) | 258 ref = int(ref, 10) |
257 return unichr(ref) | 259 return unichr(ref) |
258 else: # character entity | 260 else: # character entity |
259 ref = match.group(2) | 261 ref = match.group(2) |
260 if keepxmlentities and ref in ('amp', 'apos', 'gt', 'lt', 'quot'): | 262 if keepxmlentities and ref in ('amp', 'apos', 'gt', 'lt', |
263 'quot'): | |
261 return '&%s;' % ref | 264 return '&%s;' % ref |
262 try: | 265 try: |
263 codepoint = htmlentitydefs.name2codepoint[ref] | 266 codepoint = htmlentitydefs.name2codepoint[ref] |
264 return unichr(codepoint) | 267 return unichr(codepoint) |
265 except KeyError: | 268 except KeyError: |