changeset 728:c7badaa0be0c trunk

Fixed overly greedy `substitute` transformation. Reported in #226, thanks to jhammel for the ticket and patch!
author athomas
date Tue, 20 May 2008 00:26:50 +0000
parents 9e466c16f40b
children be0b4a7b2fd4
files genshi/filters/transform.py
diffstat 1 files changed, 11 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/genshi/filters/transform.py
+++ b/genshi/filters/transform.py
@@ -546,13 +546,17 @@
         Refer to the documentation for ``re.sub()`` for details.
 
         >>> html = HTML('<html><body>Some text, some more text and '
-        ...             '<b>some bold text</b></body></html>')
-        >>> print html | Transformer('body').substitute('(?i)some', 'SOME')
-        <html><body>SOME text, some more text and <b>SOME bold text</b></body></html>
-        >>> tags = tag.html(tag.body('Some text, some more text and ',
+        ...             '<b>some bold text</b>\\n'
+        ...             '<i>some italicised text</i></body></html>')
+        >>> print html | Transformer('body/b').substitute('(?i)some', 'SOME')
+        <html><body>Some text, some more text and <b>SOME bold text</b>
+        <i>some italicised text</i></body></html>
+        >>> tags = tag.html(tag.body('Some text, some more text and\\n',
         ...      Markup('<b>some bold text</b>')))
-        >>> print tags.generate() | Transformer('body').substitute('(?i)some', 'SOME')
-        <html><body>SOME text, some more text and <b>SOME bold text</b></body></html>
+        >>> print tags.generate() | Transformer('body').substitute(
+        ...     '(?i)some', 'SOME')
+        <html><body>SOME text, some more text and
+        <b>SOME bold text</b></body></html>
 
         :param pattern: A regular expression object or string.
         :param replace: Replacement pattern.
@@ -868,7 +872,7 @@
         :param stream: The marked event stream to filter
         """
         for mark, (kind, data, pos) in stream:
-            if kind is TEXT:
+            if mark is not None and kind is TEXT:
                 new_data = self.pattern.sub(self.replace, data, self.count)
                 if isinstance(data, Markup):
                     data = Markup(new_data)
Copyright (C) 2012-2017 Edgewall Software