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 | |||
|
|||
|
|||
Stream |
|
||
Stream |
|
||
basestring
|
|
||
Stream |
|
||
iterator |
|
||
|
|||
|
|||
|
|||
Inherited from |
Instance Variables | |
events The underlying iterable producing the events |
|
serializer The default serializion method |
Properties | |
Inherited from |
Method Details |
Note: Changed in 0.5: added the serializer argument |
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, XHTMLSerializer, HTMLSerializer, TextSerializer Note:
Changed in 0.5: added the |
Return a new stream that contains the events matching the given XPath expression. >>> from genshi import HTML >>> stream = HTML('<doc><elem>foo</elem><elem>bar</elem></doc>') >>> print(stream.select('elem')) <elem>foo</elem><elem>bar</elem> >>> print(stream.select('elem/text()')) foobar Note that the outermost element of the stream becomes the context node for the XPath test. That means that the expression "doc" would not match anything in the example above, because it only tests against child elements of the outermost element: >>> print(stream.select('doc')) <BLANKLINE> You can use the "." expression to match the context node itself (although that usually makes little sense): >>> print(stream.select('.')) <doc><elem>foo</elem><elem>bar</elem></doc>
|
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, XHTMLSerializer, HTMLSerializer, TextSerializer |
|
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Thu Apr 22 14:10:36 2010 | http://epydoc.sourceforge.net |