# HG changeset patch # User cmlenz # Date 1195769235 0 # Node ID a445c9e5ee96265ca356879aa2d317bf02791577 # Parent 69b3b6ca968b931653ca1c3accaf10723d3c352f The `TextSerializer` class no longer strips all markup in text by default, so that it is still possible to use the Genshi `escape` function even with text templates. The old behavior is available via the `strip_markup` option of the serializer. Closes #146. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -29,10 +29,10 @@ * Text templates now default to rendering as plain text; it is no longer necessary to explicitly specify the "text" method to the `render()` or `serialize()` method of the generated markup stream. - * XInclude elements in markup templates now support the `parse` attribute; when - set to "xml" (the default), the include is processed as before, but when set - to "text", the included template is parsed as a text template using the new - syntax (ticket #101). + * XInclude elements in markup templates now support the `parse` attribute; + when set to "xml" (the default), the include is processed as before, but + when set to "text", the included template is parsed as a text template using + the new syntax (ticket #101). * Python code blocks inside match templates are now executed (ticket #155). * The template engine plugin no longer adds the `default_doctype` when the `fragment` parameter is `True`. @@ -40,6 +40,10 @@ #150). * The `py:replace` directive can now also be used as an element, with an attribute named `value` (ticket #144). + * The `TextSerializer` class no longer strips all markup in text by default, + so that it is still possible to use the Genshi `escape` function even with + text templates. The old behavior is available via the `strip_markup` option + of the serializer (ticket #146). Version 0.4.4 diff --git a/genshi/output.py b/genshi/output.py --- a/genshi/output.py +++ b/genshi/output.py @@ -436,20 +436,29 @@ If text events contain literal markup (instances of the `Markup` class), - tags or entities are stripped from the output: + that markup is by default passed through unchanged: - >>> elem = tag.div(Markup('Hello!
')) - >>> print elem -
Hello!
- >>> print ''.join(TextSerializer()(elem.generate())) - Hello! + >>> elem = tag.div(Markup('Hello & Bye!
')) + >>> print elem.generate().render(TextSerializer) + Hello & Bye!
+ + 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) + Hello & Bye! """ + def __init__(self, strip_markup=False): + self.strip_markup = strip_markup + def __call__(self, stream): + strip_markup = self.strip_markup for event in stream: if event[0] is TEXT: data = event[1] - if type(data) is Markup: + if strip_markup and type(data) is Markup: data = data.striptags().stripentities() yield unicode(data)