comparison doc/streams.txt @ 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
comparison
equal deleted inserted replaced
830:de82830f8816 902:09cc3627654c
44 event “comes from”. 44 event “comes from”.
45 45
46 .. code-block:: pycon 46 .. code-block:: pycon
47 47
48 >>> for kind, data, pos in stream: 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 TEXT u'Some text and ' (None, 1, 17) 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 TEXT u'a link' (None, 1, 61) 54 TEXT u'a link' (None, 1, 61)
55 END QName(u'a') (None, 1, 67) 55 END QName('a') (None, 1, 67)
56 TEXT u'.' (None, 1, 71) 56 TEXT u'.' (None, 1, 71)
57 START (QName(u'br'), Attrs()) (None, 1, 72) 57 START (QName('br'), Attrs()) (None, 1, 72)
58 END QName(u'br') (None, 1, 77) 58 END QName('br') (None, 1, 77)
59 END QName(u'p') (None, 1, 77) 59 END QName('p') (None, 1, 77)
60 60
61 61
62 Filtering 62 Filtering
63 ========= 63 =========
64 64
141 Here's the output from ``serialize()``: 141 Here's the output from ``serialize()``:
142 142
143 .. code-block:: pycon 143 .. code-block:: pycon
144 144
145 >>> for output in stream.serialize(): 145 >>> for output in stream.serialize():
146 ... print `output` 146 ... print(repr(output))
147 ... 147 ...
148 <Markup u'<p class="intro">'> 148 <Markup u'<p class="intro">'>
149 <Markup u'Some text and '> 149 <Markup u'Some text and '>
150 <Markup u'<a href="http://example.org/">'> 150 <Markup u'<a href="http://example.org/">'>
151 <Markup u'a link'> 151 <Markup u'a link'>
156 156
157 And here's the output from ``render()``: 157 And here's the output from ``render()``:
158 158
159 .. code-block:: pycon 159 .. code-block:: pycon
160 160
161 >>> print stream.render() 161 >>> print(stream.render())
162 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br/></p> 162 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br/></p>
163 163
164 Both methods can be passed a ``method`` parameter that determines how exactly 164 Both methods can be passed a ``method`` parameter that determines how exactly
165 the events are serialized to text. This parameter can be either a string or a 165 the events are serialized to text. This parameter can be either a string or a
166 custom serializer class: 166 custom serializer class:
167 167
168 .. code-block:: pycon 168 .. code-block:: pycon
169 169
170 >>> print stream.render('html') 170 >>> print(stream.render('html'))
171 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br></p> 171 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br></p>
172 172
173 Note how the `<br>` element isn't closed, which is the right thing to do for 173 Note how the `<br>` element isn't closed, which is the right thing to do for
174 HTML. See `serialization methods`_ for more details. 174 HTML. See `serialization methods`_ for more details.
175 175
181 181
182 .. code-block:: pycon 182 .. code-block:: pycon
183 183
184 >>> from genshi.filters import HTMLSanitizer 184 >>> from genshi.filters import HTMLSanitizer
185 >>> from genshi.output import TextSerializer 185 >>> from genshi.output import TextSerializer
186 >>> print ''.join(TextSerializer()(HTMLSanitizer()(stream))) 186 >>> print(''.join(TextSerializer()(HTMLSanitizer()(stream))))
187 Some text and a link. 187 Some text and a link.
188 188
189 The pipe operator allows a nicer syntax: 189 The pipe operator allows a nicer syntax:
190 190
191 .. code-block:: pycon 191 .. code-block:: pycon
192 192
193 >>> print stream | HTMLSanitizer() | TextSerializer() 193 >>> print(stream | HTMLSanitizer() | TextSerializer())
194 Some text and a link. 194 Some text and a link.
195 195
196 196
197 .. _`serialization methods`: 197 .. _`serialization methods`:
198 198
325 .. code-block:: pycon 325 .. code-block:: pycon
326 326
327 >>> substream = stream.select('a') 327 >>> substream = stream.select('a')
328 >>> substream 328 >>> substream
329 <genshi.core.Stream object at ...> 329 <genshi.core.Stream object at ...>
330 >>> print substream 330 >>> print(substream)
331 <a href="http://example.org/">a link</a> 331 <a href="http://example.org/">a link</a>
332 332
333 Often, streams cannot be reused: in the above example, the sub-stream is based 333 Often, streams cannot be reused: in the above example, the sub-stream is based
334 on a generator. Once it has been serialized, it will have been fully consumed, 334 on a generator. Once it has been serialized, it will have been fully consumed,
335 and cannot be rendered again. To work around this, you can wrap such a stream 335 and cannot be rendered again. To work around this, you can wrap such a stream
339 339
340 >>> from genshi import Stream 340 >>> from genshi import Stream
341 >>> substream = Stream(list(stream.select('a'))) 341 >>> substream = Stream(list(stream.select('a')))
342 >>> substream 342 >>> substream
343 <genshi.core.Stream object at ...> 343 <genshi.core.Stream object at ...>
344 >>> print substream 344 >>> print(substream)
345 <a href="http://example.org/">a link</a> 345 <a href="http://example.org/">a link</a>
346 >>> print substream.select('@href') 346 >>> print(substream.select('@href'))
347 http://example.org/ 347 http://example.org/
348 >>> print substream.select('text()') 348 >>> print(substream.select('text()'))
349 a link 349 a link
350 350
351 See `Using XPath in Genshi`_ for more information about the XPath support in 351 See `Using XPath in Genshi`_ for more information about the XPath support in
352 Genshi. 352 Genshi.
353 353
377 the attribute names and values associated with the tag (excluding namespace 377 the attribute names and values associated with the tag (excluding namespace
378 declarations): 378 declarations):
379 379
380 .. code-block:: python 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 END 384 END
385 --- 385 ---
386 The closing tag of an element. 386 The closing tag of an element.
387 387
388 The ``data`` item of end events consists of just a ``QName`` instance 388 The ``data`` item of end events consists of just a ``QName`` instance
389 describing the qualified name of the tag: 389 describing the qualified name of the tag:
390 390
391 .. code-block:: python 391 .. code-block:: python
392 392
393 END, QName(u'p'), pos 393 END, QName('p'), pos
394 394
395 TEXT 395 TEXT
396 ---- 396 ----
397 Character data outside of elements and comments. 397 Character data outside of elements and comments.
398 398
Copyright (C) 2012-2017 Edgewall Software