~ubuntu-branches/ubuntu/raring/genshi/raring-proposed

« back to all changes in this revision

Viewing changes to doc/streams.txt

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Fontaine
  • Date: 2007-04-16 17:49:03 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070416174903-x2p3n9g890v18d0m
Tags: 0.4-1
* New upstream release.
* Remove useless python-markup transition package.
* Add Provides against python-markup.
* Add doc-base.
* Add depends against python-xml.
* Add suggests to python-setuptools.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 
9
9
 
10
10
.. contents:: Contents
11
 
   :depth: 2
 
11
   :depth: 1
12
12
.. sectnum::
13
13
 
14
14
 
18
18
A stream can be attained in a number of ways. It can be:
19
19
 
20
20
* the result of parsing XML or HTML text, or
21
 
* programmatically generated, or
22
 
* the result of selecting a subset of another stream filtered by an XPath
23
 
  expression.
 
21
* the result of selecting a subset of another stream using XPath, or
 
22
* programmatically generated.
24
23
 
25
24
For example, the functions ``XML()`` and ``HTML()`` can be used to convert
26
25
literal XML or HTML text to a markup stream::
30
29
  ...              '<a href="http://example.org/">a link</a>.'
31
30
  ...              '<br/></p>')
32
31
  >>> stream
33
 
  <genshi.core.Stream object at 0x6bef0>
 
32
  <genshi.core.Stream object at ...>
34
33
 
35
34
The stream is the result of parsing the text into events. Each event is a tuple
36
35
of the form ``(kind, data, pos)``, where:
38
37
* ``kind`` defines what kind of event it is (such as the start of an element,
39
38
  text, a comment, etc).
40
39
* ``data`` is the actual data associated with the event. How this looks depends
41
 
  on the event kind.
 
40
  on the event kind (see  `event kinds`_)
42
41
* ``pos`` is a ``(filename, lineno, column)`` tuple that describes where the
43
42
  event “comes from”.
44
43
 
47
46
  >>> for kind, data, pos in stream:
48
47
  ...     print kind, `data`, pos
49
48
  ... 
50
 
  START (u'p', [(u'class', u'intro')]) ('<string>', 1, 0)
51
 
  TEXT u'Some text and ' ('<string>', 1, 31)
52
 
  START (u'a', [(u'href', u'http://example.org/')]) ('<string>', 1, 31)
53
 
  TEXT u'a link' ('<string>', 1, 67)
54
 
  END u'a' ('<string>', 1, 67)
55
 
  TEXT u'.' ('<string>', 1, 72)
56
 
  START (u'br', []) ('<string>', 1, 72)
57
 
  END u'br' ('<string>', 1, 77)
58
 
  END u'p' ('<string>', 1, 77)
 
49
  START (QName(u'p'), Attrs([(QName(u'class'), u'intro')])) (None, 1, 0)
 
50
  TEXT u'Some text and ' (None, 1, 17)
 
51
  START (QName(u'a'), Attrs([(QName(u'href'), u'http://example.org/')])) (None, 1, 31)
 
52
  TEXT u'a link' (None, 1, 61)
 
53
  END QName(u'a') (None, 1, 67)
 
54
  TEXT u'.' (None, 1, 71)
 
55
  START (QName(u'br'), Attrs()) (None, 1, 72)
 
56
  END QName(u'br') (None, 1, 77)
 
57
  END QName(u'p') (None, 1, 77)
59
58
 
60
59
 
61
60
Filtering
91
90
``genshi.filters``. It processes a stream of HTML markup, and strips out any
92
91
potentially dangerous constructs, such as Javascript event handlers.
93
92
``HTMLSanitizer`` is not a function, but rather a class that implements
94
 
``__call__``, which means instances of the class are callable.
 
93
``__call__``, which means instances of the class are callable::
 
94
 
 
95
  stream = stream | HTMLSanitizer()
95
96
 
96
97
Both the ``filter()`` method and the pipe operator allow easy chaining of
97
98
filters::
103
104
 
104
105
  stream = stream | noop | HTMLSanitizer()
