Logo Search packages:      
Sourcecode: calibre version File versions

calibre::utils::genshi::filters::transform::Transformer Class Reference

List of all members.

Detailed Description

Stream filter that can apply a variety of different transformations to
a stream.

This is achieved by selecting the events to be transformed using XPath,
then applying the transformations to the events matched by the path
expression. Each marked event is in the form (mark, (kind, data, pos)),
where mark can be any of `ENTER`, `INSIDE`, `EXIT`, `OUTSIDE`, or `None`.

The first three marks match `START` and `END` events, and any events
contained `INSIDE` any selected XML/HTML element. A non-element match
outside a `START`/`END` container (e.g. ``text()``) will yield an `OUTSIDE`

>>> html = HTML('<html><head><title>Some Title</title></head>'
...             '<body>Some <em>body</em> text.</body></html>')

Transformations act on selected stream events matching an XPath expression.
Here's an example of removing some markup (the title, in this case)
selected by an expression:

>>> print html | Transformer('head/title').remove()
<html><head/><body>Some <em>body</em> text.</body></html>

Inserted content can be passed in the form of a string, or a markup event
stream, which includes streams generated programmatically via the
`builder` module:

>>> from genshi.builder import tag
>>> print html | Transformer('body').prepend(tag.h1('Document Title'))
<html><head><title>Some Title</title></head><body><h1>Document
Title</h1>Some <em>body</em> text.</body></html>

Each XPath expression determines the set of tags that will be acted upon by
subsequent transformations. In this example we select the ``<title>`` text,
copy it into a buffer, then select the ``<body>`` element and paste the
copied text into the body as ``<h1>`` enclosed text:

>>> buffer = StreamBuffer()
>>> print html | Transformer('head/title/text()').copy(buffer) \\
...     .end().select('body').prepend(tag.h1(buffer))
<html><head><title>Some Title</title></head><body><h1>Some Title</h1>Some
<em>body</em> text.</body></html>

Transformations can also be assigned and reused, although care must be
taken when using buffers, to ensure that buffers are cleared between

>>> emphasis = Transformer('body//em').attr('class', 'emphasis')
>>> print html | emphasis
<html><head><title>Some Title</title></head><body>Some <em
class="emphasis">body</em> text.</body></html>

Definition at line 124 of file transform.py.

Public Member Functions

def __call__
def __init__
def after
def append
def apply
def attr
def before
def buffer
def copy
def cut
def empty
def end
def filter
def invert
def map
def prepend
def remove
def rename
def replace
def select
def substitute
def trace
def unwrap
def wrap

Public Attributes


Private Member Functions

def _mark
def _unmark

Static Private Attributes

list __slots__ = ['transforms']

The documentation for this class was generated from the following file:

Generated by  Doxygen 1.6.0   Back to index