Mercurial > genshi > genshi-test
annotate genshi/filters/transform.py @ 933:feba07fc925b
Merge r1141 from py3k:
add support for python 3 to genshi.filters:
* minor changes to track encoding=None API change in core genshi modules.
* renamed genshi/filters/tests/html.py to test_html.py to avoid clashes with Python 3 top-level html module when running tests subset.
* did not rename genshi/filters/html.py.
* i18n filters:
* ugettext and friends are gone in Python 3 (and only gettext and friends exist and they now handle unicode)
* Some \ line continuations inside doctests confused 2to3 and so were removed them.
* Testing picked up a problem (already present in trunk) where Translator.__call__ could end up defining gettext as an endlessly recursive function. Noted with a TODO.
author | hodgestar |
---|---|
date | Fri, 18 Mar 2011 09:11:53 +0000 |
parents | 24733a5854d9 |
children |
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 # |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
3 # Copyright (C) 2007-2009 Edgewall Software |
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
|
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> |
933 | 34 ... </html>''', |
35 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
36 >>> print(html | Transformer('body/em').map(unicode.upper, TEXT) |
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
37 ... .unwrap().wrap(tag.u)) |
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
|
38 <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
|
39 <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
|
40 <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
|
41 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
|
42 </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
|
43 </html> |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
44 |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
45 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
|
46 box, but custom transformations can be added easily. |
576 | 47 |
48 :since: version 0.5 | |
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
|
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 |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
51 import re |
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
|
52 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
|
53 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
54 from genshi.builder import Element |
668 | 55 from genshi.core import Stream, Attrs, QName, TEXT, START, END, _ensure, Markup |
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
|
56 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
|
57 |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
517
diff
changeset
|
58 __all__ = ['Transformer', 'StreamBuffer', 'InjectorTransformation', 'ENTER', |
744 | 59 'EXIT', 'INSIDE', 'OUTSIDE', 'BREAK'] |
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
|
60 |
3073ac688651
Added new 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 |
3073ac688651
Added new 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 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
|
63 """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
|
64 __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
|
65 _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
|
66 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
67 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
|
68 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
|
69 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
70 |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
71 ENTER = TransformMark('ENTER') |
515 | 72 """Stream augmentation mark indicating that a selected element is being |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
73 entered.""" |
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 INSIDE = TransformMark('INSIDE') |
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 inside a |
515 | 77 selected element.""" |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
78 |
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
|
79 OUTSIDE = TransformMark('OUTSIDE') |
515 | 80 """Stream augmentation mark indicating that a match occurred outside a selected |
81 element.""" | |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
82 |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
83 ATTR = TransformMark('ATTR') |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
84 """Stream augmentation mark indicating a selected element attribute.""" |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
85 |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
86 EXIT = TransformMark('EXIT') |
515 | 87 """Stream augmentation mark indicating that a selected element is being |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
88 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
|
89 |
744 | 90 BREAK = TransformMark('BREAK') |
91 """Stream augmentation mark indicating a break between two otherwise contiguous | |
92 blocks of marked events. | |
93 | |
94 This is used primarily by the cut() transform to provide later transforms with | |
95 an opportunity to operate on the cut buffer. | |
96 """ | |
97 | |
98 | |
99 class PushBackStream(object): | |
100 """Allows a single event to be pushed back onto the stream and re-consumed. | |
101 """ | |
102 def __init__(self, stream): | |
103 self.stream = iter(stream) | |
104 self.peek = None | |
105 | |
106 def push(self, event): | |
107 assert self.peek is None | |
108 self.peek = event | |
109 | |
110 def __iter__(self): | |
111 while True: | |
112 if self.peek is not None: | |
113 peek = self.peek | |
114 self.peek = None | |
115 yield peek | |
116 else: | |
117 try: | |
118 event = self.stream.next() | |
119 yield event | |
120 except StopIteration: | |
121 if self.peek is None: | |
122 raise | |
123 | |
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
|
124 |
3073ac688651
Added new 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 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
|
126 """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
|
127 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
|
128 |
3073ac688651
Added new 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 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
|
130 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
|
131 expression. Each marked event is in the form (mark, (kind, data, pos)), |
503 | 132 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
|
133 |
3073ac688651
Added new 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 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
|
135 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
|
136 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
|
137 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
|
138 |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
139 >>> html = HTML('<html><head><title>Some Title</title></head>' |
933 | 140 ... '<body>Some <em>body</em> text.</body></html>', |
141 ... encoding='utf-8') | |
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
|
142 |
503 | 143 Transformations act on selected stream events matching an XPath expression. |
144 Here's an example of removing some markup (the title, in this case) | |
145 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
|
146 |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
147 >>> 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
|
148 <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
|
149 |
503 | 150 Inserted content can be passed in the form of a string, or a markup event |
151 stream, which includes streams generated programmatically via the | |
152 `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
|
153 |
3073ac688651
Added new 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 >>> from genshi.builder import tag |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
155 >>> 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
|
156 <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
|
157 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
|
158 |
3073ac688651
Added new 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 Each XPath expression determines the set of tags that will be acted upon by |
503 | 160 subsequent transformations. In this example we select the ``<title>`` text, |
161 copy it into a buffer, then select the ``<body>`` element and paste the | |
162 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
|
163 |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
164 >>> buffer = StreamBuffer() |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
165 >>> print(html | Transformer('head/title/text()').copy(buffer) |
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
166 ... .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
|
167 <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
|
168 <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
|
169 |
3073ac688651
Added new 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 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
|
171 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
|
172 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
|
173 |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
174 >>> emphasis = Transformer('body//em').attr('class', 'emphasis') |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
175 >>> 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
|
176 <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
|
177 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
|
178 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
179 |
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
|
180 __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
|
181 |
670 | 182 def __init__(self, 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
|
183 """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
|
184 |
503 | 185 :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
|
186 """ |
670 | 187 self.transforms = [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
|
188 |
744 | 189 def __call__(self, stream, keep_marks=False): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
190 """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
|
191 |
503 | 192 :param stream: the marked event stream to filter |
744 | 193 :param keep_marks: Do not strip transformer selection marks from the |
194 stream. Useful for testing. | |
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
|
195 :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
|
196 :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
|
197 """ |
3073ac688651
Added new 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 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
|
199 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
|
200 transforms = link(transforms) |
744 | 201 if not keep_marks: |
202 transforms = self._unmark(transforms) | |
203 return Stream(transforms, | |
605
bc5faca93699
Text templates now default to rendering as plain text; it is no longer necessary to explicitly specify the "text" method to the `render()` or `serialize()` method of the generated markup stream. See tickets #62 and #118.
cmlenz
parents:
578
diff
changeset
|
204 serializer=getattr(stream, 'serializer', 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
|
205 |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
206 def apply(self, function): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
207 """Apply a transformation to the 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
|
208 |
3073ac688651
Added new 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 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
|
210 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
|
211 |
3073ac688651
Added new 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 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
|
213 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
214 >>> def 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
|
215 ... 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
|
216 ... 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
|
217 ... 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
|
218 ... 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
|
219 ... yield mark, (kind, data, pos) |
933 | 220 >>> short_stream = HTML('<body>Some <em>test</em> text</body>', |
221 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
222 >>> print(short_stream | Transformer('.//em/text()').apply(upper)) |
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
|
223 <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
|
224 """ |
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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 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
|
230 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
|
231 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
|
232 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
233 #{ 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
|
234 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
235 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
|
236 """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
|
237 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
|
238 |
933 | 239 >>> html = HTML('<body>Some <em>test</em> text</body>', encoding='utf-8') |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
240 >>> print(html | Transformer().select('.//em').trace()) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
241 (None, ('START', (QName('body'), Attrs()), (None, 1, 0))) |
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
|
242 (None, ('TEXT', u'Some ', (None, 1, 6))) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
243 ('ENTER', ('START', (QName('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
|
244 ('INSIDE', ('TEXT', u'test', (None, 1, 15))) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
245 ('EXIT', ('END', QName('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
|
246 (None, ('TEXT', u' text', (None, 1, 24))) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
247 (None, ('END', QName('body'), (None, 1, 29))) |
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
|
248 <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
|
249 |
503 | 250 :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
|
251 :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
|
252 :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
|
253 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
254 return self.apply(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
|
255 |
3073ac688651
Added new 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 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
|
257 """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
|
258 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
|
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 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
|
261 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
|
262 |
933 | 263 >>> html = HTML('<body>Some <em>test</em> text</body>', encoding='utf-8') |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
264 >>> print(html | Transformer('//em').invert().trace()) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
265 ('OUTSIDE', ('START', (QName('body'), Attrs()), (None, 1, 0))) |
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
|
266 ('OUTSIDE', ('TEXT', u'Some ', (None, 1, 6))) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
267 (None, ('START', (QName('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
|
268 (None, ('TEXT', u'test', (None, 1, 15))) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
269 (None, ('END', QName('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
|
270 ('OUTSIDE', ('TEXT', u' text', (None, 1, 24))) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
271 ('OUTSIDE', ('END', QName('body'), (None, 1, 29))) |
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
|
272 <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
|
273 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
274 :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
|
275 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
276 return self.apply(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
|
277 |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
278 def end(self): |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
279 """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
|
280 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
281 Example: |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
282 |
933 | 283 >>> html = HTML('<body>Some <em>test</em> text</body>', encoding='utf-8') |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
284 >>> print(html | Transformer('//em').end().trace()) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
285 ('OUTSIDE', ('START', (QName('body'), Attrs()), (None, 1, 0))) |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
286 ('OUTSIDE', ('TEXT', u'Some ', (None, 1, 6))) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
287 ('OUTSIDE', ('START', (QName('em'), Attrs()), (None, 1, 11))) |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
288 ('OUTSIDE', ('TEXT', u'test', (None, 1, 15))) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
289 ('OUTSIDE', ('END', QName('em'), (None, 1, 19))) |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
290 ('OUTSIDE', ('TEXT', u' text', (None, 1, 24))) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
291 ('OUTSIDE', ('END', QName('body'), (None, 1, 29))) |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
292 <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
|
293 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
294 :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
|
295 :rtype: `Transformer` |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
296 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
297 return self.apply(EndTransformation()) |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
298 |
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
|
299 #{ 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
|
300 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
301 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
|
302 """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
|
303 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
304 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
|
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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
933 | 307 ... '<body>Some <em>body</em> text.</body></html>', |
308 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
309 >>> print(html | Transformer('.//em').empty()) |
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
|
310 <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
|
311 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
|
312 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
313 :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
|
314 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
315 return self.apply(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
|
316 |
3073ac688651
Added new 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 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
|
318 """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
|
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 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
|
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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
933 | 323 ... '<body>Some <em>body</em> text.</body></html>', |
324 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
325 >>> print(html | Transformer('.//em').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
|
326 <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
|
327 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
|
328 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
329 :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
|
330 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
331 return self.apply(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
|
332 |
3073ac688651
Added new 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 #{ 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
|
334 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
335 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
|
336 """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
|
337 |
3073ac688651
Added new 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 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
|
339 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
340 >>> html = HTML('<html><head><title>Some Title</title></head>' |
933 | 341 ... '<body>Some <em>body</em> text.</body></html>', |
342 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
343 >>> print(html | Transformer('.//em').unwrap()) |
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
|
344 <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
|
345 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
|
346 |
3073ac688651
Added new 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 :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
|
348 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
349 return self.apply(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
|
350 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
351 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
|
352 """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
|
353 |
3073ac688651
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
933 | 355 ... '<body>Some <em>body</em> text.</body></html>', |
356 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
357 >>> print(html | Transformer('.//em').wrap('strong')) |
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
|
358 <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
|
359 <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
|
360 |
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
|
361 :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
|
362 :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
|
363 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
364 return self.apply(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
|
365 |
3073ac688651
Added new 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 #{ 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
|
367 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
368 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
|
369 """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
|
370 |
3073ac688651
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
933 | 372 ... '<body>Some <em>body</em> text.</body></html>', |
373 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
374 >>> print(html | Transformer('.//title/text()').replace('New 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
|
375 <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
|
376 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
|
377 |
744 | 378 :param content: Either a callable, an iterable of events, or a string |
379 to insert. | |
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
|
380 :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
|
381 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
382 return self.apply(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
|
383 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
384 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
|
385 """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
|
386 |
3073ac688651
Added new 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 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
|
388 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
|
389 |
3073ac688651
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
933 | 391 ... '<body>Some <em>body</em> text.</body></html>', |
392 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
393 >>> print(html | Transformer('.//em').before('emphasised ')) |
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
|
394 <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
|
395 <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
|
396 |
744 | 397 :param content: Either a callable, an iterable of events, or a string |
398 to insert. | |
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
|
399 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
400 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
401 return self.apply(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
|
402 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
403 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
|
404 """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
|
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 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
|
407 |
3073ac688651
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
933 | 409 ... '<body>Some <em>body</em> text.</body></html>', |
410 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
411 >>> print(html | Transformer('.//em').after(' rock')) |
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
|
412 <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
|
413 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
|
414 |
744 | 415 :param content: Either a callable, an iterable of events, or a string |
416 to insert. | |
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 :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
|
418 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
419 return self.apply(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
|
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 def prepend(self, content): |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
422 """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
|
423 |
3073ac688651
Added new 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 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
|
425 |
3073ac688651
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
933 | 427 ... '<body>Some <em>body</em> text.</body></html>', |
428 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
429 >>> print(html | Transformer('.//body').prepend('Some new body 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
|
430 <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
|
431 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
|
432 |
744 | 433 :param content: Either a callable, an iterable of events, or a string |
434 to insert. | |
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
|
435 :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
|
436 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
437 return self.apply(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
|
438 |
3073ac688651
Added new 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 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
|
440 """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
|
441 |
3073ac688651
Added new 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>' |
933 | 443 ... '<body>Some <em>body</em> text.</body></html>', |
444 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
445 >>> print(html | Transformer('.//body').append(' Some new body 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
|
446 <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
|
447 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
|
448 |
744 | 449 :param content: Either a callable, an iterable of events, or a string |
450 to insert. | |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
451 :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
|
452 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
453 return self.apply(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
|
454 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
455 #{ 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
|
456 |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
457 def attr(self, name, value): |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
458 """Add, replace or delete an attribute on selected elements. |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
459 |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
460 If `value` evaulates to `None` the attribute will be deleted from the |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
461 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
|
462 |
3073ac688651
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
464 ... '<body>Some <em class="before">body</em> <em>text</em>.</body>' |
933 | 465 ... '</html>', encoding='utf-8') |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
466 >>> print(html | Transformer('body/em').attr('class', None)) |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
467 <html><head><title>Some Title</title></head><body>Some <em>body</em> |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
468 <em>text</em>.</body></html> |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
469 |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
470 Otherwise the attribute will be set to `value`: |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
471 |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
472 >>> print(html | Transformer('body/em').attr('class', 'emphasis')) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
473 <html><head><title>Some Title</title></head><body>Some <em |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
474 class="emphasis">body</em> <em class="emphasis">text</em>.</body></html> |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
475 |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
476 If `value` is a callable it will be called with the attribute name and |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
477 the `START` event for the matching element. Its return value will then |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
478 be used to set the attribute: |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
479 |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
480 >>> def print_attr(name, event): |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
481 ... attrs = event[1][1] |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
482 ... print(attrs) |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
483 ... return attrs.get(name) |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
484 >>> print(html | Transformer('body/em').attr('class', print_attr)) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
485 Attrs([(QName('class'), u'before')]) |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
486 Attrs() |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
487 <html><head><title>Some Title</title></head><body>Some <em |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
488 class="before">body</em> <em>text</em>.</body></html> |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
489 |
503 | 490 :param name: the name of the attribute |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
491 :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
|
492 :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
|
493 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
494 return self.apply(AttrTransformation(name, value)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
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 #{ 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
|
497 |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
498 def copy(self, buffer, accumulate=False): |
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 """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
|
500 |
744 | 501 The buffer is replaced by each *contiguous* selection before being passed |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
502 to the next transformation. If accumulate=True, further selections will |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
503 be appended to the buffer rather than replacing it. |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
504 |
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
|
505 >>> 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
|
506 >>> 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
|
507 >>> html = HTML('<html><head><title>Some Title</title></head>' |
933 | 508 ... '<body>Some <em>body</em> text.</body></html>', |
509 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
510 >>> print(html | Transformer('head/title/text()').copy(buffer) |
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
511 ... .end().select('body').prepend(tag.h1(buffer))) |
509 | 512 <html><head><title>Some Title</title></head><body><h1>Some |
513 Title</h1>Some <em>body</em> text.</body></html> | |
514 | |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
515 This example illustrates that only a single contiguous selection will |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
516 be buffered: |
509 | 517 |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
518 >>> print(html | Transformer('head/title/text()').copy(buffer) |
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
519 ... .end().select('body/em').copy(buffer).end().select('body') |
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
520 ... .prepend(tag.h1(buffer))) |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
521 <html><head><title>Some Title</title></head><body><h1>Some |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
522 Title</h1>Some <em>body</em> text.</body></html> |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
523 >>> print(buffer) |
509 | 524 <em>body</em> |
525 | |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
526 Element attributes can also be copied for later use: |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
527 |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
528 >>> html = HTML('<html><head><title>Some Title</title></head>' |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
529 ... '<body><em>Some</em> <em class="before">body</em>' |
933 | 530 ... '<em>text</em>.</body></html>', |
531 ... encoding='utf-8') | |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
532 >>> buffer = StreamBuffer() |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
533 >>> def apply_attr(name, entry): |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
534 ... return list(buffer)[0][1][1].get('class') |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
535 >>> print(html | Transformer('body/em[@class]/@class').copy(buffer) |
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
536 ... .end().buffer().select('body/em[not(@class)]') |
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
537 ... .attr('class', apply_attr)) |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
538 <html><head><title>Some Title</title></head><body><em |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
539 class="before">Some</em> <em class="before">body</em><em |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
540 class="before">text</em>.</body></html> |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
541 |
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
|
542 |
507 | 543 :param buffer: the `StreamBuffer` in which the selection should be |
544 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
|
545 :rtype: `Transformer` |
752 | 546 :note: Copy (and cut) copy each individual selected object into the |
744 | 547 buffer before passing to the next transform. For example, the |
548 XPath ``*|text()`` will select all elements and text, each | |
549 instance of which will be copied to the buffer individually | |
550 before passing to the next transform. This has implications for | |
551 how ``StreamBuffer`` objects can be used, so some | |
552 experimentation may be required. | |
553 | |
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
|
554 """ |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
555 return self.apply(CopyTransformation(buffer, accumulate)) |
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 |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
557 def cut(self, buffer, accumulate=False): |
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
|
558 """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
|
559 |
3073ac688651
Added new 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 >>> 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
|
561 >>> 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
|
562 >>> html = HTML('<html><head><title>Some Title</title></head>' |
933 | 563 ... '<body>Some <em>body</em> text.</body></html>', |
564 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
565 >>> print(html | Transformer('.//em/text()').cut(buffer) |
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
566 ... .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
|
567 <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
|
568 <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
|
569 |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
570 Specifying accumulate=True, appends all selected intervals onto the |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
571 buffer. Combining this with the .buffer() operation allows us operate |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
572 on all copied events rather than per-segment. See the documentation on |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
573 buffer() for more information. |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
574 |
507 | 575 :param buffer: the `StreamBuffer` in which the selection should be |
576 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
|
577 :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
|
578 :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
|
579 """ |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
580 return self.apply(CutTransformation(buffer, accumulate)) |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
581 |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
582 def buffer(self): |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
583 """Buffer the entire stream (can consume a considerable amount of |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
584 memory). |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
585 |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
586 Useful in conjunction with copy(accumulate=True) and |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
587 cut(accumulate=True) to ensure that all marked events in the entire |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
588 stream are copied to the buffer before further transformations are |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
589 applied. |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
590 |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
591 For example, to move all <note> elements inside a <notes> tag at the |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
592 top of the document: |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
593 |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
594 >>> doc = HTML('<doc><notes></notes><body>Some <note>one</note> ' |
933 | 595 ... 'text <note>two</note>.</body></doc>', |
596 ... encoding='utf-8') | |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
597 >>> buffer = StreamBuffer() |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
598 >>> print(doc | Transformer('body/note').cut(buffer, accumulate=True) |
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
599 ... .end().buffer().select('notes').prepend(buffer)) |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
600 <doc><notes><note>one</note><note>two</note></notes><body>Some text |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
601 .</body></doc> |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
602 |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
603 """ |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
604 return self.apply(list) |
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
|
605 |
3073ac688651
Added new 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 #{ 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
|
607 |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
608 def filter(self, filter): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
609 """Apply a normal stream filter to the selection. The filter is called |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
610 once for each contiguous block of marked events. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
611 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
612 >>> from genshi.filters.html import HTMLSanitizer |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
613 >>> html = HTML('<html><body>Some text<script>alert(document.cookie)' |
933 | 614 ... '</script> and some more text</body></html>', |
615 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
616 >>> print(html | Transformer('body/*').filter(HTMLSanitizer())) |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
617 <html><body>Some text and some more text</body></html> |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
618 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
619 :param filter: The stream filter to apply. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
620 :rtype: `Transformer` |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
621 """ |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
622 return self.apply(FilterTransformation(filter)) |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
623 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
624 def map(self, function, kind): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
625 """Applies a function to the ``data`` element of events of ``kind`` in |
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
|
626 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
|
627 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
628 >>> html = HTML('<html><head><title>Some Title</title></head>' |
933 | 629 ... '<body>Some <em>body</em> text.</body></html>', |
630 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
631 >>> print(html | Transformer('head/title').map(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
|
632 <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
|
633 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
|
634 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
635 :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
|
636 :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
|
637 :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
|
638 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
639 return self.apply(MapTransformation(function, kind)) |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
640 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
641 def substitute(self, pattern, replace, count=1): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
642 """Replace text matching a regular expression. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
643 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
644 Refer to the documentation for ``re.sub()`` for details. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
645 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
646 >>> html = HTML('<html><body>Some text, some more text and ' |
728 | 647 ... '<b>some bold text</b>\\n' |
933 | 648 ... '<i>some italicised text</i></body></html>', |
649 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
650 >>> print(html | Transformer('body/b').substitute('(?i)some', 'SOME')) |
728 | 651 <html><body>Some text, some more text and <b>SOME bold text</b> |
652 <i>some italicised text</i></body></html> | |
653 >>> tags = tag.html(tag.body('Some text, some more text and\\n', | |
668 | 654 ... Markup('<b>some bold text</b>'))) |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
655 >>> print(tags.generate() | Transformer('body').substitute( |
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
656 ... '(?i)some', 'SOME')) |
728 | 657 <html><body>SOME text, some more text and |
658 <b>SOME bold text</b></body></html> | |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
659 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
660 :param pattern: A regular expression object or string. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
661 :param replace: Replacement pattern. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
662 :param count: Number of replacements to make in each text fragment. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
663 :rtype: `Transformer` |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
664 """ |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
665 return self.apply(SubstituteTransformation(pattern, replace, count)) |
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
|
666 |
578 | 667 def rename(self, name): |
668 """Rename matching elements. | |
669 | |
670 >>> html = HTML('<html><body>Some text, some more text and ' | |
933 | 671 ... '<b>some bold text</b></body></html>', |
672 ... encoding='utf-8') | |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
673 >>> print(html | Transformer('body/b').rename('strong')) |
578 | 674 <html><body>Some text, some more text and <strong>some bold text</strong></body></html> |
675 """ | |
676 return self.apply(RenameTransformation(name)) | |
677 | |
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
|
678 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
|
679 """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
|
680 |
933 | 681 >>> html = HTML('<body>Some <em>test</em> text</body>', encoding='utf-8') |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
682 >>> print(html | Transformer('em').trace()) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
683 (None, ('START', (QName('body'), Attrs()), (None, 1, 0))) |
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
|
684 (None, ('TEXT', u'Some ', (None, 1, 6))) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
685 ('ENTER', ('START', (QName('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
|
686 ('INSIDE', ('TEXT', u'test', (None, 1, 15))) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
687 ('EXIT', ('END', QName('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
|
688 (None, ('TEXT', u' text', (None, 1, 24))) |
857
24733a5854d9
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
854
diff
changeset
|
689 (None, ('END', QName('body'), (None, 1, 29))) |
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
|
690 <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
|
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: 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
|
693 :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
|
694 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
|
695 :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
|
696 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
697 return self.apply(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
|
698 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
699 # 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
|
700 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
701 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
|
702 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
|
703 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
|
704 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
705 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
|
706 for mark, event in stream: |
744 | 707 kind = event[0] |
708 if not (kind is None or kind is ATTR or kind is BREAK): | |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
709 yield 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
|
710 |
3073ac688651
Added new 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 |
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
|
712 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
|
713 """Select and mark events that match an XPath expression.""" |
503 | 714 |
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
|
715 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
|
716 """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
|
717 |
503 | 718 :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
|
719 """ |
503 | 720 if not isinstance(path, Path): |
721 path = Path(path) | |
722 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
|
723 |
3073ac688651
Added new 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 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
|
725 """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
|
726 |
503 | 727 :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
|
728 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
729 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
|
730 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
|
731 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
|
732 stream = iter(stream) |
818 | 733 next = stream.next |
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
|
734 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
|
735 if mark is None: |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
736 yield mark, event |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
737 continue |
818 | 738 result = test(event, namespaces, variables) |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
739 # XXX This is effectively genshi.core._ensure() for transform |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
740 # streams. |
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
|
741 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
|
742 if event[0] is START: |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
743 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
|
744 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
|
745 while depth > 0: |
818 | 746 mark, subevent = next() |
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
|
747 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
|
748 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
|
749 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
|
750 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
|
751 if depth == 0: |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
752 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
|
753 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
|
754 yield INSIDE, subevent |
818 | 755 test(subevent, namespaces, variables, updateonly=True) |
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
|
756 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
|
757 yield OUTSIDE, event |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
758 elif isinstance(result, Attrs): |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
759 # XXX Selected *attributes* are given a "kind" of None to |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
760 # indicate they are not really part of the stream. |
744 | 761 yield ATTR, (ATTR, (QName(event[1][0] + '@*'), result), event[2]) |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
762 yield None, event |
744 | 763 elif isinstance(result, tuple): |
764 yield OUTSIDE, 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
|
765 elif result: |
744 | 766 # XXX Assume everything else is "text"? |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
767 yield None, (TEXT, unicode(result), (None, -1, -1)) |
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
|
768 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
|
769 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
|
770 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
771 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
772 class 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
|
773 """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
|
774 |
3073ac688651
Added new 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 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
|
776 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
|
777 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
778 |
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
|
779 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
|
780 """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
|
781 |
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
|
782 :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
|
783 """ |
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
|
784 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
|
785 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
|
786 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
|
787 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
|
788 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
|
789 |
3073ac688651
Added new 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 |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
791 class EndTransformation(object): |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
792 """End the current selection.""" |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
793 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
794 def __call__(self, stream): |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
795 """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
|
796 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
797 :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
|
798 """ |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
799 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
|
800 yield OUTSIDE, event |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
801 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
802 |
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
|
803 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
|
804 """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
|
805 |
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
|
806 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
|
807 """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
|
808 |
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
|
809 :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
|
810 """ |
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
|
811 for mark, event in stream: |
744 | 812 yield mark, event |
813 if mark is ENTER: | |
814 for mark, event in stream: | |
815 if mark is EXIT: | |
816 yield mark, event | |
817 break | |
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
|
818 |
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 |
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
|
820 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
|
821 """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
|
822 |
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
|
823 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
|
824 """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
|
825 |
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
|
826 :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
|
827 """ |
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
|
828 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
|
829 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
|
830 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
|
831 |
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
|
832 |
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
|
833 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
|
834 """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
|
835 |
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
|
836 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
|
837 """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
|
838 |
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
|
839 :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
|
840 """ |
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
|
841 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
|
842 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
|
843 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
|
844 |
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
|
845 |
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
|
846 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
|
847 """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
|
848 |
3073ac688651
Added new 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 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
|
850 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
|
851 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
|
852 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
|
853 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
|
854 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
855 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
|
856 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
|
857 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
|
858 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
|
859 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
|
860 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
|
861 yield mark, event |
744 | 862 start = mark |
863 stopped = False | |
864 for mark, event in stream: | |
865 if start is ENTER and mark is EXIT: | |
866 yield mark, event | |
867 stopped = True | |
868 break | |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
869 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
|
870 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
|
871 yield mark, event |
744 | 872 else: |
873 stopped = True | |
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
|
874 yield None, element[-1] |
744 | 875 if not stopped: |
876 yield mark, 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
|
877 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
|
878 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
|
879 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
880 |
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
|
881 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
|
882 """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
|
883 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
884 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
|
885 """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
|
886 |
3073ac688651
Added new 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 :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
|
888 :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
|
889 """ |
3073ac688651
Added new 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 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
|
891 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
|
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 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
|
894 """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
|
895 |
503 | 896 :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
|
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 for event in stream: |
854
0d9e87c6cf6e
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
899 self.fileobj.write('%s%s\n' % (self.prefix, 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
|
900 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
|
901 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
902 |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
903 class FilterTransformation(object): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
904 """Apply a normal stream filter to the selection. The filter is called once |
744 | 905 for each selection.""" |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
906 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
907 def __init__(self, filter): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
908 """Create the transform. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
909 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
910 :param filter: The stream filter to apply. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
911 """ |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
912 self.filter = filter |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
913 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
914 def __call__(self, stream): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
915 """Apply the transform filter to the marked stream. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
916 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
917 :param stream: The marked event stream to filter |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
918 """ |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
919 def flush(queue): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
920 if queue: |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
921 for event in self.filter(queue): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
922 yield OUTSIDE, event |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
923 del queue[:] |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
924 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
925 queue = [] |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
926 for mark, event in stream: |
744 | 927 if mark is ENTER: |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
928 queue.append(event) |
744 | 929 for mark, event in stream: |
930 queue.append(event) | |
931 if mark is EXIT: | |
932 break | |
577
5796de680d94
Fix for #136, where transformer marks were not being stripped correctly when
athomas
parents:
576
diff
changeset
|
933 for queue_event in flush(queue): |
5796de680d94
Fix for #136, where transformer marks were not being stripped correctly when
athomas
parents:
576
diff
changeset
|
934 yield queue_event |
744 | 935 elif mark is OUTSIDE: |
800
1d7b582b09a4
Fix for filter transformation, closing #290. Thanks to Stephen Compall for reporting the problem and providing a patch.
cmlenz
parents:
793
diff
changeset
|
936 stopped = False |
744 | 937 queue.append(event) |
938 for mark, event in stream: | |
939 if mark is not OUTSIDE: | |
940 break | |
941 queue.append(event) | |
942 else: | |
943 stopped = True | |
944 for queue_event in flush(queue): | |
945 yield queue_event | |
946 if not stopped: | |
800
1d7b582b09a4
Fix for filter transformation, closing #290. Thanks to Stephen Compall for reporting the problem and providing a patch.
cmlenz
parents:
793
diff
changeset
|
947 yield mark, event |
744 | 948 else: |
949 yield mark, event | |
950 for queue_event in flush(queue): | |
951 yield queue_event | |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
952 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
953 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
954 class MapTransformation(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
|
955 """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
|
956 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
|
957 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
958 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
959 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
|
960 """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
|
961 |
503 | 962 :param function: the function to apply; the function must take one |
963 argument, the `data` element of each selected event | |
964 :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
|
965 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
966 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
|
967 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
|
968 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
969 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
|
970 """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
|
971 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
972 :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
|
973 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
974 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
|
975 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
|
976 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
|
977 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
|
978 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
|
979 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
980 |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
981 class SubstituteTransformation(object): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
982 """Replace text matching a regular expression. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
983 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
984 Refer to the documentation for ``re.sub()`` for details. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
985 """ |
744 | 986 def __init__(self, pattern, replace, count=0): |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
987 """Create the transform. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
988 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
989 :param pattern: A regular expression object, or string. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
990 :param replace: Replacement pattern. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
991 :param count: Number of replacements to make in each text fragment. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
992 """ |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
993 if isinstance(pattern, basestring): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
994 self.pattern = re.compile(pattern) |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
995 else: |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
996 self.pattern = pattern |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
997 self.count = count |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
998 self.replace = replace |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
999 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
1000 def __call__(self, stream): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
1001 """Apply the transform filter to the marked stream. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
1002 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
1003 :param stream: The marked event stream to filter |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
1004 """ |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
1005 for mark, (kind, data, pos) in stream: |
728 | 1006 if mark is not None and kind is TEXT: |
668 | 1007 new_data = self.pattern.sub(self.replace, data, self.count) |
1008 if isinstance(data, Markup): | |
1009 data = Markup(new_data) | |
1010 else: | |
1011 data = new_data | |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
1012 yield mark, (kind, data, pos) |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
1013 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
1014 |
578 | 1015 class RenameTransformation(object): |
1016 """Rename matching elements.""" | |
1017 def __init__(self, name): | |
1018 """Create the transform. | |
1019 | |
1020 :param name: New element name. | |
1021 """ | |
1022 self.name = QName(name) | |
1023 | |
1024 def __call__(self, stream): | |
1025 """Apply the transform filter to the marked stream. | |
1026 | |
1027 :param stream: The marked event stream to filter | |
1028 """ | |
1029 for mark, (kind, data, pos) in stream: | |
1030 if mark is ENTER: | |
1031 data = self.name, data[1] | |
1032 elif mark is EXIT: | |
1033 data = self.name | |
1034 yield mark, (kind, data, pos) | |
1035 | |
1036 | |
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
|
1037 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
|
1038 """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
|
1039 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
|
1040 |
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
|
1041 >>> 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
|
1042 ... 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
|
1043 ... 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
|
1044 ... 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
|
1045 ... 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
|
1046 ... yield event |
933 | 1047 >>> html = HTML('<body>Some <em>test</em> text</body>', encoding='utf-8') |
853
4376010bb97e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
818
diff
changeset
|
1048 >>> print(html | Transformer('.//em').apply(Top('Prefix '))) |
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
|
1049 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
|
1050 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1051 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
|
1052 """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
|
1053 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1054 :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
|
1055 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
|
1056 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1057 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
|
1058 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1059 def _inject(self): |
744 | 1060 content = self.content |
793 | 1061 if hasattr(content, '__call__'): |
744 | 1062 content = content() |
1063 for event in _ensure(content): | |
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
|
1064 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
|
1065 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1066 |
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
|
1067 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
|
1068 """Replace selection with content.""" |
503 | 1069 |
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
|
1070 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
|
1071 """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
|
1072 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1073 :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
|
1074 """ |
744 | 1075 stream = PushBackStream(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
|
1076 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
|
1077 if mark is not None: |
744 | 1078 start = mark |
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
|
1079 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
|
1080 yield subevent |
744 | 1081 for mark, event in stream: |
1082 if start is ENTER: | |
1083 if mark is EXIT: | |
1084 break | |
1085 elif mark != start: | |
1086 stream.push((mark, 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
|
1087 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
|
1088 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
|
1089 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
|
1090 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1091 |
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
|
1092 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
|
1093 """Insert content before selection.""" |
503 | 1094 |
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
|
1095 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
|
1096 """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
|
1097 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1098 :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
|
1099 """ |
744 | 1100 stream = PushBackStream(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
|
1101 for mark, event in stream: |
575
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
1102 if mark is not None: |
744 | 1103 start = mark |
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
|
1104 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
|
1105 yield subevent |
575
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
1106 yield mark, event |
744 | 1107 for mark, event in stream: |
1108 if mark != start and start is not ENTER: | |
1109 stream.push((mark, event)) | |
575
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
1110 break |
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
1111 yield mark, event |
744 | 1112 if start is ENTER and mark is EXIT: |
1113 break | |
1114 else: | |
1115 yield mark, 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
|
1116 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1117 |
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
|
1118 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
|
1119 """Insert content after selection.""" |
503 | 1120 |
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
|
1121 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
|
1122 """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
|
1123 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1124 :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
|
1125 """ |
744 | 1126 stream = PushBackStream(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
|
1127 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
|
1128 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
|
1129 if mark: |
744 | 1130 start = mark |
1131 for mark, event in stream: | |
1132 if start is not ENTER and mark != start: | |
1133 stream.push((mark, 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
|
1134 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
|
1135 yield mark, event |
744 | 1136 if start is ENTER and mark is EXIT: |
1137 break | |
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
|
1138 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
|
1139 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
|
1140 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1141 |
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
|
1142 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
|
1143 """Prepend content to the inside of selected elements.""" |
503 | 1144 |
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
|
1145 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
|
1146 """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
|
1147 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1148 :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
|
1149 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1150 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
|
1151 yield mark, event |
744 | 1152 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
|
1153 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
|
1154 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
|
1155 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1156 |
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
|
1157 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
|
1158 """Append content after the content of selected elements.""" |
503 | 1159 |
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
|
1160 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
|
1161 """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
|
1162 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1163 :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
|
1164 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1165 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
|
1166 yield mark, event |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
1167 if mark is ENTER: |
744 | 1168 for mark, event in stream: |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
1169 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
|
1170 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
|
1171 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
|
1172 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
|
1173 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
|
1174 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
|
1175 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1176 |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1177 class AttrTransformation(object): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1178 """Set an attribute on selected elements.""" |
503 | 1179 |
1180 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
|
1181 """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
|
1182 |
503 | 1183 :param name: name of the attribute that should be set |
1184 :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
|
1185 """ |
503 | 1186 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
|
1187 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
|
1188 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1189 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
|
1190 """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
|
1191 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1192 :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
|
1193 """ |
793 | 1194 callable_value = hasattr(self.value, '__call__') |
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
|
1195 for mark, (kind, data, pos) in stream: |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
1196 if mark is ENTER: |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1197 if callable_value: |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1198 value = self.value(self.name, (kind, data, pos)) |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1199 else: |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1200 value = self.value |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1201 if value is None: |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1202 attrs = data[1] - [QName(self.name)] |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1203 else: |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1204 attrs = data[1] | [(QName(self.name), value)] |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1205 data = (data[0], attrs) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1206 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
|
1207 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1208 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1209 |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1210 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
|
1211 """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
|
1212 |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1213 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
|
1214 """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
|
1215 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
|
1216 |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1217 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
|
1218 """Add an event to the buffer. |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1219 |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1220 :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
|
1221 """ |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1222 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
|
1223 |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1224 def reset(self): |
744 | 1225 """Empty the buffer of events.""" |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1226 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
|
1227 |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1228 |
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
|
1229 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
|
1230 """Copy selected events into a buffer for later insertion.""" |
503 | 1231 |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1232 def __init__(self, buffer, accumulate=False): |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1233 """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
|
1234 |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1235 :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
|
1236 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
|
1237 """ |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1238 if not accumulate: |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1239 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
|
1240 self.buffer = buffer |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1241 self.accumulate = accumulate |
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
|
1242 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1243 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
|
1244 """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
|
1245 |
503 | 1246 :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
|
1247 """ |
744 | 1248 stream = PushBackStream(stream) |
1249 | |
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
|
1250 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
|
1251 if mark: |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1252 if not self.accumulate: |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1253 self.buffer.reset() |
744 | 1254 events = [(mark, event)] |
1255 self.buffer.append(event) | |
1256 start = mark | |
1257 for mark, event in stream: | |
1258 if start is not ENTER and mark != start: | |
1259 stream.push((mark, event)) | |
1260 break | |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1261 events.append((mark, event)) |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1262 self.buffer.append(event) |
744 | 1263 if start is ENTER and mark is EXIT: |
1264 break | |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1265 for i in events: |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1266 yield i |
744 | 1267 else: |
1268 yield mark, 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
|
1269 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1270 |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1271 class CutTransformation(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
|
1272 """Cut selected events into a buffer for later insertion and remove the |
503 | 1273 selection. |
1274 """ | |
1275 | |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1276 def __init__(self, buffer, accumulate=False): |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1277 """Create the cut transformation. |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1278 |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1279 :param buffer: the `StreamBuffer` in which the selection should be |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1280 stored |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1281 """ |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1282 self.buffer = buffer |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1283 self.accumulate = accumulate |
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1284 |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1285 |
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
|
1286 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
|
1287 """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
|
1288 |
503 | 1289 :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
|
1290 """ |
744 | 1291 attributes = [] |
1292 stream = PushBackStream(stream) | |
1293 broken = False | |
1294 if not self.accumulate: | |
1295 self.buffer.reset() | |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1296 for mark, event in stream: |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1297 if mark: |
744 | 1298 # Send a BREAK event if there was no other event sent between |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1299 if not self.accumulate: |
744 | 1300 if not broken and self.buffer: |
1301 yield BREAK, (BREAK, None, None) | |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1302 self.buffer.reset() |
744 | 1303 self.buffer.append(event) |
1304 start = mark | |
1305 if mark is ATTR: | |
1306 attributes.extend([name for name, _ in event[1][1]]) | |
1307 for mark, event in stream: | |
1308 if start is mark is ATTR: | |
1309 attributes.extend([name for name, _ in event[1][1]]) | |
1310 # Handle non-element contiguous selection | |
1311 if start is not ENTER and mark != start: | |
1312 # Operating on the attributes of a START event | |
1313 if start is ATTR: | |
1314 kind, data, pos = event | |
1315 assert kind is START | |
1316 data = (data[0], data[1] - attributes) | |
1317 attributes = None | |
1318 stream.push((mark, (kind, data, pos))) | |
1319 else: | |
1320 stream.push((mark, event)) | |
1321 break | |
734
a61ab7bbe12d
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1322 self.buffer.append(event) |
744 | 1323 if start is ENTER and mark is EXIT: |
1324 break | |
1325 broken = False | |
1326 else: | |
1327 broken = True | |
1328 yield mark, event | |
1329 if not broken and self.buffer: | |
1330 yield BREAK, (BREAK, None, None) |