105
106
 
 
107
For more information about the built-in filters, see `Stream Filters`_.
 
108
 
 
109
.. _`Stream Filters`: filters.html
 
110
 
106
111
 
107
112
Serialization
108
113
=============
109
114
 
110
 
The ``Stream`` class provides two methods for serializing this list of events:
111
 
``serialize()`` and ``render()``. The former is a generator that yields chunks
112
 
of ``Markup`` objects (which are basically unicode strings that are considered
113
 
safe for output on the web). The latter returns a single string, by default
114
 
UTF-8 encoded.
 
115
Serialization means producing some kind of textual output from a stream of
 
116
events, which you'll need when you want to transmit or store the results of
 
117
generating or otherwise processing markup.
 
118
 
 
119
The ``Stream`` class provides two methods for serialization: ``serialize()`` and
 
120
``render()``. The former is a generator that yields chunks of ``Markup`` objects
 
121
(which are basically unicode strings that are considered safe for output on the
 
122
web). The latter returns a single string, by default UTF-8 encoded.
115
123
 
116
124
Here's the output from ``serialize()``::
117
125
 
150
158
 
151
159
  >>> from genshi.filters import HTMLSanitizer
152
160
  >>> from genshi.output import TextSerializer
153
 
  >>> print TextSerializer()(HTMLSanitizer()(stream))
 
161
  >>> print ''.join(TextSerializer()(HTMLSanitizer()(stream)))
154
162
  Some text and a link.
155
163
 
156
164
The pipe operator allows a nicer syntax::
158
166
  >>> print stream | HTMLSanitizer() | TextSerializer()
159
167
  Some text and a link.
160
168
 
 
169
 
 
170
Serialization Options
 
171
---------------------
 
172
 
 
173
Both ``serialize()`` and ``render()`` support additional keyword arguments that
 
174
are passed through to the initializer of the serializer class. The following
 
175
options are supported by the built-in serializers:
 
176
 
 
177
``strip_whitespace``
 
178
  Whether the serializer should remove trailing spaces and empty lines. Defaults
 
179
  to ``True``.
 
180
 
 
181
  (This option is not available for serialization to plain text.)
 
182
 
 
183
``doctype``
 
184
  A ``(name, pubid, sysid)`` tuple defining the name, publid identifier, and
 
185
  system identifier of a ``DOCTYPE`` declaration to prepend to the generated
 
186
  output. If provided, this declaration will override any ``DOCTYPE``
 
187
  declaration in the stream.
 
188
 
 
189
  (This option is not available for serialization to plain text.)
 
190
 
 
191
``namespace_prefixes``
 
192
  The namespace prefixes to use for namespace that are not bound to a prefix
 
193
  in the stream itself.
 
194
 
 
195
  (This option is not available for serialization to HTML or plain text.)
 
196
 
 
197
 
 
198
 
161
199
Using XPath
162
200
===========
163
201
 
166
204
 
167
205
  >>> substream = stream.select('a')
168
206
  >>> substream
169
 
  <genshi.core.Stream object at 0x7118b0>
 
207
  <genshi.core.Stream object at ...>
170
208
  >>> print substream
171
209
  <a href="http://example.org/">a link</a>
172
210
 
178
216
  >>> from genshi import Stream
179
217
  >>> substream = Stream(list(stream.select('a')))
180
218
  >>> substream
181
 
  <genshi.core.Stream object at 0x7118b0>
 
219
  <genshi.core.Stream object at ...>
182
220
  >>> print substream
183
221
  <a href="http://example.org/">a link</a>
184
222
  >>> print substream.select('@href')
185
223
  http://example.org/
186
224
  >>> print substream.select('text()')
187
225
  a link
 
226
 
 
227
See `Using XPath in Genshi`_ for more information about the XPath support in
 
228
Genshi.
 
229
 
 
230
.. _`Using XPath in Genshi`: xpath.html
 
231
 
 
232
 
 
233
.. _`event kinds`:
 
234
 
 
235
Event Kinds
 
236
===========
 
