diff genshi/filters/transform.py @ 902:09cc3627654c experimental-inline

Sync `experimental/inline` branch with [source:trunk@1126].
author cmlenz
date Fri, 23 Apr 2010 21:08:26 +0000
parents 1837f39efd6f
children
line wrap: on
line diff
--- a/genshi/filters/transform.py
+++ b/genshi/filters/transform.py
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
-# Copyright (C) 2007 Edgewall Software
+# Copyright (C) 2007-2009 Edgewall Software
 # All rights reserved.
 #
 # This software is licensed as described in the file COPYING, which
@@ -32,8 +32,8 @@
 ...    Some <em>body</em> text.
 ...  </body>
 ... </html>''')
->>> print html | Transformer('body/em').map(unicode.upper, TEXT) \\
-...                                    .unwrap().wrap(tag.u)
+>>> print(html | Transformer('body/em').map(unicode.upper, TEXT)
+...                                    .unwrap().wrap(tag.u))
 <html>
   <head><title>Some Title</title></head>
   <body>
@@ -142,7 +142,7 @@
     Here's an example of removing some markup (the title, in this case)
     selected by an expression:
 
-    >>> print html | Transformer('head/title').remove()
+    >>> print(html | Transformer('head/title').remove())
     <html><head/><body>Some <em>body</em> text.</body></html>
 
     Inserted content can be passed in the form of a string, or a markup event
@@ -150,7 +150,7 @@
     `builder` module:
 
     >>> from genshi.builder import tag
-    >>> print html | Transformer('body').prepend(tag.h1('Document Title'))
+    >>> print(html | Transformer('body').prepend(tag.h1('Document Title')))
     <html><head><title>Some Title</title></head><body><h1>Document
     Title</h1>Some <em>body</em> text.</body></html>
 
@@ -160,8 +160,8 @@
     copied text into the body as ``<h1>`` enclosed text:
 
     >>> buffer = StreamBuffer()
-    >>> print html | Transformer('head/title/text()').copy(buffer) \\
-    ...     .end().select('body').prepend(tag.h1(buffer))
+    >>> print(html | Transformer('head/title/text()').copy(buffer)
+    ...     .end().select('body').prepend(tag.h1(buffer)))
     <html><head><title>Some Title</title></head><body><h1>Some Title</h1>Some
     <em>body</em> text.</body></html>
 
@@ -170,7 +170,7 @@
     transforms:
 
     >>> emphasis = Transformer('body//em').attr('class', 'emphasis')
-    >>> print html | emphasis
+    >>> print(html | emphasis)
     <html><head><title>Some Title</title></head><body>Some <em
     class="emphasis">body</em> text.</body></html>
     """
@@ -216,7 +216,7 @@
         ...         else:
         ...             yield mark, (kind, data, pos)
         >>> short_stream = HTML('<body>Some <em>test</em> text</body>')
-        >>> print short_stream | Transformer('.//em/text()').apply(upper)
+        >>> print(short_stream | Transformer('.//em/text()').apply(upper))
         <body>Some <em>TEST</em> text</body>
         """
         transformer = Transformer()
@@ -234,14 +234,14 @@
         selection.
 
         >>> html = HTML('<body>Some <em>test</em> text</body>')
-        >>> print html | Transformer().select('.//em').trace()
-        (None, ('START', (QName(u'body'), Attrs()), (None, 1, 0)))
+        >>> print(html | Transformer().select('.//em').trace())
+        (None, ('START', (QName('body'), Attrs()), (None, 1, 0)))
         (None, ('TEXT', u'Some ', (None, 1, 6)))
-        ('ENTER', ('START', (QName(u'em'), Attrs()), (None, 1, 11)))
+        ('ENTER', ('START', (QName('em'), Attrs()), (None, 1, 11)))
         ('INSIDE', ('TEXT', u'test', (None, 1, 15)))
-        ('EXIT', ('END', QName(u'em'), (None, 1, 19)))
+        ('EXIT', ('END', QName('em'), (None, 1, 19)))
         (None, ('TEXT', u' text', (None, 1, 24)))
-        (None, ('END', QName(u'body'), (None, 1, 29)))
+        (None, ('END', QName('body'), (None, 1, 29)))
         <body>Some <em>test</em> text</body>
 
         :param path: an XPath expression (as string) or a `Path` instance
@@ -258,14 +258,14 @@
         are converted to OUTSIDE marks.
 
         >>> html = HTML('<body>Some <em>test</em> text</body>')
-        >>> print html | Transformer('//em').invert().trace()
-        ('OUTSIDE', ('START', (QName(u'body'), Attrs()), (None, 1, 0)))
+        >>> print(html | Transformer('//em').invert().trace())
+        ('OUTSIDE', ('START', (QName('body'), Attrs()), (None, 1, 0)))
         ('OUTSIDE', ('TEXT', u'Some ', (None, 1, 6)))
-        (None, ('START', (QName(u'em'), Attrs()), (None, 1, 11)))
+        (None, ('START', (QName('em'), Attrs()), (None, 1, 11)))
         (None, ('TEXT', u'test', (None, 1, 15)))
-        (None, ('END', QName(u'em'), (None, 1, 19)))
+        (None, ('END', QName('em'), (None, 1, 19)))
         ('OUTSIDE', ('TEXT', u' text', (None, 1, 24)))
-        ('OUTSIDE', ('END', QName(u'body'), (None, 1, 29)))
+        ('OUTSIDE', ('END', QName('body'), (None, 1, 29)))
         <body>Some <em>test</em> text</body>
 
         :rtype: `Transformer`
@@ -278,14 +278,14 @@
         Example:
 
         >>> html = HTML('<body>Some <em>test</em> text</body>')
-        >>> print html | Transformer('//em').end().trace()
-        ('OUTSIDE', ('START', (QName(u'body'), Attrs()), (None, 1, 0)))
+        >>> print(html | Transformer('//em').end().trace())
+        ('OUTSIDE', ('START', (QName('body'), Attrs()), (None, 1, 0)))
         ('OUTSIDE', ('TEXT', u'Some ', (None, 1, 6)))
-        ('OUTSIDE', ('START', (QName(u'em'), Attrs()), (None, 1, 11)))
+        ('OUTSIDE', ('START', (QName('em'), Attrs()), (None, 1, 11)))
         ('OUTSIDE', ('TEXT', u'test', (None, 1, 15)))
-        ('OUTSIDE', ('END', QName(u'em'), (None, 1, 19)))
+        ('OUTSIDE', ('END', QName('em'), (None, 1, 19)))
         ('OUTSIDE', ('TEXT', u' text', (None, 1, 24)))
-        ('OUTSIDE', ('END', QName(u'body'), (None, 1, 29)))
+        ('OUTSIDE', ('END', QName('body'), (None, 1, 29)))
         <body>Some <em>test</em> text</body>
 
         :return: the stream augmented by transformation marks
@@ -302,7 +302,7 @@
 
         >>> html = HTML('<html><head><title>Some Title</title></head>'
         ...             '<body>Some <em>body</em> text.</body></html>')
-        >>> print html | Transformer('.//em').empty()
+        >>> print(html | Transformer('.//em').empty())
         <html><head><title>Some Title</title></head><body>Some <em/>
         text.</body></html>
 
@@ -317,7 +317,7 @@
 
         >>> html = HTML('<html><head><title>Some Title</title></head>'
         ...             '<body>Some <em>body</em> text.</body></html>')
-        >>> print html | Transformer('.//em').remove()
+        >>> print(html | Transformer('.//em').remove())
         <html><head><title>Some Title</title></head><body>Some
         text.</body></html>
 
@@ -334,7 +334,7 @@
 
         >>> html = HTML('<html><head><title>Some Title</title></head>'
         ...             '<body>Some <em>body</em> text.</body></html>')
-        >>> print html | Transformer('.//em').unwrap()
+        >>> print(html | Transformer('.//em').unwrap())
         <html><head><title>Some Title</title></head><body>Some body
         text.</body></html>
 
@@ -347,7 +347,7 @@
 
         >>> html = HTML('<html><head><title>Some Title</title></head>'
         ...             '<body>Some <em>body</em> text.</body></html>')
-        >>> print html | Transformer('.//em').wrap('strong')
+        >>> print(html | Transformer('.//em').wrap('strong'))
         <html><head><title>Some Title</title></head><body>Some
         <strong><em>body</em></strong> text.</body></html>
 
@@ -363,7 +363,7 @@
 
         >>> html = HTML('<html><head><title>Some Title</title></head>'
         ...             '<body>Some <em>body</em> text.</body></html>')
-        >>> print html | Transformer('.//title/text()').replace('New Title')
+        >>> print(html | Transformer('.//title/text()').replace('New Title'))
         <html><head><title>New Title</title></head><body>Some <em>body</em>
         text.</body></html>
 
@@ -381,7 +381,7 @@
 
         >>> html = HTML('<html><head><title>Some Title</title></head>'
         ...             '<body>Some <em>body</em> text.</body></html>')
-        >>> print html | Transformer('.//em').before('emphasised ')
+        >>> print(html | Transformer('.//em').before('emphasised '))
         <html><head><title>Some Title</title></head><body>Some emphasised
         <em>body</em> text.</body></html>
 
@@ -398,7 +398,7 @@
 
         >>> html = HTML('<html><head><title>Some Title</title></head>'
         ...             '<body>Some <em>body</em> text.</body></html>')
-        >>> print html | Transformer('.//em').after(' rock')
+        >>> print(html | Transformer('.//em').after(' rock'))
         <html><head><title>Some Title</title></head><body>Some <em>body</em>
         rock text.</body></html>
 
@@ -415,7 +415,7 @@
 
         >>> html = HTML('<html><head><title>Some Title</title></head>'
         ...             '<body>Some <em>body</em> text.</body></html>')
-        >>> print html | Transformer('.//body').prepend('Some new body text. ')
+        >>> print(html | Transformer('.//body').prepend('Some new body text. '))
         <html><head><title>Some Title</title></head><body>Some new body text.
         Some <em>body</em> text.</body></html>
 
@@ -430,7 +430,7 @@
 
         >>> html = HTML('<html><head><title>Some Title</title></head>'
         ...             '<body>Some <em>body</em> text.</body></html>')
-        >>> print html | Transformer('.//body').append(' Some new body text.')
+        >>> print(html | Transformer('.//body').append(' Some new body text.'))
         <html><head><title>Some Title</title></head><body>Some <em>body</em>
         text. Some new body text.</body></html>
 
@@ -451,13 +451,13 @@
         >>> html = HTML('<html><head><title>Some Title</title></head>'
         ...             '<body>Some <em class="before">body</em> <em>text</em>.</body>'
         ...             '</html>')
-        >>> print html | Transformer('body/em').attr('class', None)
+        >>> print(html | Transformer('body/em').attr('class', None))
         <html><head><title>Some Title</title></head><body>Some <em>body</em>
         <em>text</em>.</body></html>
 
         Otherwise the attribute will be set to `value`:
 
-        >>> print html | Transformer('body/em').attr('class', 'emphasis')
+        >>> print(html | Transformer('body/em').attr('class', 'emphasis'))
         <html><head><title>Some Title</title></head><body>Some <em
         class="emphasis">body</em> <em class="emphasis">text</em>.</body></html>
 
@@ -467,10 +467,10 @@
 
         >>> def print_attr(name, event):
         ...     attrs = event[1][1]
-        ...     print attrs
+        ...     print(attrs)
         ...     return attrs.get(name)
-        >>> print html | Transformer('body/em').attr('class', print_attr)
-        Attrs([(QName(u'class'), u'before')])
+        >>> print(html | Transformer('body/em').attr('class', print_attr))
+        Attrs([(QName('class'), u'before')])
         Attrs()
         <html><head><title>Some Title</title></head><body>Some <em
         class="before">body</em> <em>text</em>.</body></html>
@@ -494,20 +494,20 @@
         >>> buffer = StreamBuffer()
         >>> html = HTML('<html><head><title>Some Title</title></head>'
         ...             '<body>Some <em>body</em> text.</body></html>')
-        >>> print html | Transformer('head/title/text()').copy(buffer) \\
-        ...     .end().select('body').prepend(tag.h1(buffer))
+        >>> print(html | Transformer('head/title/text()').copy(buffer)
+        ...     .end().select('body').prepend(tag.h1(buffer)))
         <html><head><title>Some Title</title></head><body><h1>Some
         Title</h1>Some <em>body</em> text.</body></html>
 
         This example illustrates that only a single contiguous selection will
         be buffered:
 
-        >>> print html | Transformer('head/title/text()').copy(buffer) \\
-        ...     .end().select('body/em').copy(buffer).end().select('body') \\
-        ...     .prepend(tag.h1(buffer))
+        >>> print(html | Transformer('head/title/text()').copy(buffer)
+        ...     .end().select('body/em').copy(buffer).end().select('body')
+        ...     .prepend(tag.h1(buffer)))
         <html><head><title>Some Title</title></head><body><h1>Some
         Title</h1>Some <em>body</em> text.</body></html>
-        >>> print buffer
+        >>> print(buffer)
         <em>body</em>
 
         Element attributes can also be copied for later use:
@@ -518,9 +518,9 @@
         >>> buffer = StreamBuffer()
         >>> def apply_attr(name, entry):
         ...     return list(buffer)[0][1][1].get('class')
-        >>> print html | Transformer('body/em[@class]/@class').copy(buffer) \\
-        ...     .end().buffer().select('body/em[not(@class)]') \\
-        ...     .attr('class', apply_attr)
+        >>> print(html | Transformer('body/em[@class]/@class').copy(buffer)
+        ...     .end().buffer().select('body/em[not(@class)]')
+        ...     .attr('class', apply_attr))
         <html><head><title>Some Title</title></head><body><em
         class="before">Some</em> <em class="before">body</em><em
         class="before">text</em>.</body></html>
@@ -547,8 +547,8 @@
         >>> buffer = StreamBuffer()
         >>> html = HTML('<html><head><title>Some Title</title></head>'
         ...             '<body>Some <em>body</em> text.</body></html>')
-        >>> print html | Transformer('.//em/text()').cut(buffer) \\
-        ...     .end().select('.//em').after(tag.h1(buffer))
+        >>> print(html | Transformer('.//em/text()').cut(buffer)
+        ...     .end().select('.//em').after(tag.h1(buffer)))
         <html><head><title>Some Title</title></head><body>Some
         <em/><h1>body</h1> text.</body></html>
 
@@ -579,8 +579,8 @@
         >>> doc = HTML('<doc><notes></notes><body>Some <note>one</note> '
         ...            'text <note>two</note>.</body></doc>')
         >>> buffer = StreamBuffer()
-        >>> print doc | Transformer('body/note').cut(buffer, accumulate=True) \\
-        ...     .end().buffer().select('notes').prepend(buffer)
+        >>> print(doc | Transformer('body/note').cut(buffer, accumulate=True)
+        ...     .end().buffer().select('notes').prepend(buffer))
         <doc><notes><note>one</note><note>two</note></notes><body>Some  text
         .</body></doc>
 
@@ -596,7 +596,7 @@
         >>> from genshi.filters.html import HTMLSanitizer
         >>> html = HTML('<html><body>Some text<script>alert(document.cookie)'
         ...             '</script> and some more text</body></html>')
-        >>> print html | Transformer('body/*').filter(HTMLSanitizer())
+        >>> print(html | Transformer('body/*').filter(HTMLSanitizer()))
         <html><body>Some text and some more text</body></html>
 
         :param filter: The stream filter to apply.
@@ -610,7 +610,7 @@
 
         >>> html = HTML('<html><head><title>Some Title</title></head>'
         ...               '<body>Some <em>body</em> text.</body></html>')
-        >>> print html | Transformer('head/title').map(unicode.upper, TEXT)
+        >>> print(html | Transformer('head/title').map(unicode.upper, TEXT))
         <html><head><title>SOME TITLE</title></head><body>Some <em>body</em>
         text.</body></html>
 
@@ -628,13 +628,13 @@
         >>> html = HTML('<html><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')
+        >>> 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')
+        >>> print(tags.generate() | Transformer('body').substitute(
+        ...     '(?i)some', 'SOME'))
         <html><body>SOME text, some more text and
         <b>SOME bold text</b></body></html>
 
@@ -650,7 +650,7 @@
 
         >>> html = HTML('<html><body>Some text, some more text and '
         ...             '<b>some bold text</b></body></html>')
-        >>> print html | Transformer('body/b').rename('strong')
+        >>> print(html | Transformer('body/b').rename('strong'))
         <html><body>Some text, some more text and <strong>some bold text</strong></body></html>
         """
         return self.apply(RenameTransformation(name))
@@ -659,14 +659,14 @@
         """Print events as they pass through the transform.
 
         >>> html = HTML('<body>Some <em>test</em> text</body>')
-        >>> print html | Transformer('em').trace()
-        (None, ('START', (QName(u'body'), Attrs()), (None, 1, 0)))
+        >>> print(html | Transformer('em').trace())
+        (None, ('START', (QName('body'), Attrs()), (None, 1, 0)))
         (None, ('TEXT', u'Some ', (None, 1, 6)))
-        ('ENTER', ('START', (QName(u'em'), Attrs()), (None, 1, 11)))
+        ('ENTER', ('START', (QName('em'), Attrs()), (None, 1, 11)))
         ('INSIDE', ('TEXT', u'test', (None, 1, 15)))
-        ('EXIT', ('END', QName(u'em'), (None, 1, 19)))
+        ('EXIT', ('END', QName('em'), (None, 1, 19)))
         (None, ('TEXT', u' text', (None, 1, 24)))
-        (None, ('END', QName(u'body'), (None, 1, 29)))
+        (None, ('END', QName('body'), (None, 1, 29)))
         <body>Some <em>test</em> text</body>
 
         :param prefix: a string to prefix each event with in the output
@@ -876,7 +876,7 @@
         :param stream: the marked event stream to filter
         """
         for event in stream:
-            print>>self.fileobj, self.prefix + str(event)
+            self.fileobj.write('%s%s\n' % (self.prefix, event))
             yield event
 
 
@@ -1025,7 +1025,7 @@
     ...         for event in stream:
     ...             yield event
     >>> html = HTML('<body>Some <em>test</em> text</body>')
-    >>> print html | Transformer('.//em').apply(Top('Prefix '))
+    >>> print(html | Transformer('.//em').apply(Top('Prefix ')))
     Prefix <body>Some <em>test</em> text</body>
     """
     def __init__(self, content):
Copyright (C) 2012-2017 Edgewall Software