Ticket #168: substitute-preserve-Markup-r783.patch
| File substitute-preserve-Markup-r783.patch, 2.3 KB (added by cboos, 16 years ago) |
|---|
-
genshi/filters/tests/transform.py
19 19 20 20 def suite(): 21 21 from genshi.input import HTML 22 from genshi.core import Markup 23 from genshi.builder import tag 22 24 suite = doctest.DocTestSuite(genshi.filters.transform, 23 25 optionflags=doctest.NORMALIZE_WHITESPACE, 24 extraglobs={'HTML': HTML}) 26 extraglobs={'HTML': HTML, 'tag': tag, 27 'Markup': Markup}) 25 28 return suite 26 29 27 30 if __name__ == '__main__': -
genshi/filters/transform.py
51 51 import sys 52 52 53 53 from genshi.builder import Element 54 from genshi.core import Stream, Attrs, QName, TEXT, START, END, _ensure 54 from genshi.core import Stream, Attrs, QName, TEXT, START, END, _ensure, Markup 55 55 from genshi.path import Path 56 56 57 57 __all__ = ['Transformer', 'StreamBuffer', 'InjectorTransformation', 'ENTER', … … 549 549 ... '<b>some bold text</b></body></html>') 550 550 >>> print html | Transformer('body').substitute('(?i)some', 'SOME') 551 551 <html><body>SOME text, some more text and <b>SOME bold text</b></body></html> 552 >>> tags = tag.html(tag.body('Some text, some more text and ', 553 ... Markup('<b>some bold text</b>'))) 554 >>> print tags.generate() | Transformer('body').substitute('(?i)some', 'SOME') 555 <html><body>SOME text, some more text and <b>SOME bold text</b></body></html> 552 556 553 557 :param pattern: A regular expression object or string. 554 558 :param replace: Replacement pattern. … … 865 869 """ 866 870 for mark, (kind, data, pos) in stream: 867 871 if kind is TEXT: 868 data = self.pattern.sub(self.replace, data, self.count) 872 data1 = self.pattern.sub(self.replace, data, self.count) 873 data = isinstance(data, Markup) and Markup(data1) or data1 869 874 yield mark, (kind, data, pos) 870 875 871 876
