~jtaylor/ubuntu/oneiric/genshi/dh_python2

« back to all changes in this revision

Viewing changes to doc/streams.txt

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2010-05-05 02:05:51 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100505020551-zmiep9cjs3s5c093
Tags: 0.6-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - debian/rules: Enable testsuite.
  - Update maintainer according to specification.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
.. code-block:: pycon
47
47
 
48
48
  >>> for kind, data, pos in stream:
49
 
  ...     print kind, `data`, pos
 
49
  ...     print('%s %r %r' % (kind, data, pos))
50
50
  ... 
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)
60
60
 
61
61
 
62
62
Filtering
143
143
.. code-block:: pycon
144
144
 
145
145
  >>> for output in stream.serialize():
146
 
  ...     print `output`
 
146
  ...     print(repr(output))
147
147
  ... 
148
148
  <Markup u'<p class="intro">'>
149
149
  <Markup u'Some text and '>
158
158
 
159
159
.. code-block:: pycon
160
160
 
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>
163
163
 
164
164
Both methods can be passed a ``method`` parameter that determines how exactly
167
167
 
168
168
.. code-block:: pycon
169
169
 
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>
172
172
 
173
173
Note how the `<br>` element isn't closed, which is the right thing to do for
183
183
 
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.
188
188
 
189
189
The pipe operator allows a nicer syntax:
190
190
 
191
191
.. code-block:: pycon
192
192
 
193
 
  >>> print stream | HTMLSanitizer() | TextSerializer()
 
193
  >>> print(stream | HTMLSanitizer() | TextSerializer())
194
194
  Some text and a link.
195
195
 
196
196
 
327
327
  >>> substream = stream.select('a')
328
328
  >>> substream
329
329
  <genshi.core.Stream object at ...>
330
 
  >>> print substream
 
330
  >>> print(substream)
331
331
  <a href="http://example.org/">a link</a>
332
332
 
333
333
Often, streams cannot be reused: in the above example, the sub-stream is based
341
341
  >>> substream = Stream(list(stream.select('a')))
342
342
  >>> substream
343
343
  <genshi.core.Stream object at ...>
344
 
  >>> print substream
 
344
  >>> print(substream)
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()'))
349
349
  a link
350
350
 
351
351
See `Using XPath in Genshi`_ for more information about the XPath support in
379
379
 
380
380
.. code-block:: python
381
381
 
382
 
  START, (QName(u'p'), Attrs([(QName(u'class'), u'intro')])), pos
 
382
  START, (QName('p'), Attrs([(QName('class'), u'intro')])), pos
383
383
 
384
384
END
385
385
---
390
390
 
391
391
.. code-block:: python
392
392
 
393
 
  END, QName(u'p'), pos
 
393
  END, QName('p'), pos
394
394
 
395
395
TEXT
396
396
----