Package genshi :: Module output :: Class TextSerializer

Class TextSerializer

object --+
         |
        TextSerializer

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 genshi.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), that markup is by default passed through unchanged:

>>> elem = tag.div(Markup('<a href="foo">Hello &amp; Bye!</a><br/>'))
>>> print(elem.generate().render(TextSerializer, encoding=None))
<a href="foo">Hello &amp; Bye!</a><br/>

You can use the strip_markup to change this behavior, so that tags and entities are stripped from the output (or in the case of entities, replaced with the equivalent character):

>>> print(elem.generate().render(TextSerializer, strip_markup=True,
...                              encoding=None))
Hello & Bye!
Instance Methods
 
__init__(self, strip_markup=False)
Create the serializer.
 
__call__(self, stream)

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self, strip_markup=False)
(Constructor)

 
Create the serializer.
Parameters:
  • strip_markup - whether markup (tags and encoded characters) found in the text should be removed
Overrides: object.__init__