237
 
 
238
Every event in a stream is of one of several *kinds*, which also determines
 
239
what the ``data`` item of the event tuple looks like. The different kinds of
 
240
events are documented below.
 
241
 
 
242
.. note:: The ``data`` item is generally immutable. If the data is to be
 
243
   modified when processing a stream, it must be replaced by a new tuple.
 
244
   Effectively, this means the entire event tuple is immutable.
 
245
 
 
246
START
 
247
-----
 
248
The opening tag of an element.
 
249
 
 
250
For this kind of event, the ``data`` item is a tuple of the form
 
251
``(tagname, attrs)``, where ``tagname`` is a ``QName`` instance describing the
 
252
qualified name of the tag, and ``attrs`` is an ``Attrs`` instance containing
 
253
the attribute names and values associated with the tag (excluding namespace
 
254
declarations)::
 
255
 
 
256
  START, (QName(u'p'), Attrs([(u'class', u'intro')])), pos
 
257
 
 
258
END
 
259
---
 
260
The closing tag of an element.
 
261
 
 
262
The ``data`` item of end events consists of just a ``QName`` instance
 
263
describing the qualified name of the tag::
 
264
 
 
265
  END, QName(u'p'), pos
 
266
 
 
267
TEXT
 
268
----
 
269
Character data outside of elements and comments.
 
270
 
 
271
For text events, the ``data`` item should be a unicode object::
 
272
 
 
273
  TEXT, u'Hello, world!', pos
 
274
 
 
275
START_NS
 
276
--------
 
277
The start of a namespace mapping, binding a namespace prefix to a URI.
 
278
 
 
279
The ``data`` item of this kind of event is a tuple of the form
 
280
``(prefix, uri)``, where ``prefix`` is the namespace prefix and ``uri`` is the
 
281
full URI to which the prefix is bound. Both should be unicode objects. If the
 
282
namespace is not bound to any prefix, the ``prefix`` item is an empty string::
 
283
 
 
284
  START_NS, (u'svg', u'http://www.w3.org/2000/svg'), pos
 
285
 
 
286
END_NS
 
287
------
 
288
The end of a namespace mapping.
 
289
 
 
290
The ``data`` item of such events consists of only the namespace prefix (a
 
291
unicode object)::
 
292
 
 
293
  END_NS, u'svg', pos
 
294
 
 
295
DOCTYPE
 
296
-------
 
297
A document type declaration.
 
298
 
 
299
For this type of event, the ``data`` item is a tuple of the form
 
300
``(name, pubid, sysid)``, where ``name`` is the name of the root element,
 
301
``pubid`` is the public identifier of the DTD (or ``None``), and ``sysid`` is
 
302
the system identifier of the DTD (or ``None``)::
 
303
 
 
304
  DOCTYPE, (u'html', u'-//W3C//DTD XHTML 1.0 Transitional//EN', \
 
305
            u'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'), pos
 
306
 
 
307
COMMENT
 
308
-------
 
309
A comment.
 
310
 
 
311
For such events, the ``data`` item is a unicode object containing all character
 
312
data between the comment delimiters::
 
313
 
 
314
  COMMENT, u'Commented out', pos
 
315
 
 
316
PI
 
317
--
 
318
A processing instruction.
 
319
 
 
320
The ``data`` item is a tuple of the form ``(target, data)`` for processing
 
321
instructions, where ``target`` is the target of the PI (used to identify the
 
322
application by which the instruction should be processed), and ``data`` is text
 
323
following the target (excluding the terminating question mark)::
 
324
 
 
325
  PI, (u'php', u'echo "Yo" '), pos
 
326
 
 
327
START_CDATA
 
328
-----------
 
329
Marks the beginning of a ``CDATA`` section.
 
330
 
 
331
The ``data`` item for such events is always ``None``::
 
332
 
 
333
  START_CDATA, None, pos
 
334
 
 
335
END_CDATA
 
336
---------
 
337
Marks the end of a ``CDATA`` section.
 
338
 
 
339
The ``data`` item for such events is always ``None``::
 
340
 
 
341
  END_CDATA, None, pos