Ticket #41: text_serializer-r248.patch
| File text_serializer-r248.patch, 2.8 KB (added by cboos, 7 years ago) |
|---|
-
markup/core.py
16 16 import htmlentitydefs 17 17 import re 18 18 19 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'Namespace', 'QName'] 19 __all__ = ['Stream', 'Markup', 'escape', 'unescape', 'plaintext', 20 'Namespace', 'QName'] 20 21 21 22 22 23 class StreamEventKind(str): … … 81 82 """Return a string representation of the stream. 82 83 83 84 @param method: determines how the stream is serialized; can be either 84 "xml", "xhtml", or "html",or a custom `Serializer`85 "xml", "xhtml", "html", "text" or a custom `Serializer` 85 86 subclass 86 87 @param encoding: how the output string should be encoded; if set to 87 88 `None`, this method returns a `unicode` object … … 113 114 string. 114 115 115 116 @param method: determines how the stream is serialized; can be either 116 "xml", "xhtml", or "html", or a custom serializer class 117 "xml", "xhtml", "html", "text" or a custom serializer 118 class 117 119 118 120 Any additional keyword arguments are passed to the serializer, and thus 119 121 depend on the `method` parameter value. … … 123 125 if isinstance(method, basestring): 124 126 cls = {'xml': output.XMLSerializer, 125 127 'xhtml': output.XHTMLSerializer, 126 'html': output.HTMLSerializer}[method] 128 'html': output.HTMLSerializer, 129 'text': output.TextSerializer}[method] 127 130 serialize = cls(**kwargs) 128 131 return serialize(_ensure(self)) 129 132 -
markup/output.py
398 398 yield Markup('<?%s %s?>' % data) 399 399 400 400 401 class TextSerializer(XMLSerializer): 402 """Produces simple text (i.e. no markup, no entities) from an event stream. 403 404 >>> from markup.builder import tag 405 >>> elem = tag.div(tag.a('<Hello!>', href='foo'), tag.br) 406 >>> print ''.join(TextSerializer()(elem.generate())) 407 <Hello!> 408 """ 409 410 def __init__(self): 411 """Initialize the Text serializer.""" 412 XMLSerializer.__init__(self, doctype=None, strip_whitespace=False) 413 414 def __call__(self, stream): 415 for token in XMLSerializer.__call__(self, stream): 416 if isinstance(token, Markup): 417 yield token.striptags().stripentities() 418 else: 419 yield token # CDATA 420 421 401 422 class WhitespaceFilter(object): 402 423 """A filter that removes extraneous ignorable white space from the 403 424 stream."""
