Home | Trees | Indices | Help |
|
---|
|
object --+ | Stream
Represents a stream of markup events.
This class is basically an iterator over the events.
Stream events are tuples of the form:
(kind, data, position)
where kind is the event kind (such as START, END, TEXT, etc), data depends on the kind of event, and position is a (filename, line, offset) tuple that contains the location of the original element or text in the input. If the original location is unknown, position is (None, -1, -1).
Also provided are ways to serialize the stream to text. The serialize() method will return an iterator over generated strings, while render() returns the complete generated text at once. Both accept various parameters that impact the way the stream is serialized.
Instance Methods | |||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
Inherited from |
Properties | |
events | |
Inherited from |
Method Details |
|
Override the "bitwise or" operator to apply filters or serializers to the stream, providing a syntax similar to pipes on Unix shells. Assume the following stream produced by the HTML function: >>> from genshi.input import HTML >>> html = HTML('''<p onclick="alert('Whoa')">Hello, world!</p>''') >>> print html <p onclick="alert('Whoa')">Hello, world!</p> A filter such as the HTML sanitizer can be applied to that stream using the pipe notation as follows: >>> from genshi.filters import HTMLSanitizer >>> sanitizer = HTMLSanitizer() >>> print html | sanitizer <p>Hello, world!</p> Filters can be any function that accepts and produces a stream (where a stream is anything that iterates over events): >>> def uppercase(stream): ... for kind, data, pos in stream: ... if kind is TEXT: ... data = data.upper() ... yield kind, data, pos >>> print html | sanitizer | uppercase <p>HELLO, WORLD!</p> Serializers can also be used with this notation: >>> from genshi.output import TextSerializer >>> output = TextSerializer() >>> print html | sanitizer | uppercase | output HELLO, WORLD! Commonly, serializers should be used at the end of the "pipeline"; using them somewhere in the middle may produce unexpected results. |
Apply filters to the stream. This method returns a new stream with the given filters applied. The filters must be callables that accept the stream object as parameter, and return the filtered stream. The call: stream.filter(filter1, filter2) is equivalent to: stream | filter1 | filter2 |
Return a string representation of the stream. Any additional keyword arguments are passed to the serializer, and thus
depend on the
See Also: XMLSerializer.__init__, XHTMLSerializer.__init__, HTMLSerializer.__init__, TextSerializer.__init__ |
|
Generate strings corresponding to a specific serialization of the stream. Unlike the render() method, this method is a generator that returns the serialized output incrementally, as opposed to returning a single string. Any additional keyword arguments are passed to the serializer, and thus
depend on the
See Also: XMLSerializer.__init__, XHTMLSerializer.__init__, HTMLSerializer.__init__, TextSerializer.__init__ |
|
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0beta1 on Mon Apr 16 16:02:13 2007 | http://epydoc.sourceforge.net |