Mercurial > genshi > genshi-test
comparison genshi/output.py @ 658:a445c9e5ee96
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.
author | cmlenz |
---|---|
date | Thu, 22 Nov 2007 22:07:15 +0000 |
parents | 49848aaa7839 |
children | 641d76ae430c |
comparison
equal
deleted
inserted
replaced
657:69b3b6ca968b | 658:a445c9e5ee96 |
---|---|
434 <div><a href="foo"><Hello!></a><br/></div> | 434 <div><a href="foo"><Hello!></a><br/></div> |
435 >>> print ''.join(TextSerializer()(elem.generate())) | 435 >>> print ''.join(TextSerializer()(elem.generate())) |
436 <Hello!> | 436 <Hello!> |
437 | 437 |
438 If text events contain literal markup (instances of the `Markup` class), | 438 If text events contain literal markup (instances of the `Markup` class), |
439 tags or entities are stripped from the output: | 439 that markup is by default passed through unchanged: |
440 | 440 |
441 >>> elem = tag.div(Markup('<a href="foo">Hello!</a><br/>')) | 441 >>> elem = tag.div(Markup('<a href="foo">Hello & Bye!</a><br/>')) |
442 >>> print elem | 442 >>> print elem.generate().render(TextSerializer) |
443 <div><a href="foo">Hello!</a><br/></div> | 443 <a href="foo">Hello & Bye!</a><br/> |
444 >>> print ''.join(TextSerializer()(elem.generate())) | 444 |
445 Hello! | 445 You can use the `strip_markup` to change this behavior, so that tags and |
446 """ | 446 entities are stripped from the output (or in the case of entities, |
447 replaced with the equivalent character): | |
448 | |
449 >>> print elem.generate().render(TextSerializer, strip_markup=True) | |
450 Hello & Bye! | |
451 """ | |
452 | |
453 def __init__(self, strip_markup=False): | |
454 self.strip_markup = strip_markup | |
447 | 455 |
448 def __call__(self, stream): | 456 def __call__(self, stream): |
457 strip_markup = self.strip_markup | |
449 for event in stream: | 458 for event in stream: |
450 if event[0] is TEXT: | 459 if event[0] is TEXT: |
451 data = event[1] | 460 data = event[1] |
452 if type(data) is Markup: | 461 if strip_markup and type(data) is Markup: |
453 data = data.striptags().stripentities() | 462 data = data.striptags().stripentities() |
454 yield unicode(data) | 463 yield unicode(data) |
455 | 464 |
456 | 465 |
457 class EmptyTagFilter(object): | 466 class EmptyTagFilter(object): |