diff markup/output.py @ 200:5861f4446c26 trunk

Add serialization to plain text, based on cboos' patch. Closes #41.
author cmlenz
date Fri, 25 Aug 2006 11:14:04 +0000
parents ba7556e3a835
children c5e0a1c86173
line wrap: on
line diff
--- a/markup/output.py
+++ b/markup/output.py
@@ -26,7 +26,8 @@
 from markup.core import DOCTYPE, START, END, START_NS, TEXT, START_CDATA, \
                         END_CDATA, PI, COMMENT, XML_NAMESPACE
 
-__all__ = ['Serializer', 'XMLSerializer', 'HTMLSerializer']
+__all__ = ['DocType', 'XMLSerializer', 'XHTMLSerializer', 'HTMLSerializer',
+           'TextSerializer']
 
 
 class DocType(object):
@@ -398,6 +399,37 @@
                 yield Markup('<?%s %s?>' % data)
 
 
+class TextSerializer(object):
+    """Produces plain text from an event stream.
+    
+    Only text events are included in the output. Unlike the other serializer,
+    special XML characters are not escaped:
+    
+    >>> from markup.builder import tag
+    >>> elem = tag.div(tag.a('<Hello!>', href='foo'), tag.br)
+    >>> print elem
+    <div><a href="foo">&lt;Hello!&gt;</a><br/></div>
+    >>> print ''.join(TextSerializer()(elem.generate()))
+    <Hello!>
+
+    If text events contain literal markup (instances of the `Markup` class),
+    tags or entities are stripped from the output:
+    
+    >>> elem = tag.div(Markup('<a href="foo">Hello!</a><br/>'))
+    >>> print elem
+    <div><a href="foo">Hello!</a><br/></div>
+    >>> print ''.join(TextSerializer()(elem.generate()))
+    Hello!
+    """
+
+    def __call__(self, stream):
+        for kind, data, pos in stream:
+            if kind is TEXT:
+                if type(data) is Markup:
+                    data = data.striptags().stripentities()
+                yield data
+
+
 class WhitespaceFilter(object):
     """A filter that removes extraneous ignorable white space from the
     stream."""
Copyright (C) 2012-2017 Edgewall Software