genshi.filters.transform

Package genshi :: Package filters :: Module transform |

# Module transform

A filter for functional-style transformations of markup streams.

The Transformer filter provides a variety of transformations that can be applied to parts of streams that match given XPath expressions. These transformations can be chained to achieve results that would be comparitively tedious to achieve by writing stream filters by hand. The approach of chaining node selection and transformation has been inspired by the jQuery Javascript library.

For example, the following transformation removes the `<title>` element from the `<head>` of the input document:

    >>> from genshi.builder import tag
    >>> html = HTML('''<html>
    ...  <head><title>Some Title</title></head>
    ...  <body>
    ...    Some <em>body</em> text.
    ...  </body>
    ... </html>''')
    >>> print html | Transformer('body/em').map(unicode.upper, TEXT) \
    ...                                    .unwrap().wrap(tag.u)
    <html>
      <head><title>Some Title</title></head>
      <body>
        Some <u>BODY</u> text.
      </body>
    </html>

The `Transformer` support a large number of useful transformations out of the box, but custom transformations can be added easily.

Since: version 0.5

Classes
| Transformer Stream filter that can apply a variety of different transformations to a stream.
| InjectorTransformation Abstract base class for transformations that inject content into a stream.
| StreamBuffer Stream event buffer used for cut and copy transformations.

Variables
| ENTER = `'ENTER'` Stream augmentation mark indicating that a selected element is being entered.
| INSIDE = `'INSIDE'` Stream augmentation mark indicating that processing is currently inside a selected element.
| OUTSIDE = `'OUTSIDE'` Stream augmentation mark indicating that a match occurred outside a selected element.
| EXIT = `'EXIT'` Stream augmentation mark indicating that a selected element is being exited.
| BREAK = `'BREAK'` Stream augmentation mark indicating a break between two otherwise contiguous blocks of marked events.

Variables Details

  ### BREAK Stream augmentation mark indicating a break between two otherwise contiguous blocks of marked events. This is used primarily by the cut() transform to provide later transforms with an opportunity to operate on the cut buffer. Value:
      'BREAK'