annotate genshi/filters/transform.py @ 518:97cdadc17e20

Attributes selected with an XPath are now returned as an `Attrs()` object in all cases. Incidentally this also required adding explicit type coercion to the parser in `genshi.path`.
author athomas
date Thu, 07 Jun 2007 13:52:13 +0000
parents c98e9dcbedbb
children 9f1d90d6abd4
rev   line source
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
2 #
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2006-2007 Edgewall Software
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
4 # All rights reserved.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
5 #
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
8 # are also available at http://genshi.edgewall.org/wiki/License.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
9 #
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://genshi.edgewall.org/log/.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
13
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
14 """A filter for functional-style transformations of markup streams.
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
15
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
16 The `Transformer` filter provides a variety of transformations that can be
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
17 applied to parts of streams that match given XPath expressions. These
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
18 transformations can be chained to achieve results that would be comparitively
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
19 tedious to achieve by writing stream filters by hand. The approach of chaining
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
20 node selection and transformation has been inspired by the `jQuery`_ Javascript
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
21 library.
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
22
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
23 .. _`jQuery`: http://jquery.com/
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
24
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
25 For example, the following transformation removes the ``<title>`` element from
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
26 the ``<head>`` of the input document:
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
27
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
28 >>> from genshi.builder import tag
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
29 >>> html = HTML('''<html>
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
30 ... <head><title>Some Title</title></head>
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
31 ... <body>
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
32 ... Some <em>body</em> text.
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
33 ... </body>
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
34 ... </html>''')
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
35 >>> print html | Transformer('body/em').apply(unicode.upper, TEXT) \\
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
36 ... .unwrap().wrap(tag.u)
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
37 <html>
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
38 <head><title>Some Title</title></head>
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
39 <body>
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
40 Some <u>BODY</u> text.
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
41 </body>
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
42 </html>
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
43
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
44 The ``Transformer`` support a large number of useful transformations out of the
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
45 box, but custom transformations can be added easily.
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
46 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
47
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
48 import sys
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
49
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
50 from genshi.builder import Element
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
51 from genshi.core import Stream, Attrs, QName, TEXT, START, END, _ensure
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
52 from genshi.path import Path
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
53
518
97cdadc17e20 Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents: 517
diff changeset
54 __all__ = ['Transformer', 'StreamBuffer', 'InjectorTransformation', 'ENTER',
97cdadc17e20 Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents: 517
diff changeset
55 'EXIT', 'INSIDE', 'OUTSIDE']
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
56
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
57
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
58 class TransformMark(str):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
59 """A mark on a transformation stream."""
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
60 __slots__ = []
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
61 _instances = {}
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
62
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
63 def __new__(cls, val):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
64 return cls._instances.setdefault(val, str.__new__(cls, val))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
65
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
66
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
67 ENTER = TransformMark('ENTER')
515
6983367c1c78 Clarify !TransformMark docstrings.
athomas
parents: 514
diff changeset
68 """Stream augmentation mark indicating that a selected element is being
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
69 entered."""
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
70
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
71 INSIDE = TransformMark('INSIDE')
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
72 """Stream augmentation mark indicating that processing is currently inside a
515
6983367c1c78 Clarify !TransformMark docstrings.
athomas
parents: 514
diff changeset
73 selected element."""
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
74
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
75 OUTSIDE = TransformMark('OUTSIDE')
515
6983367c1c78 Clarify !TransformMark docstrings.
athomas
parents: 514
diff changeset
76 """Stream augmentation mark indicating that a match occurred outside a selected
6983367c1c78 Clarify !TransformMark docstrings.
athomas
parents: 514
diff changeset
77 element."""
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
78
517
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
79 ATTRIBUTE = TransformMark('ATTRIBUTE')
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
80 """Stream augmentation mark indicating that a an element attribute was
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
81 selected."""
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
82
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
83 EXIT = TransformMark('EXIT')
515
6983367c1c78 Clarify !TransformMark docstrings.
athomas
parents: 514
diff changeset
84 """Stream augmentation mark indicating that a selected element is being
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
85 exited."""
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
86
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
87
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
88 class Transformer(object):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
89 """Stream filter that can apply a variety of different transformations to
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
90 a stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
91
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
92 This is achieved by selecting the events to be transformed using XPath,
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
93 then applying the transformations to the events matched by the path
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
94 expression. Each marked event is in the form (mark, (kind, data, pos)),
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
95 where mark can be any of `ENTER`, `INSIDE`, `EXIT`, `OUTSIDE`, or `None`.
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
96
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
97 The first three marks match `START` and `END` events, and any events
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
98 contained `INSIDE` any selected XML/HTML element. A non-element match
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
99 outside a `START`/`END` container (e.g. ``text()``) will yield an `OUTSIDE`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
100 mark.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
101
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
102 >>> html = HTML('<html><head><title>Some Title</title></head>'
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
103 ... '<body>Some <em>body</em> text.</body></html>')
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
104
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
105 Transformations act on selected stream events matching an XPath expression.
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
106 Here's an example of removing some markup (the title, in this case)
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
107 selected by an expression:
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
108
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
109 >>> print html | Transformer('head/title').remove()
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
110 <html><head/><body>Some <em>body</em> text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
111
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
112 Inserted content can be passed in the form of a string, or a markup event
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
113 stream, which includes streams generated programmatically via the
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
114 `builder` module:
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
115
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
116 >>> from genshi.builder import tag
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
117 >>> print html | Transformer('body').prepend(tag.h1('Document Title'))
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
118 <html><head><title>Some Title</title></head><body><h1>Document
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
119 Title</h1>Some <em>body</em> text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
120
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
121 Each XPath expression determines the set of tags that will be acted upon by
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
122 subsequent transformations. In this example we select the ``<title>`` text,
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
123 copy it into a buffer, then select the ``<body>`` element and paste the
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
124 copied text into the body as ``<h1>`` enclosed text:
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
125
506
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
126 >>> buffer = StreamBuffer()
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
127 >>> print html | Transformer('head/title/text()').copy(buffer) \\
514
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
128 ... .end().select('body').prepend(tag.h1(buffer))
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
129 <html><head><title>Some Title</title></head><body><h1>Some Title</h1>Some
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
130 <em>body</em> text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
131
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
132 Transformations can also be assigned and reused, although care must be
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
133 taken when using buffers, to ensure that buffers are cleared between
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
134 transforms:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
135
517
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
136 >>> emphasis = Transformer('body//em').attr('class', 'emphasis')
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
137 >>> print html | emphasis
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
138 <html><head><title>Some Title</title></head><body>Some <em
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
139 class="emphasis">body</em> text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
140 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
141
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
142 __slots__ = ['transforms']
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
143
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
144 def __init__(self, path=None):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
145 """Construct a new transformation filter.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
146
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
147 :param path: an XPath expression (as string) or a `Path` instance
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
148 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
149 self.transforms = []
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
150 if path:
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
151 self.transforms.append(SelectTransformation(path))
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
152
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
153 def __call__(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
154 """Apply the transform filter to the marked stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
155
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
156 :param stream: the marked event stream to filter
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
157 :return: the transformed stream
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
158 :rtype: `Stream`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
159 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
160 transforms = self._mark(stream)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
161 for link in self.transforms:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
162 transforms = link(transforms)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
163 return Stream(self._unmark(transforms))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
164
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
165 def __or__(self, function):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
166 """Combine transformations.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
167
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
168 Transformations can be chained, similar to stream filters. Any callable
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
169 accepting a marked stream can be used as a transform.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
170
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
171 As an example, here is a simple `TEXT` event upper-casing transform:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
172
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
173 >>> def upper(stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
174 ... for mark, (kind, data, pos) in stream:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
175 ... if mark and kind is TEXT:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
176 ... yield mark, (kind, data.upper(), pos)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
177 ... else:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
178 ... yield mark, (kind, data, pos)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
179 >>> short_stream = HTML('<body>Some <em>test</em> text</body>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
180 >>> print short_stream | (Transformer('.//em/text()') | upper)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
181 <body>Some <em>TEST</em> text</body>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
182 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
183 transformer = Transformer()
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
184 transformer.transforms = self.transforms[:]
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
185 if isinstance(function, Transformer):
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
186 transformer.transforms.extend(function.transforms)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
187 else:
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
188 transformer.transforms.append(function)
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
189 return transformer
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
190
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
191 #{ Selection operations
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
192
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
193 def select(self, path):
514
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
194 """Mark events matching the given XPath expression, within the current
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
195 selection.
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
196
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
197 >>> html = HTML('<body>Some <em>test</em> text</body>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
198 >>> print html | Transformer().select('.//em').trace()
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
199 (None, ('START', (QName(u'body'), Attrs()), (None, 1, 0)))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
200 (None, ('TEXT', u'Some ', (None, 1, 6)))
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
201 ('ENTER', ('START', (QName(u'em'), Attrs()), (None, 1, 11)))
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
202 ('INSIDE', ('TEXT', u'test', (None, 1, 15)))
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
203 ('EXIT', ('END', QName(u'em'), (None, 1, 19)))
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
204 (None, ('TEXT', u' text', (None, 1, 24)))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
205 (None, ('END', QName(u'body'), (None, 1, 29)))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
206 <body>Some <em>test</em> text</body>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
207
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
208 :param path: an XPath expression (as string) or a `Path` instance
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
209 :return: the stream augmented by transformation marks
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
210 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
211 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
212 return self | SelectTransformation(path)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
213
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
214 def invert(self):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
215 """Invert selection so that marked events become unmarked, and vice
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
216 versa.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
217
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
218 Specificaly, all marks are converted to null marks, and all null marks
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
219 are converted to OUTSIDE marks.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
220
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
221 >>> html = HTML('<body>Some <em>test</em> text</body>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
222 >>> print html | Transformer('//em').invert().trace()
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
223 ('OUTSIDE', ('START', (QName(u'body'), Attrs()), (None, 1, 0)))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
224 ('OUTSIDE', ('TEXT', u'Some ', (None, 1, 6)))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
225 (None, ('START', (QName(u'em'), Attrs()), (None, 1, 11)))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
226 (None, ('TEXT', u'test', (None, 1, 15)))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
227 (None, ('END', QName(u'em'), (None, 1, 19)))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
228 ('OUTSIDE', ('TEXT', u' text', (None, 1, 24)))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
229 ('OUTSIDE', ('END', QName(u'body'), (None, 1, 29)))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
230 <body>Some <em>test</em> text</body>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
231
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
232 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
233 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
234 return self | InvertTransformation()
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
235
514
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
236 def end(self):
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
237 """End current selection, allowing all events to be selected.
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
238
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
239 Example:
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
240
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
241 >>> html = HTML('<body>Some <em>test</em> text</body>')
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
242 >>> print html | Transformer('//em').end().trace()
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
243 ('OUTSIDE', ('START', (QName(u'body'), Attrs()), (None, 1, 0)))
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
244 ('OUTSIDE', ('TEXT', u'Some ', (None, 1, 6)))
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
245 ('OUTSIDE', ('START', (QName(u'em'), Attrs()), (None, 1, 11)))
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
246 ('OUTSIDE', ('TEXT', u'test', (None, 1, 15)))
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
247 ('OUTSIDE', ('END', QName(u'em'), (None, 1, 19)))
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
248 ('OUTSIDE', ('TEXT', u' text', (None, 1, 24)))
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
249 ('OUTSIDE', ('END', QName(u'body'), (None, 1, 29)))
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
250 <body>Some <em>test</em> text</body>
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
251
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
252 :return: the stream augmented by transformation marks
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
253 :rtype: `Transformer`
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
254 """
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
255 return self | EndTransformation()
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
256
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
257 #{ Deletion operations
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
258
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
259 def empty(self):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
260 """Empty selected elements of all content.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
261
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
262 Example:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
263
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
264 >>> html = HTML('<html><head><title>Some Title</title></head>'
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
265 ... '<body>Some <em>body</em> text.</body></html>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
266 >>> print html | Transformer('.//em').empty()
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
267 <html><head><title>Some Title</title></head><body>Some <em/>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
268 text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
269
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
270 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
271 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
272 return self | EmptyTransformation()
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
273
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
274 def remove(self):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
275 """Remove selection from the stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
276
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
277 Example:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
278
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
279 >>> html = HTML('<html><head><title>Some Title</title></head>'
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
280 ... '<body>Some <em>body</em> text.</body></html>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
281 >>> print html | Transformer('.//em').remove()
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
282 <html><head><title>Some Title</title></head><body>Some
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
283 text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
284
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
285 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
286 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
287 return self | RemoveTransformation()
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
288
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
289 #{ Direct element operations
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
290
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
291 def unwrap(self):
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
292 """Remove outermost enclosing elements from selection.
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
293
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
294 Example:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
295
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
296 >>> html = HTML('<html><head><title>Some Title</title></head>'
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
297 ... '<body>Some <em>body</em> text.</body></html>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
298 >>> print html | Transformer('.//em').unwrap()
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
299 <html><head><title>Some Title</title></head><body>Some body
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
300 text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
301
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
302 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
303 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
304 def _unwrap(stream):
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
305 for mark, event in stream:
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
306 if mark not in (ENTER, EXIT):
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
307 yield mark, event
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
308 return self | UnwrapTransformation()
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
309
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
310 def wrap(self, element):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
311 """Wrap selection in an element.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
312
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
313 >>> html = HTML('<html><head><title>Some Title</title></head>'
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
314 ... '<body>Some <em>body</em> text.</body></html>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
315 >>> print html | Transformer('.//em').wrap('strong')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
316 <html><head><title>Some Title</title></head><body>Some
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
317 <strong><em>body</em></strong> text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
318
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
319 :param element: either a tag name (as string) or an `Element` object
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
320 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
321 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
322 return self | WrapTransformation(element)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
323
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
324 #{ Content insertion operations
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
325
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
326 def replace(self, content):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
327 """Replace selection with content.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
328
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
329 >>> html = HTML('<html><head><title>Some Title</title></head>'
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
330 ... '<body>Some <em>body</em> text.</body></html>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
331 >>> print html | Transformer('.//title/text()').replace('New Title')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
332 <html><head><title>New Title</title></head><body>Some <em>body</em>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
333 text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
334
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
335 :param content: Either an iterable of events or a string to insert.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
336 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
337 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
338 return self | ReplaceTransformation(content)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
339
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
340 def before(self, content):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
341 """Insert content before selection.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
342
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
343 In this example we insert the word 'emphasised' before the <em> opening
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
344 tag:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
345
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
346 >>> html = HTML('<html><head><title>Some Title</title></head>'
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
347 ... '<body>Some <em>body</em> text.</body></html>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
348 >>> print html | Transformer('.//em').before('emphasised ')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
349 <html><head><title>Some Title</title></head><body>Some emphasised
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
350 <em>body</em> text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
351
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
352 :param content: Either an iterable of events or a string to insert.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
353 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
354 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
355 return self | BeforeTransformation(content)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
356
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
357 def after(self, content):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
358 """Insert content after selection.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
359
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
360 Here, we insert some text after the </em> closing tag:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
361
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
362 >>> html = HTML('<html><head><title>Some Title</title></head>'
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
363 ... '<body>Some <em>body</em> text.</body></html>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
364 >>> print html | Transformer('.//em').after(' rock')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
365 <html><head><title>Some Title</title></head><body>Some <em>body</em>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
366 rock text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
367
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
368 :param content: Either an iterable of events or a string to insert.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
369 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
370 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
371 return self | AfterTransformation(content)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
372
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
373 def prepend(self, content):
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
374 """Insert content after the ENTER event of the selection.
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
375
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
376 Inserting some new text at the start of the <body>:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
377
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
378 >>> html = HTML('<html><head><title>Some Title</title></head>'
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
379 ... '<body>Some <em>body</em> text.</body></html>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
380 >>> print html | Transformer('.//body').prepend('Some new body text. ')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
381 <html><head><title>Some Title</title></head><body>Some new body text.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
382 Some <em>body</em> text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
383
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
384 :param content: Either an iterable of events or a string to insert.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
385 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
386 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
387 return self | PrependTransformation(content)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
388
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
389 def append(self, content):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
390 """Insert content before the END event of the selection.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
391
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
392 >>> html = HTML('<html><head><title>Some Title</title></head>'
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
393 ... '<body>Some <em>body</em> text.</body></html>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
394 >>> print html | Transformer('.//body').append(' Some new body text.')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
395 <html><head><title>Some Title</title></head><body>Some <em>body</em>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
396 text. Some new body text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
397
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
398 :param content: Either an iterable of events or a string to insert.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
399 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
400 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
401 return self | AppendTransformation(content)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
402
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
403 #{ Attribute manipulation
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
404
517
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
405 def attr(self, name, value):
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
406 """Add, replace or delete an attribute on selected elements.
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
407
517
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
408 If `value` evaulates to `None` the attribute will be deleted from the
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
409 element:
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
410
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
411 >>> html = HTML('<html><head><title>Some Title</title></head>'
517
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
412 ... '<body>Some <em class="before">body</em> <em>text</em>.</body>'
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
413 ... '</html>')
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
414 >>> print html | Transformer('body/em').attr('class', None)
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
415 <html><head><title>Some Title</title></head><body>Some <em>body</em>
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
416 <em>text</em>.</body></html>
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
417
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
418 Otherwise the attribute will be set to `value`:
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
419
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
420 >>> print html | Transformer('body/em').attr('class', 'emphasis')
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
421 <html><head><title>Some Title</title></head><body>Some <em
517
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
422 class="emphasis">body</em> <em class="emphasis">text</em>.</body></html>
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
423
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
424 If `value` is a callable it will be called with the attribute name and
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
425 the `START` event for the matching element. Its return value will then
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
426 be used to set the attribute:
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
427
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
428 >>> def print_attr(name, event):
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
429 ... attrs = event[1][1]
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
430 ... print attrs
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
431 ... return attrs.get(name)
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
432 >>> print html | Transformer('body/em').attr('class', print_attr)
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
433 Attrs([(QName(u'class'), u'before')])
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
434 Attrs()
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
435 <html><head><title>Some Title</title></head><body>Some <em
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
436 class="before">body</em> <em>text</em>.</body></html>
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
437
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
438 :param name: the name of the attribute
517
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
439 :param value: the value that should be set for the attribute.
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
440 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
441 """
517
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
442 return self | AttrTransformation(name, value)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
443
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
444 #{ Buffer operations
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
445
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
446 def copy(self, buffer):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
447 """Copy selection into buffer.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
448
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
449 >>> from genshi.builder import tag
506
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
450 >>> buffer = StreamBuffer()
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
451 >>> html = HTML('<html><head><title>Some Title</title></head>'
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
452 ... '<body>Some <em>body</em> text.</body></html>')
509
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
453 >>> print html | Transformer('title/text()').copy(buffer) \\
514
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
454 ... .end().select('body').prepend(tag.h1(buffer))
509
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
455 <html><head><title>Some Title</title></head><body><h1>Some
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
456 Title</h1>Some <em>body</em> text.</body></html>
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
457
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
458 To ensure that a transformation can be reused deterministically, the
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
459 contents of ``buffer`` is replaced by the ``copy()`` operation:
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
460
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
461 >>> print buffer
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
462 Some Title
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
463 >>> print html | Transformer('head/title/text()').copy(buffer) \\
514
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
464 ... .end().select('body/em').copy(buffer).end().select('body') \\
509
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
465 ... .prepend(tag.h1(buffer))
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
466 <html><head><title>Some
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
467 Title</title></head><body><h1><em>body</em></h1>Some <em>body</em>
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
468 text.</body></html>
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
469 >>> print buffer
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
470 <em>body</em>
1997f7af845c Add a test for buffer reset by copy().
athomas
parents: 507
diff changeset
471
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
472
507
c006015dc52d Update API docs for [610].
cmlenz
parents: 506
diff changeset
473 :param buffer: the `StreamBuffer` in which the selection should be
c006015dc52d Update API docs for [610].
cmlenz
parents: 506
diff changeset
474 stored
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
475 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
476 :note: this transformation will buffer the entire input stream
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
477 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
478 return self | CopyTransformation(buffer)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
479
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
480 def cut(self, buffer):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
481 """Copy selection into buffer and remove the selection from the stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
482
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
483 >>> from genshi.builder import tag
506
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
484 >>> buffer = StreamBuffer()
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
485 >>> html = HTML('<html><head><title>Some Title</title></head>'
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
486 ... '<body>Some <em>body</em> text.</body></html>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
487 >>> print html | Transformer('.//em/text()').cut(buffer) \\
514
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
488 ... .end().select('.//em').after(tag.h1(buffer))
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
489 <html><head><title>Some Title</title></head><body>Some
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
490 <em/><h1>body</h1> text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
491
507
c006015dc52d Update API docs for [610].
cmlenz
parents: 506
diff changeset
492 :param buffer: the `StreamBuffer` in which the selection should be
c006015dc52d Update API docs for [610].
cmlenz
parents: 506
diff changeset
493 stored
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
494 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
495 :note: this transformation will buffer the entire input stream
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
496 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
497 return self | CutTransformation(buffer)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
498
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
499 #{ Miscellaneous operations
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
500
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
501 def apply(self, function, kind):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
502 """Apply a function to the ``data`` element of events of ``kind`` in
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
503 the selection.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
504
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
505 >>> html = HTML('<html><head><title>Some Title</title></head>'
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
506 ... '<body>Some <em>body</em> text.</body></html>')
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
507 >>> print html | Transformer('head/title').apply(unicode.upper, TEXT)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
508 <html><head><title>SOME TITLE</title></head><body>Some <em>body</em>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
509 text.</body></html>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
510
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
511 :param function: the function to apply
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
512 :param kind: the kind of event the function should be applied to
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
513 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
514 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
515 return self | ApplyTransformation(function, kind)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
516
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
517 def trace(self, prefix='', fileobj=None):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
518 """Print events as they pass through the transform.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
519
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
520 >>> html = HTML('<body>Some <em>test</em> text</body>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
521 >>> print html | Transformer('em').trace()
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
522 (None, ('START', (QName(u'body'), Attrs()), (None, 1, 0)))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
523 (None, ('TEXT', u'Some ', (None, 1, 6)))
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
524 ('ENTER', ('START', (QName(u'em'), Attrs()), (None, 1, 11)))
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
525 ('INSIDE', ('TEXT', u'test', (None, 1, 15)))
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
526 ('EXIT', ('END', QName(u'em'), (None, 1, 19)))
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
527 (None, ('TEXT', u' text', (None, 1, 24)))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
528 (None, ('END', QName(u'body'), (None, 1, 29)))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
529 <body>Some <em>test</em> text</body>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
530
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
531 :param prefix: a string to prefix each event with in the output
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
532 :param fileobj: the writable file-like object to write to; defaults to
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
533 the standard output stream
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
534 :rtype: `Transformer`
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
535 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
536 return self | TraceTransformation(prefix, fileobj=fileobj)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
537
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
538 # Internal methods
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
539
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
540 def _mark(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
541 for event in stream:
514
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
542 yield OUTSIDE, event
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
543
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
544 def _unmark(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
545 for mark, event in stream:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
546 yield event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
547
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
548
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
549 class SelectTransformation(object):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
550 """Select and mark events that match an XPath expression."""
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
551
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
552 def __init__(self, path):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
553 """Create selection.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
554
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
555 :param path: an XPath expression (as string) or a `Path` object
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
556 """
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
557 if not isinstance(path, Path):
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
558 path = Path(path)
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
559 self.path = path
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
560
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
561 def __call__(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
562 """Apply the transform filter to the marked stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
563
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
564 :param stream: the marked event stream to filter
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
565 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
566 namespaces = {}
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
567 variables = {}
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
568 test = self.path.test()
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
569 stream = iter(stream)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
570 for mark, event in stream:
514
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
571 if mark is None:
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
572 yield mark, event
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
573 continue
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
574 result = test(event, {}, {})
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
575 if result is True:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
576 if event[0] is START:
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
577 yield ENTER, event
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
578 depth = 1
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
579 while depth > 0:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
580 mark, subevent = stream.next()
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
581 if subevent[0] is START:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
582 depth += 1
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
583 elif subevent[0] is END:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
584 depth -= 1
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
585 if depth == 0:
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
586 yield EXIT, subevent
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
587 else:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
588 yield INSIDE, subevent
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
589 test(subevent, {}, {}, updateonly=True)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
590 else:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
591 yield OUTSIDE, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
592 elif result:
517
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
593 yield None, event
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
594 yield ATTRIBUTE, result
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
595 else:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
596 yield None, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
597
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
598
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
599 class InvertTransformation(object):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
600 """Invert selection so that marked events become unmarked, and vice versa.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
601
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
602 Specificaly, all input marks are converted to null marks, and all input
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
603 null marks are converted to OUTSIDE marks.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
604 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
605
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
606 def __call__(self, stream):
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
607 """Apply the transform filter to the marked stream.
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
608
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
609 :param stream: the marked event stream to filter
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
610 """
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
611 for mark, event in stream:
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
612 if mark:
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
613 yield None, event
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
614 else:
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
615 yield OUTSIDE, event
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
616
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
617
514
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
618 class EndTransformation(object):
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
619 """End the current selection."""
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
620
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
621 def __call__(self, stream):
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
622 """Apply the transform filter to the marked stream.
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
623
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
624 :param stream: the marked event stream to filter
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
625 """
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
626 for mark, event in stream:
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
627 yield OUTSIDE, event
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
628
e293bbb40507 Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents: 509
diff changeset
629
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
630 class EmptyTransformation(object):
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
631 """Empty selected elements of all content."""
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
632
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
633 def __call__(self, stream):
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
634 """Apply the transform filter to the marked stream.
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
635
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
636 :param stream: the marked event stream to filter
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
637 """
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
638 for mark, event in stream:
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
639 if mark not in (INSIDE, OUTSIDE):
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
640 yield mark, event
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
641
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
642
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
643 class RemoveTransformation(object):
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
644 """Remove selection from the stream."""
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
645
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
646 def __call__(self, stream):
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
647 """Apply the transform filter to the marked stream.
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
648
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
649 :param stream: the marked event stream to filter
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
650 """
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
651 for mark, event in stream:
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
652 if mark is None:
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
653 yield mark, event
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
654
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
655
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
656 class UnwrapTransformation(object):
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
657 """Remove outtermost enclosing elements from selection."""
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
658
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
659 def __call__(self, stream):
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
660 """Apply the transform filter to the marked stream.
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
661
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
662 :param stream: the marked event stream to filter
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
663 """
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
664 for mark, event in stream:
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
665 if mark not in (ENTER, EXIT):
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
666 yield mark, event
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
667
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
668
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
669 class WrapTransformation(object):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
670 """Wrap selection in an element."""
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
671
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
672 def __init__(self, element):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
673 if isinstance(element, Element):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
674 self.element = element
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
675 else:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
676 self.element = Element(element)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
677
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
678 def __call__(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
679 for mark, event in stream:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
680 if mark:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
681 element = list(self.element.generate())
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
682 for prefix in element[:-1]:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
683 yield None, prefix
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
684 yield mark, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
685 while True:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
686 mark, event = stream.next()
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
687 if not mark:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
688 break
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
689 yield mark, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
690 yield None, element[-1]
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
691 yield mark, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
692 else:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
693 yield mark, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
694
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
695
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
696 class TraceTransformation(object):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
697 """Print events as they pass through the transform."""
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
698
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
699 def __init__(self, prefix='', fileobj=None):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
700 """Trace constructor.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
701
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
702 :param prefix: text to prefix each traced line with.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
703 :param fileobj: the writable file-like object to write to
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
704 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
705 self.prefix = prefix
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
706 self.fileobj = fileobj or sys.stdout
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
707
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
708 def __call__(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
709 """Apply the transform filter to the marked stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
710
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
711 :param stream: the marked event stream to filter
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
712 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
713 for event in stream:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
714 print>>self.fileobj, self.prefix + str(event)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
715 yield event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
716
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
717
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
718 class ApplyTransformation(object):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
719 """Apply a function to the `data` element of events of ``kind`` in the
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
720 selection.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
721 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
722
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
723 def __init__(self, function, kind):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
724 """Create the transform.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
725
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
726 :param function: the function to apply; the function must take one
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
727 argument, the `data` element of each selected event
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
728 :param kind: the stream event ``kind`` to apply the `function` to
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
729 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
730 self.function = function
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
731 self.kind = kind
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
732
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
733 def __call__(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
734 """Apply the transform filter to the marked stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
735
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
736 :param stream: The marked event stream to filter
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
737 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
738 for mark, (kind, data, pos) in stream:
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
739 if mark and self.kind in (None, kind):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
740 yield mark, (kind, self.function(data), pos)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
741 else:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
742 yield mark, (kind, data, pos)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
743
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
744
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
745 class InjectorTransformation(object):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
746 """Abstract base class for transformations that inject content into a
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
747 stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
748
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
749 >>> class Top(InjectorTransformation):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
750 ... def __call__(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
751 ... for event in self._inject():
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
752 ... yield event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
753 ... for event in stream:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
754 ... yield event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
755 >>> html = HTML('<body>Some <em>test</em> text</body>')
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
756 >>> print html | (Transformer('.//em') | Top('Prefix '))
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
757 Prefix <body>Some <em>test</em> text</body>
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
758 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
759 def __init__(self, content):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
760 """Create a new injector.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
761
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
762 :param content: An iterable of Genshi stream events, or a string to be
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
763 injected.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
764 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
765 self.content = content
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
766
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
767 def _inject(self):
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
768 for event in _ensure(self.content):
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
769 yield None, event
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
770
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
771
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
772 class ReplaceTransformation(InjectorTransformation):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
773 """Replace selection with content."""
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
774
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
775 def __call__(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
776 """Apply the transform filter to the marked stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
777
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
778 :param stream: The marked event stream to filter
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
779 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
780 for mark, event in stream:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
781 if mark is not None:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
782 for subevent in self._inject():
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
783 yield subevent
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
784 while True:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
785 mark, event = stream.next()
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
786 if mark is None:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
787 yield mark, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
788 break
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
789 else:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
790 yield mark, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
791
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
792
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
793 class BeforeTransformation(InjectorTransformation):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
794 """Insert content before selection."""
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
795
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
796 def __call__(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
797 """Apply the transform filter to the marked stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
798
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
799 :param stream: The marked event stream to filter
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
800 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
801 for mark, event in stream:
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
802 if mark in (ENTER, OUTSIDE):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
803 for subevent in self._inject():
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
804 yield subevent
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
805 yield mark, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
806
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
807
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
808 class AfterTransformation(InjectorTransformation):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
809 """Insert content after selection."""
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
810
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
811 def __call__(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
812 """Apply the transform filter to the marked stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
813
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
814 :param stream: The marked event stream to filter
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
815 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
816 for mark, event in stream:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
817 yield mark, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
818 if mark:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
819 while True:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
820 mark, event = stream.next()
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
821 if not mark:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
822 break
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
823 yield mark, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
824 for subevent in self._inject():
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
825 yield subevent
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
826 yield mark, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
827
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
828
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
829 class PrependTransformation(InjectorTransformation):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
830 """Prepend content to the inside of selected elements."""
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
831
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
832 def __call__(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
833 """Apply the transform filter to the marked stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
834
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
835 :param stream: The marked event stream to filter
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
836 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
837 for mark, event in stream:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
838 yield mark, event
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
839 if mark in (ENTER, OUTSIDE):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
840 for subevent in self._inject():
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
841 yield subevent
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
842
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
843
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
844 class AppendTransformation(InjectorTransformation):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
845 """Append content after the content of selected elements."""
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
846
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
847 def __call__(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
848 """Apply the transform filter to the marked stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
849
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
850 :param stream: The marked event stream to filter
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
851 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
852 for mark, event in stream:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
853 yield mark, event
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
854 if mark is ENTER:
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
855 while True:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
856 mark, event = stream.next()
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
857 if mark is EXIT:
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
858 break
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
859 yield mark, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
860 for subevent in self._inject():
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
861 yield subevent
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
862 yield mark, event
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
863
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
864
517
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
865 class AttrTransformation(object):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
866 """Set an attribute on selected elements."""
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
867
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
868 def __init__(self, name, value):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
869 """Construct transform.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
870
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
871 :param name: name of the attribute that should be set
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
872 :param value: the value to set
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
873 """
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
874 self.name = name
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
875 self.value = value
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
876
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
877 def __call__(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
878 """Apply the transform filter to the marked stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
879
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
880 :param stream: The marked event stream to filter
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
881 """
517
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
882 callable_value = callable(self.value)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
883 for mark, (kind, data, pos) in stream:
502
53b478e3f3e2 Minor doc and naming improvements for the new transformation filter.
cmlenz
parents: 501
diff changeset
884 if mark is ENTER:
517
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
885 if callable_value:
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
886 value = self.value(self.name, (kind, data, pos))
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
887 else:
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
888 value = self.value
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
889 if value is None:
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
890 attrs = data[1] - [QName(self.name)]
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
891 else:
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
892 attrs = data[1] | [(QName(self.name), value)]
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
893 data = (data[0], attrs)
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
894 yield mark, (kind, data, pos)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
895
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
896
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
897
506
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
898 class StreamBuffer(Stream):
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
899 """Stream event buffer used for cut and copy transformations."""
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
900
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
901 def __init__(self):
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
902 """Create the buffer."""
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
903 Stream.__init__(self, [])
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
904
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
905 def append(self, event):
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
906 """Add an event to the buffer.
517
c98e9dcbedbb More flexible transformation attribute set and delete. Attribute selections are
athomas
parents: 515
diff changeset
907
506
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
908 :param event: the markup event to add
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
909 """
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
910 self.events.append(event)
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
911
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
912 def reset(self):
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
913 """Reset the buffer so that it's empty."""
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
914 del self.events[:]
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
915
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
916
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
917 class CopyTransformation(object):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
918 """Copy selected events into a buffer for later insertion."""
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
919
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
920 def __init__(self, buffer):
506
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
921 """Create the copy transformation.
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
922
506
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
923 :param buffer: the `StreamBuffer` in which the selection should be
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
924 stored
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
925 """
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
926 self.buffer = buffer
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
927
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
928 def __call__(self, stream):
506
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
929 """Apply the transformation to the marked stream.
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
930
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
931 :param stream: the marked event stream to filter
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
932 """
506
f1bfa8a09d99 Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents: 504
diff changeset
933 self.buffer.reset()
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
934 stream = list(stream)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
935 for mark, event in stream:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
936 if mark:
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
937 self.buffer.append(event)
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
938 return stream
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
939
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
940
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
941 class CutTransformation(CopyTransformation, RemoveTransformation):
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
942 """Cut selected events into a buffer for later insertion and remove the
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
943 selection.
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
944 """
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
945
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
946 def __call__(self, stream):
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
947 """Apply the transform filter to the marked stream.
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
948
503
2b4653edbc03 Minor doc tweaks for new transformation filter.
cmlenz
parents: 502
diff changeset
949 :param stream: the marked event stream to filter
501
3073ac688651 Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff changeset
950 """
504
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
951 stream = CopyTransformation.__call__(self, stream)
2db10ef79e66 Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents: 503
diff changeset
952 return RemoveTransformation.__call__(self, stream)
Copyright (C) 2012-2017 Edgewall Software