46
46
.. code-block:: pycon
48
48
>>> for kind, data, pos in stream:
49
... print kind, `data`, pos
49
... print('%s %r %r' % (kind, data, pos))
51
START (QName(u'p'), Attrs([(QName(u'class'), u'intro')])) (None, 1, 0)
51
START (QName('p'), Attrs([(QName('class'), u'intro')])) (None, 1, 0)
52
52
TEXT u'Some text and ' (None, 1, 17)
53
START (QName(u'a'), Attrs([(QName(u'href'), u'http://example.org/')])) (None, 1, 31)
53
START (QName('a'), Attrs([(QName('href'), u'http://example.org/')])) (None, 1, 31)
54
54
TEXT u'a link' (None, 1, 61)
55
END QName(u'a') (None, 1, 67)
55
END QName('a') (None, 1, 67)
56
56
TEXT u'.' (None, 1, 71)
57
START (QName(u'br'), Attrs()) (None, 1, 72)
58
END QName(u'br') (None, 1, 77)
59
END QName(u'p') (None, 1, 77)
57
START (QName('br'), Attrs()) (None, 1, 72)
58
END QName('br') (None, 1, 77)
59
END QName('p') (None, 1, 77)
159
159
.. code-block:: pycon
161
>>> print stream.render()
161
>>> print(stream.render())
162
162
<p class="intro">Some text and <a href="http://example.org/">a link</a>.<br/></p>
164
164
Both methods can be passed a ``method`` parameter that determines how exactly
168
168
.. code-block:: pycon
170
>>> print stream.render('html')
170
>>> print(stream.render('html'))
171
171
<p class="intro">Some text and <a href="http://example.org/">a link</a>.<br></p>
173
173
Note how the `<br>` element isn't closed, which is the right thing to do for
184
184
>>> from genshi.filters import HTMLSanitizer
185
185
>>> from genshi.output import TextSerializer
186
>>> print ''.join(TextSerializer()(HTMLSanitizer()(stream)))
186
>>> print(''.join(TextSerializer()(HTMLSanitizer()(stream))))
187
187
Some text and a link.
189
189
The pipe operator allows a nicer syntax:
191
191
.. code-block:: pycon
193
>>> print stream | HTMLSanitizer() | TextSerializer()
193
>>> print(stream | HTMLSanitizer() | TextSerializer())
194
194
Some text and a link.
327
327
>>> substream = stream.select('a')
329
329
<genshi.core.Stream object at ...>
331
331
<a href="http://example.org/">a link</a>
333
333
Often, streams cannot be reused: in the above example, the sub-stream is based
341
341
>>> substream = Stream(list(stream.select('a')))
343
343
<genshi.core.Stream object at ...>
345
345
<a href="http://example.org/">a link</a>
346
>>> print substream.select('@href')
346
>>> print(substream.select('@href'))
347
347
http://example.org/
348
>>> print substream.select('text()')
348
>>> print(substream.select('text()'))
351
351
See `Using XPath in Genshi`_ for more information about the XPath support in
380
380
.. code-block:: python
382
START, (QName(u'p'), Attrs([(QName(u'class'), u'intro')])), pos
382
START, (QName('p'), Attrs([(QName('class'), u'intro')])), pos