Mercurial > genshi > genshi-test
annotate genshi/filters/transform.py @ 514:e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
the previous selection. Introduced the end() transformation to reset the
current selection.
author | athomas |
---|---|
date | Wed, 06 Jun 2007 12:51:54 +0000 |
parents | 1997f7af845c |
children | 6983367c1c78 |
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 | 14 """A filter for functional-style transformations of markup streams. |
15 | |
16 The `Transformer` filter provides a variety of transformations that can be | |
17 applied to parts of streams that match given XPath expressions. These | |
18 transformations can be chained to achieve results that would be comparitively | |
19 tedious to achieve by writing stream filters by hand. The approach of chaining | |
20 node selection and transformation has been inspired by the `jQuery`_ Javascript | |
21 library. | |
22 | |
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 |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
54 __all__ = ['Transformer', 'StreamBuffer', 'InjectorTransformation', 'ENTER', |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
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') |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
68 """Stream augmentation mark indicating that a selected range of events is being |
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 |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
73 selected range of events.""" |
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') |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
76 """Stream augmentation mark indicating that processing is currently outside any |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
77 selected range of events.""" |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
78 |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
79 EXIT = TransformMark('EXIT') |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
80 """Stream augmentation mark indicating that a selected range of events is being |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
81 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
|
82 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
83 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
84 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
|
85 """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
|
86 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
|
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 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
|
89 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
|
90 expression. Each marked event is in the form (mark, (kind, data, pos)), |
503 | 91 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
|
92 |
3073ac688651
Added new 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 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
|
94 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
|
95 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
|
96 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
|
97 |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
98 >>> 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
|
99 ... '<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
|
100 |
503 | 101 Transformations act on selected stream events matching an XPath expression. |
102 Here's an example of removing some markup (the title, in this case) | |
103 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
|
104 |
503 | 105 >>> 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
|
106 <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
|
107 |
503 | 108 Inserted content can be passed in the form of a string, or a markup event |
109 stream, which includes streams generated programmatically via the | |
110 `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
|
111 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
112 >>> from genshi.builder import tag |
503 | 113 >>> 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
|
114 <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
|
115 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
|
116 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
117 Each XPath expression determines the set of tags that will be acted upon by |
503 | 118 subsequent transformations. In this example we select the ``<title>`` text, |
119 copy it into a buffer, then select the ``<body>`` element and paste the | |
120 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
|
121 |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
122 >>> buffer = StreamBuffer() |
503 | 123 >>> 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
|
124 ... .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
|
125 <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
|
126 <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
|
127 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
128 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
|
129 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
|
130 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
|
131 |
503 | 132 >>> emphasis = Transformer('body//em').setattr('class', 'emphasis') |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
133 >>> 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
|
134 <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
|
135 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
|
136 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
137 |
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
|
138 __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
|
139 |
3073ac688651
Added new 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 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
|
141 """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
|
142 |
503 | 143 :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
|
144 """ |
3073ac688651
Added new 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 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
|
146 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
|
147 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
|
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 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
|
150 """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
|
151 |
503 | 152 :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
|
153 :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
|
154 :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
|
155 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
156 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
|
157 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
|
158 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
|
159 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
|
160 |
3073ac688651
Added new 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 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
|
162 """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
|
163 |
3073ac688651
Added new 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 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
|
165 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
|
166 |
3073ac688651
Added new 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 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
|
168 |
3073ac688651
Added new 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 >>> 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
|
170 ... 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
|
171 ... 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
|
172 ... 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
|
173 ... 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
|
174 ... 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
|
175 >>> 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
|
176 >>> 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
|
177 <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
|
178 """ |
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
|
179 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
|
180 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
|
181 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
|
182 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
|
183 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
|
184 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
|
185 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
|
186 |
3073ac688651
Added new 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 #{ 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
|
188 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
189 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
|
190 """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
|
191 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
|
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 >>> 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
|
194 >>> 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
|
195 (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
|
196 (None, ('TEXT', u'Some ', (None, 1, 6))) |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
197 ('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
|
198 ('INSIDE', ('TEXT', u'test', (None, 1, 15))) |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
199 ('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
|
200 (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
|
201 (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
|
202 <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
|
203 |
503 | 204 :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
|
205 :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
|
206 :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
|
207 """ |
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
|
208 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
|
209 |
3073ac688651
Added new 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 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
|
211 """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
|
212 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
|
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 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
|
215 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
|
216 |
3073ac688651
Added new 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 >>> 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
|
218 >>> 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
|
219 ('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
|
220 ('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
|
221 (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
|
222 (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
|
223 (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
|
224 ('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
|
225 ('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
|
226 <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
|
227 |
3073ac688651
Added new 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 :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
|
229 """ |
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
|
230 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
|
231 |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
232 def end(self): |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
233 """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
|
234 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
235 Example: |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
236 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
237 >>> 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
|
238 >>> 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
|
239 ('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
|
240 ('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
|
241 ('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
|
242 ('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
|
243 ('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
|
244 ('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
|
245 ('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
|
246 <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
|
247 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
248 :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
|
249 :rtype: `Transformer` |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
250 """ |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
251 return self | EndTransformation() |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
252 |
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
|
253 #{ 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
|
254 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
255 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
|
256 """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
|
257 |
3073ac688651
Added new 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 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
|
259 |
3073ac688651
Added new 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 >>> 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
|
261 ... '<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
|
262 >>> 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
|
263 <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
|
264 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
|
265 |
3073ac688651
Added new 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 :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
|
267 """ |
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
|
268 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
|
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 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
|
271 """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
|
272 |
3073ac688651
Added new 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 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
|
274 |
3073ac688651
Added new 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 >>> 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
|
276 ... '<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
|
277 >>> 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
|
278 <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
|
279 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
|
280 |
3073ac688651
Added new 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 :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
|
282 """ |
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
|
283 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
|
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 #{ 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
|
286 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
287 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
|
288 """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
|
289 |
3073ac688651
Added new 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 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
|
291 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
292 >>> 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
|
293 ... '<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
|
294 >>> 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
|
295 <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
|
296 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
|
297 |
3073ac688651
Added new 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 :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
|
299 """ |
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
|
300 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
|
301 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
|
302 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
|
303 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
|
304 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
|
305 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
306 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
|
307 """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
|
308 |
3073ac688651
Added new 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 >>> 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
|
310 ... '<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
|
311 >>> 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
|
312 <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
|
313 <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
|
314 |
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
|
315 :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
|
316 :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
|
317 """ |
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
|
318 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
|
319 |
3073ac688651
Added new 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 #{ 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
|
321 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
322 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
|
323 """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
|
324 |
3073ac688651
Added new 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 >>> 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
|
326 ... '<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
|
327 >>> 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
|
328 <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
|
329 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
|
330 |
3073ac688651
Added new 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 :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
|
332 :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
|
333 """ |
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
|
334 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
|
335 |
3073ac688651
Added new 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 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
|
337 """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
|
338 |
3073ac688651
Added new 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 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
|
340 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
|
341 |
3073ac688651
Added new 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 >>> 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
|
343 ... '<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
|
344 >>> 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
|
345 <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
|
346 <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
|
347 |
3073ac688651
Added new 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 :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
|
349 :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
|
350 """ |
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
|
351 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
|
352 |
3073ac688651
Added new 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 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
|
354 """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
|
355 |
3073ac688651
Added new 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 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
|
357 |
3073ac688651
Added new 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 >>> 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
|
359 ... '<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
|
360 >>> 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
|
361 <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
|
362 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
|
363 |
3073ac688651
Added new 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 :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
|
365 :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
|
366 """ |
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
|
367 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
|
368 |
3073ac688651
Added new 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 def prepend(self, content): |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
370 """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
|
371 |
3073ac688651
Added new 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 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
|
373 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
374 >>> 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
|
375 ... '<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
|
376 >>> 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
|
377 <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
|
378 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
|
379 |
3073ac688651
Added new 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 :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
|
381 :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
|
382 """ |
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
|
383 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
|
384 |
3073ac688651
Added new 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 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
|
386 """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
|
387 |
3073ac688651
Added new 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 >>> 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
|
389 ... '<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
|
390 >>> 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
|
391 <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
|
392 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
|
393 |
3073ac688651
Added new 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 :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
|
395 :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
|
396 """ |
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
|
397 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
|
398 |
3073ac688651
Added new 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 #{ 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
|
400 |
503 | 401 def setattr(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
|
402 """Add or replace an attribute to selected elements. |
3073ac688651
Added new 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 |
3073ac688651
Added new 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 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
|
405 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
406 >>> 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
|
407 ... '<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
|
408 >>> print html | Transformer('.//em').setattr('class', 'emphasis') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
409 <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
|
410 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
|
411 |
503 | 412 :param name: the name of the attribute |
413 :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
|
414 :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
|
415 """ |
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
|
416 return self | SetattrTransformation(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
|
417 |
503 | 418 def delattr(self, name): |
419 """Remove an attribute from 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
|
420 |
3073ac688651
Added new 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 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
|
422 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
423 >>> 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
|
424 ... '<body>Some <em class="emphasis">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
|
425 ... '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
|
426 >>> print html | Transformer('.//*[@class="emphasis"]').delattr('class') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
427 <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
|
428 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
|
429 |
503 | 430 :param name: the name of the attribute to 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
|
431 :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
|
432 """ |
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
|
433 return self | DelattrTransformation(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
|
434 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
435 #{ 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
|
436 |
3073ac688651
Added new 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 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
|
438 """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
|
439 |
3073ac688651
Added new 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 >>> 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
|
441 >>> 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
|
442 >>> 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
|
443 ... '<body>Some <em>body</em> text.</body></html>') |
509 | 444 >>> 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
|
445 ... .end().select('body').prepend(tag.h1(buffer)) |
509 | 446 <html><head><title>Some Title</title></head><body><h1>Some |
447 Title</h1>Some <em>body</em> text.</body></html> | |
448 | |
449 To ensure that a transformation can be reused deterministically, the | |
450 contents of ``buffer`` is replaced by the ``copy()`` operation: | |
451 | |
452 >>> print buffer | |
453 Some Title | |
454 >>> 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
|
455 ... .end().select('body/em').copy(buffer).end().select('body') \\ |
509 | 456 ... .prepend(tag.h1(buffer)) |
457 <html><head><title>Some | |
458 Title</title></head><body><h1><em>body</em></h1>Some <em>body</em> | |
459 text.</body></html> | |
460 >>> print buffer | |
461 <em>body</em> | |
462 | |
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
|
463 |
507 | 464 :param buffer: the `StreamBuffer` in which the selection should be |
465 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
|
466 :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
|
467 :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
|
468 """ |
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
|
469 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
|
470 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
471 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
|
472 """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
|
473 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
474 >>> 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
|
475 >>> 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
|
476 >>> 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
|
477 ... '<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
|
478 >>> 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
|
479 ... .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
|
480 <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
|
481 <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
|
482 |
507 | 483 :param buffer: the `StreamBuffer` in which the selection should be |
484 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
|
485 :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
|
486 :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
|
487 """ |
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
|
488 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
|
489 |
3073ac688651
Added new 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 #{ 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
|
491 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
492 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
|
493 """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
|
494 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
|
495 |
3073ac688651
Added new 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 >>> 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
|
497 ... '<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
|
498 >>> 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
|
499 <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
|
500 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
|
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
|
502 :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
|
503 :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
|
504 :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
|
505 """ |
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
|
506 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
|
507 |
3073ac688651
Added new 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 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
|
509 """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
|
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 >>> 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
|
512 >>> 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
|
513 (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
|
514 (None, ('TEXT', u'Some ', (None, 1, 6))) |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
515 ('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
|
516 ('INSIDE', ('TEXT', u'test', (None, 1, 15))) |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
517 ('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
|
518 (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
|
519 (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
|
520 <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 |
3073ac688651
Added new 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 :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
|
523 :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
|
524 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
|
525 :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
|
526 """ |
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
|
527 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
|
528 |
3073ac688651
Added new 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 # 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
|
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 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
|
532 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
|
533 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
|
534 |
3073ac688651
Added new 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 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
|
536 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
|
537 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
|
538 |
3073ac688651
Added new 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 |
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
|
540 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
|
541 """Select and mark events that match an XPath expression.""" |
503 | 542 |
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 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
|
544 """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
|
545 |
503 | 546 :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
|
547 """ |
503 | 548 if not isinstance(path, Path): |
549 path = Path(path) | |
550 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
|
551 |
3073ac688651
Added new 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 __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
|
553 """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
|
554 |
503 | 555 :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
|
556 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
557 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
|
558 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
|
559 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
|
560 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
|
561 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
|
562 if mark is None: |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
563 yield mark, event |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
564 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
|
565 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
|
566 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
|
567 if event[0] is START: |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
568 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
|
569 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
|
570 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
|
571 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
|
572 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
|
573 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
|
574 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
|
575 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
|
576 if depth == 0: |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
577 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
|
578 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
|
579 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
|
580 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
|
581 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
|
582 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
|
583 elif result: |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
584 yield ENTER, 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
|
585 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
|
586 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
|
587 |
3073ac688651
Added new 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 |
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
|
589 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
|
590 """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
|
591 |
3073ac688651
Added new 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 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
|
593 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
|
594 """ |
3073ac688651
Added new 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 |
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
|
596 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
|
597 """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
|
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 :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
|
600 """ |
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
|
601 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
|
602 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
|
603 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
|
604 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
|
605 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
|
606 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
607 |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
608 class EndTransformation(object): |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
609 """End the current selection.""" |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
610 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
611 def __call__(self, stream): |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
612 """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
|
613 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
614 :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
|
615 """ |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
616 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
|
617 yield OUTSIDE, event |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
618 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
619 |
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
|
620 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
|
621 """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
|
622 |
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
|
623 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
|
624 """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
|
625 |
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
|
626 :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
|
627 """ |
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
|
628 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
|
629 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
|
630 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
|
631 |
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 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
|
634 """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
|
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 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
|
637 """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
|
638 |
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 :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
|
640 """ |
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 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
|
642 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
|
643 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
|
644 |
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 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
|
647 """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
|
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 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
|
650 """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
|
651 |
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 :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
|
653 """ |
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 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
|
655 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
|
656 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
|
657 |
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 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
|
660 """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
|
661 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
662 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
|
663 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
|
664 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
|
665 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
|
666 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
|
667 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
668 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
|
669 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
|
670 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
|
671 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
|
672 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
|
673 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
|
674 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
|
675 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
|
676 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
|
677 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
|
678 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
|
679 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
|
680 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
|
681 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
|
682 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
|
683 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
|
684 |
3073ac688651
Added new 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 |
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
|
686 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
|
687 """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
|
688 |
3073ac688651
Added new 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 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
|
690 """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
|
691 |
3073ac688651
Added new 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 :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
|
693 :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
|
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 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
|
696 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
|
697 |
3073ac688651
Added new 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 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
|
699 """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
|
700 |
503 | 701 :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
|
702 """ |
3073ac688651
Added new 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 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
|
704 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
|
705 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
|
706 |
3073ac688651
Added new 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 |
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
|
708 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
|
709 """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
|
710 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
|
711 """ |
3073ac688651
Added new 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 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
|
714 """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
|
715 |
503 | 716 :param function: the function to apply; the function must take one |
717 argument, the `data` element of each selected event | |
718 :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
|
719 """ |
3073ac688651
Added new 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 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
|
721 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
|
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 __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
|
724 """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
|
725 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
726 :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
|
727 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
728 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
|
729 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
|
730 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
|
731 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
|
732 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
|
733 |
3073ac688651
Added new 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 |
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
|
735 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
|
736 """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
|
737 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
|
738 |
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 >>> 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
|
740 ... 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
|
741 ... 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
|
742 ... 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
|
743 ... 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
|
744 ... 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
|
745 >>> 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
|
746 >>> 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
|
747 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
|
748 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
749 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
|
750 """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
|
751 |
3073ac688651
Added new 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 :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
|
753 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
|
754 """ |
3073ac688651
Added new 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 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
|
756 |
3073ac688651
Added new 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 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
|
758 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
|
759 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
|
760 |
3073ac688651
Added new 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 |
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
|
762 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
|
763 """Replace selection with content.""" |
503 | 764 |
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
|
765 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
|
766 """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
|
767 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
768 :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
|
769 """ |
3073ac688651
Added new 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 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
|
771 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
|
772 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
|
773 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
|
774 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
|
775 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
|
776 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
|
777 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
|
778 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
|
779 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
|
780 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
|
781 |
3073ac688651
Added new 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 |
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
|
783 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
|
784 """Insert content before selection.""" |
503 | 785 |
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
|
786 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
|
787 """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
|
788 |
3073ac688651
Added new 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 :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
|
790 """ |
3073ac688651
Added new 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 for mark, event in stream: |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
792 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
|
793 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
|
794 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
|
795 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
|
796 |
3073ac688651
Added new 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 |
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
|
798 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
|
799 """Insert content after selection.""" |
503 | 800 |
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
|
801 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
|
802 """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
|
803 |
3073ac688651
Added new 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 :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
|
805 """ |
3073ac688651
Added new 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 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
|
807 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
|
808 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
|
809 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
|
810 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
|
811 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
|
812 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
|
813 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
|
814 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
|
815 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
|
816 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
|
817 |
3073ac688651
Added new 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 |
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
|
819 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
|
820 """Prepend content to the inside of selected elements.""" |
503 | 821 |
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
|
822 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
|
823 """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
|
824 |
3073ac688651
Added new 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 :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
|
826 """ |
3073ac688651
Added new 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 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
|
828 yield mark, event |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
829 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
|
830 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
|
831 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
|
832 |
3073ac688651
Added new 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 |
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
|
834 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
|
835 """Append content after the content of selected elements.""" |
503 | 836 |
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
|
837 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
|
838 """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
|
839 |
3073ac688651
Added new 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 :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
|
841 """ |
3073ac688651
Added new 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 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
|
843 yield mark, event |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
844 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
|
845 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
|
846 mark, event = stream.next() |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
847 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
|
848 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
|
849 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
|
850 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
|
851 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
|
852 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
|
853 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
854 |
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
|
855 class SetattrTransformation(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
|
856 """Set an attribute on selected elements.""" |
503 | 857 |
858 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
|
859 """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
|
860 |
503 | 861 :param name: name of the attribute that should be set |
862 :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
|
863 """ |
503 | 864 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
|
865 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
|
866 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
867 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
|
868 """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
|
869 |
3073ac688651
Added new 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 :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
|
871 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
872 for mark, (kind, data, pos) in stream: |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
873 if mark is ENTER: |
503 | 874 data = (data[0], data[1] | [(QName(self.name), 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
|
875 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
|
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 |
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
|
878 class DelattrTransformation(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
|
879 """Delete an attribute of selected elements.""" |
503 | 880 |
881 def __init__(self, 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
|
882 """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
|
883 |
503 | 884 :param name: the name of the attribute to remove |
885 """ | |
886 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
|
887 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
888 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
|
889 """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
|
890 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
891 :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
|
892 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
893 for mark, (kind, data, pos) in stream: |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
894 if mark is ENTER: |
503 | 895 data = (data[0], data[1] - self.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
|
896 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
|
897 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
898 |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
899 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
|
900 """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
|
901 |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
902 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
|
903 """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
|
904 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
|
905 |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
906 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
|
907 """Add an event to the buffer. |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
908 |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
909 :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
|
910 """ |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
911 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
|
912 |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
913 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
|
914 """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
|
915 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
|
916 |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
917 |
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
|
918 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
|
919 """Copy selected events into a buffer for later insertion.""" |
503 | 920 |
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
|
921 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
|
922 """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
|
923 |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
924 :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
|
925 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
|
926 """ |
3073ac688651
Added new 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 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
|
928 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
929 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
|
930 """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
|
931 |
503 | 932 :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
|
933 """ |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
934 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
|
935 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
|
936 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
|
937 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
|
938 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
|
939 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
|
940 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
941 |
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
|
942 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
|
943 """Cut selected events into a buffer for later insertion and remove the |
503 | 944 selection. |
945 """ | |
946 | |
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
|
947 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
|
948 """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
|
949 |
503 | 950 :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
|
951 """ |
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
|
952 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
|
953 return RemoveTransformation.__call__(self, stream) |