Mercurial > genshi > mirror
annotate genshi/filters/transform.py @ 782:ae44bf4d1638 trunk
Fix wrong XPath example in `Transformer` docstring that breaks with the more correct XPath processing on the GSoC branch.
author | cmlenz |
---|---|
date | Mon, 14 Jul 2008 16:47:36 +0000 |
parents | 8bb31ed1072e |
children | be88c77839fc |
rev | line source |
---|---|
501
5e7604c2d60d
Added new 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 -*- |
5e7604c2d60d
Added new 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 # |
531 | 3 # Copyright (C) 2007 Edgewall Software |
501
5e7604c2d60d
Added new 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. |
5e7604c2d60d
Added new 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 # |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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. |
5e7604c2d60d
Added new 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 # |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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/. |
5e7604c2d60d
Added new 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
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
24 |
11842b2d5941
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 |
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
26 the ``<head>`` of the input document: |
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
27 |
504
a2dca8066a5a
Renamed 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 |
a2dca8066a5a
Renamed 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> |
a2dca8066a5a
Renamed 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> |
a2dca8066a5a
Renamed 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> |
a2dca8066a5a
Renamed 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. |
a2dca8066a5a
Renamed 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> |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
34 ... </html>''') |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
35 >>> print html | Transformer('body/em').map(unicode.upper, TEXT) \\ |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
36 ... .unwrap().wrap(tag.u) |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
37 <html> |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
38 <head><title>Some Title</title></head> |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
39 <body> |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
40 Some <u>BODY</u> text. |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
41 </body> |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
42 </html> |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
43 |
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
44 The ``Transformer`` support a large number of useful transformations out of the |
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
45 box, but custom transformations can be added easily. |
576 | 46 |
47 :since: version 0.5 | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
48 """ |
5e7604c2d60d
Added new 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 |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
50 import re |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
51 import sys |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 from genshi.builder import Element |
668 | 54 from genshi.core import Stream, Attrs, QName, TEXT, START, END, _ensure, Markup |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
55 from genshi.path import Path |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
56 |
518
c1f2a859fd75
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
517
diff
changeset
|
57 __all__ = ['Transformer', 'StreamBuffer', 'InjectorTransformation', 'ENTER', |
744 | 58 'EXIT', 'INSIDE', 'OUTSIDE', 'BREAK'] |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
59 |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 class TransformMark(str): |
5e7604c2d60d
Added new 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 """A mark on a transformation stream.""" |
5e7604c2d60d
Added new 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 __slots__ = [] |
5e7604c2d60d
Added new 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 _instances = {} |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 def __new__(cls, val): |
5e7604c2d60d
Added new 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 return cls._instances.setdefault(val, str.__new__(cls, val)) |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
70 ENTER = TransformMark('ENTER') |
515 | 71 """Stream augmentation mark indicating that a selected element is being |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
72 entered.""" |
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
73 |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
74 INSIDE = TransformMark('INSIDE') |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
75 """Stream augmentation mark indicating that processing is currently inside a |
515 | 76 selected element.""" |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
77 |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
78 OUTSIDE = TransformMark('OUTSIDE') |
515 | 79 """Stream augmentation mark indicating that a match occurred outside a selected |
80 element.""" | |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
81 |
519
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
82 ATTR = TransformMark('ATTR') |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
83 """Stream augmentation mark indicating a selected element attribute.""" |
517
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
84 |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
85 EXIT = TransformMark('EXIT') |
515 | 86 """Stream augmentation mark indicating that a selected element is being |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
87 exited.""" |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
88 |
744 | 89 BREAK = TransformMark('BREAK') |
90 """Stream augmentation mark indicating a break between two otherwise contiguous | |
91 blocks of marked events. | |
92 | |
93 This is used primarily by the cut() transform to provide later transforms with | |
94 an opportunity to operate on the cut buffer. | |
95 """ | |
96 | |
97 | |
98 class PushBackStream(object): | |
99 """Allows a single event to be pushed back onto the stream and re-consumed. | |
100 """ | |
101 def __init__(self, stream): | |
102 self.stream = iter(stream) | |
103 self.peek = None | |
104 | |
105 def push(self, event): | |
106 assert self.peek is None | |
107 self.peek = event | |
108 | |
109 def __iter__(self): | |
110 while True: | |
111 if self.peek is not None: | |
112 peek = self.peek | |
113 self.peek = None | |
114 yield peek | |
115 else: | |
116 try: | |
117 event = self.stream.next() | |
118 yield event | |
119 except StopIteration: | |
120 if self.peek is None: | |
121 raise | |
122 | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
123 |
5e7604c2d60d
Added new 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 class Transformer(object): |
5e7604c2d60d
Added new 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 """Stream filter that can apply a variety of different transformations to |
5e7604c2d60d
Added new 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 a stream. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 This is achieved by selecting the events to be transformed using XPath, |
5e7604c2d60d
Added new 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 then applying the transformations to the events matched by the path |
5e7604c2d60d
Added new 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 expression. Each marked event is in the form (mark, (kind, data, pos)), |
503 | 131 where mark can be any of `ENTER`, `INSIDE`, `EXIT`, `OUTSIDE`, or `None`. |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
132 |
5e7604c2d60d
Added new 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 The first three marks match `START` and `END` events, and any events |
5e7604c2d60d
Added new 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 contained `INSIDE` any selected XML/HTML element. A non-element match |
5e7604c2d60d
Added new 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 outside a `START`/`END` container (e.g. ``text()``) will yield an `OUTSIDE` |
5e7604c2d60d
Added new 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 mark. |
5e7604c2d60d
Added new 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 |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
138 >>> html = HTML('<html><head><title>Some Title</title></head>' |
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
139 ... '<body>Some <em>body</em> text.</body></html>') |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
140 |
503 | 141 Transformations act on selected stream events matching an XPath expression. |
142 Here's an example of removing some markup (the title, in this case) | |
143 selected by an expression: | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
144 |
503 | 145 >>> print html | Transformer('head/title').remove() |
501
5e7604c2d60d
Added new 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 <html><head/><body>Some <em>body</em> text.</body></html> |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
147 |
503 | 148 Inserted content can be passed in the form of a string, or a markup event |
149 stream, which includes streams generated programmatically via the | |
150 `builder` module: | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
151 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
152 >>> from genshi.builder import tag |
503 | 153 >>> print html | Transformer('body').prepend(tag.h1('Document Title')) |
501
5e7604c2d60d
Added new 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 <html><head><title>Some Title</title></head><body><h1>Document |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
155 Title</h1>Some <em>body</em> text.</body></html> |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 Each XPath expression determines the set of tags that will be acted upon by |
503 | 158 subsequent transformations. In this example we select the ``<title>`` text, |
159 copy it into a buffer, then select the ``<body>`` element and paste the | |
160 copied text into the body as ``<h1>`` enclosed text: | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
161 |
506
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
162 >>> buffer = StreamBuffer() |
503 | 163 >>> print html | Transformer('head/title/text()').copy(buffer) \\ |
514
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
164 ... .end().select('body').prepend(tag.h1(buffer)) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
165 <html><head><title>Some Title</title></head><body><h1>Some Title</h1>Some |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
166 <em>body</em> text.</body></html> |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
168 Transformations can also be assigned and reused, although care must be |
5e7604c2d60d
Added new 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 taken when using buffers, to ensure that buffers are cleared between |
5e7604c2d60d
Added new 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 transforms: |
5e7604c2d60d
Added new 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 |
517
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
172 >>> emphasis = Transformer('body//em').attr('class', 'emphasis') |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
173 >>> print html | emphasis |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
174 <html><head><title>Some Title</title></head><body>Some <em |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
175 class="emphasis">body</em> text.</body></html> |
5e7604c2d60d
Added new 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 """ |
5e7604c2d60d
Added new 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 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
178 __slots__ = ['transforms'] |
501
5e7604c2d60d
Added new 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 |
670 | 180 def __init__(self, path='.'): |
501
5e7604c2d60d
Added new 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 """Construct a new transformation filter. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
182 |
503 | 183 :param path: an XPath expression (as string) or a `Path` instance |
501
5e7604c2d60d
Added new 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 """ |
670 | 185 self.transforms = [SelectTransformation(path)] |
501
5e7604c2d60d
Added new 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 |
744 | 187 def __call__(self, stream, keep_marks=False): |
501
5e7604c2d60d
Added new 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 """Apply the transform filter to the marked stream. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
189 |
503 | 190 :param stream: the marked event stream to filter |
744 | 191 :param keep_marks: Do not strip transformer selection marks from the |
192 stream. Useful for testing. | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
193 :return: the transformed stream |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
194 :rtype: `Stream` |
5e7604c2d60d
Added new 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 """ |
5e7604c2d60d
Added new 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 transforms = self._mark(stream) |
5e7604c2d60d
Added new 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 for link in self.transforms: |
5e7604c2d60d
Added new 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 = link(transforms) |
744 | 199 if not keep_marks: |
200 transforms = self._unmark(transforms) | |
201 return Stream(transforms, | |
605
d0345c64da65
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
|
202 serializer=getattr(stream, 'serializer', None)) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
203 |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
204 def apply(self, function): |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
205 """Apply a transformation to the stream. |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
206 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
207 Transformations can be chained, similar to stream filters. Any callable |
5e7604c2d60d
Added new 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 accepting a marked stream can be used as a transform. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 As an example, here is a simple `TEXT` event upper-casing transform: |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 >>> def upper(stream): |
5e7604c2d60d
Added new 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 ... for mark, (kind, data, pos) in stream: |
5e7604c2d60d
Added new 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 ... if mark and kind is TEXT: |
5e7604c2d60d
Added new 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 ... yield mark, (kind, data.upper(), pos) |
5e7604c2d60d
Added new 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 ... else: |
5e7604c2d60d
Added new 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, pos) |
5e7604c2d60d
Added new 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 >>> short_stream = HTML('<body>Some <em>test</em> text</body>') |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
219 >>> print short_stream | Transformer('.//em/text()').apply(upper) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
220 <body>Some <em>TEST</em> text</body> |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
221 """ |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
222 transformer = Transformer() |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
223 transformer.transforms = self.transforms[:] |
501
5e7604c2d60d
Added new 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 if isinstance(function, Transformer): |
504
a2dca8066a5a
Renamed 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.transforms.extend(function.transforms) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
226 else: |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
227 transformer.transforms.append(function) |
a2dca8066a5a
Renamed 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 return transformer |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
230 #{ Selection operations |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
231 |
5e7604c2d60d
Added new 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 def select(self, path): |
514
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
233 """Mark events matching the given XPath expression, within the current |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
234 selection. |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
236 >>> html = HTML('<body>Some <em>test</em> text</body>') |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
237 >>> print html | Transformer().select('.//em').trace() |
5e7604c2d60d
Added new 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 (None, ('START', (QName(u'body'), Attrs()), (None, 1, 0))) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
239 (None, ('TEXT', u'Some ', (None, 1, 6))) |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
240 ('ENTER', ('START', (QName(u'em'), Attrs()), (None, 1, 11))) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
241 ('INSIDE', ('TEXT', u'test', (None, 1, 15))) |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
242 ('EXIT', ('END', QName(u'em'), (None, 1, 19))) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
243 (None, ('TEXT', u' text', (None, 1, 24))) |
5e7604c2d60d
Added new 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 (None, ('END', QName(u'body'), (None, 1, 29))) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
245 <body>Some <em>test</em> text</body> |
5e7604c2d60d
Added new 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 |
503 | 247 :param path: an XPath expression (as string) or a `Path` instance |
501
5e7604c2d60d
Added new 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 :return: the stream augmented by transformation marks |
5e7604c2d60d
Added new 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 :rtype: `Transformer` |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
250 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
251 return self.apply(SelectTransformation(path)) |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 def invert(self): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
254 """Invert selection so that marked events become unmarked, and vice |
5e7604c2d60d
Added new 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 versa. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 Specificaly, all marks are converted to null marks, and all null marks |
5e7604c2d60d
Added new 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 are converted to OUTSIDE marks. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
260 >>> html = HTML('<body>Some <em>test</em> text</body>') |
5e7604c2d60d
Added new 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 >>> print html | Transformer('//em').invert().trace() |
5e7604c2d60d
Added new 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 ('OUTSIDE', ('START', (QName(u'body'), Attrs()), (None, 1, 0))) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
263 ('OUTSIDE', ('TEXT', u'Some ', (None, 1, 6))) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
264 (None, ('START', (QName(u'em'), Attrs()), (None, 1, 11))) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
265 (None, ('TEXT', u'test', (None, 1, 15))) |
5e7604c2d60d
Added new 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 (None, ('END', QName(u'em'), (None, 1, 19))) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
267 ('OUTSIDE', ('TEXT', u' text', (None, 1, 24))) |
5e7604c2d60d
Added new 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 ('OUTSIDE', ('END', QName(u'body'), (None, 1, 29))) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
269 <body>Some <em>test</em> text</body> |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
271 :rtype: `Transformer` |
5e7604c2d60d
Added new 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 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
273 return self.apply(InvertTransformation()) |
501
5e7604c2d60d
Added new 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 |
514
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
275 def end(self): |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
276 """End current selection, allowing all events to be selected. |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
277 |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
278 Example: |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
279 |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
280 >>> html = HTML('<body>Some <em>test</em> text</body>') |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
281 >>> print html | Transformer('//em').end().trace() |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
282 ('OUTSIDE', ('START', (QName(u'body'), Attrs()), (None, 1, 0))) |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
283 ('OUTSIDE', ('TEXT', u'Some ', (None, 1, 6))) |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
284 ('OUTSIDE', ('START', (QName(u'em'), Attrs()), (None, 1, 11))) |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
285 ('OUTSIDE', ('TEXT', u'test', (None, 1, 15))) |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
286 ('OUTSIDE', ('END', QName(u'em'), (None, 1, 19))) |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
287 ('OUTSIDE', ('TEXT', u' text', (None, 1, 24))) |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
288 ('OUTSIDE', ('END', QName(u'body'), (None, 1, 29))) |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
289 <body>Some <em>test</em> text</body> |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
290 |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
291 :return: the stream augmented by transformation marks |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
292 :rtype: `Transformer` |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
293 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
294 return self.apply(EndTransformation()) |
514
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
295 |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
296 #{ Deletion operations |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
297 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
298 def empty(self): |
5e7604c2d60d
Added new 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 """Empty selected elements of all content. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 Example: |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
5e7604c2d60d
Added new 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 ... '<body>Some <em>body</em> text.</body></html>') |
5e7604c2d60d
Added new 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 >>> print html | Transformer('.//em').empty() |
5e7604c2d60d
Added new 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><head><title>Some Title</title></head><body>Some <em/> |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
307 text.</body></html> |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
308 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
309 :rtype: `Transformer` |
5e7604c2d60d
Added new 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 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
311 return self.apply(EmptyTransformation()) |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 def remove(self): |
5e7604c2d60d
Added new 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 """Remove selection from the stream. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
315 |
5e7604c2d60d
Added new 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 Example: |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
5e7604c2d60d
Added new 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 ... '<body>Some <em>body</em> text.</body></html>') |
5e7604c2d60d
Added new 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 >>> print html | Transformer('.//em').remove() |
5e7604c2d60d
Added new 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 <html><head><title>Some Title</title></head><body>Some |
5e7604c2d60d
Added new 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 text.</body></html> |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
323 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
324 :rtype: `Transformer` |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
325 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
326 return self.apply(RemoveTransformation()) |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 #{ Direct element operations |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 def unwrap(self): |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
331 """Remove outermost enclosing elements from selection. |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 Example: |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
336 ... '<body>Some <em>body</em> text.</body></html>') |
5e7604c2d60d
Added new 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 >>> print html | Transformer('.//em').unwrap() |
5e7604c2d60d
Added new 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 <html><head><title>Some Title</title></head><body>Some body |
5e7604c2d60d
Added new 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 text.</body></html> |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
341 :rtype: `Transformer` |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
342 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
343 return self.apply(UnwrapTransformation()) |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 def wrap(self, element): |
5e7604c2d60d
Added new 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 """Wrap selection in an element. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
349 ... '<body>Some <em>body</em> text.</body></html>') |
5e7604c2d60d
Added new 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 >>> print html | Transformer('.//em').wrap('strong') |
5e7604c2d60d
Added new 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 <html><head><title>Some Title</title></head><body>Some |
5e7604c2d60d
Added new 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 <strong><em>body</em></strong> text.</body></html> |
5e7604c2d60d
Added new 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 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
354 :param element: either a tag name (as string) or an `Element` object |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
355 :rtype: `Transformer` |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
356 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
357 return self.apply(WrapTransformation(element)) |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 #{ Content insertion operations |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
361 def replace(self, content): |
5e7604c2d60d
Added new 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 """Replace selection with content. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
364 >>> html = HTML('<html><head><title>Some Title</title></head>' |
5e7604c2d60d
Added new 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 ... '<body>Some <em>body</em> text.</body></html>') |
5e7604c2d60d
Added new 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 >>> print html | Transformer('.//title/text()').replace('New Title') |
5e7604c2d60d
Added new 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 <html><head><title>New Title</title></head><body>Some <em>body</em> |
5e7604c2d60d
Added new 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 text.</body></html> |
5e7604c2d60d
Added new 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 |
744 | 370 :param content: Either a callable, an iterable of events, or a string |
371 to insert. | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
372 :rtype: `Transformer` |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
373 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
374 return self.apply(ReplaceTransformation(content)) |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 def before(self, content): |
5e7604c2d60d
Added new 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 """Insert content before selection. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
378 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
379 In this example we insert the word 'emphasised' before the <em> opening |
5e7604c2d60d
Added new 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 tag: |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
382 >>> html = HTML('<html><head><title>Some Title</title></head>' |
5e7604c2d60d
Added new 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 ... '<body>Some <em>body</em> text.</body></html>') |
5e7604c2d60d
Added new 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 >>> print html | Transformer('.//em').before('emphasised ') |
5e7604c2d60d
Added new 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 <html><head><title>Some Title</title></head><body>Some emphasised |
5e7604c2d60d
Added new 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 <em>body</em> text.</body></html> |
5e7604c2d60d
Added new 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 |
744 | 388 :param content: Either a callable, an iterable of events, or a string |
389 to insert. | |
501
5e7604c2d60d
Added new 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 :rtype: `Transformer` |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
391 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
392 return self.apply(BeforeTransformation(content)) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
393 |
5e7604c2d60d
Added new 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 def after(self, content): |
5e7604c2d60d
Added new 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 """Insert content after selection. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
397 Here, we insert some text after the </em> closing tag: |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
398 |
5e7604c2d60d
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
5e7604c2d60d
Added new 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 ... '<body>Some <em>body</em> text.</body></html>') |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
401 >>> print html | Transformer('.//em').after(' rock') |
5e7604c2d60d
Added new 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 <html><head><title>Some Title</title></head><body>Some <em>body</em> |
5e7604c2d60d
Added new 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 rock text.</body></html> |
5e7604c2d60d
Added new 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 |
744 | 405 :param content: Either a callable, an iterable of events, or a string |
406 to insert. | |
501
5e7604c2d60d
Added new 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 :rtype: `Transformer` |
5e7604c2d60d
Added new 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 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
409 return self.apply(AfterTransformation(content)) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
410 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
411 def prepend(self, content): |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
412 """Insert content after the ENTER event of the selection. |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 Inserting some new text at the start of the <body>: |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
415 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
416 >>> html = HTML('<html><head><title>Some Title</title></head>' |
5e7604c2d60d
Added new 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 ... '<body>Some <em>body</em> text.</body></html>') |
5e7604c2d60d
Added new 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 >>> print html | Transformer('.//body').prepend('Some new body text. ') |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
419 <html><head><title>Some Title</title></head><body>Some new body text. |
5e7604c2d60d
Added new 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 Some <em>body</em> text.</body></html> |
5e7604c2d60d
Added new 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 |
744 | 422 :param content: Either a callable, an iterable of events, or a string |
423 to insert. | |
501
5e7604c2d60d
Added new 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 :rtype: `Transformer` |
5e7604c2d60d
Added new 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 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
426 return self.apply(PrependTransformation(content)) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
427 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
428 def append(self, content): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
429 """Insert content before the END event of the selection. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
5e7604c2d60d
Added new 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 ... '<body>Some <em>body</em> text.</body></html>') |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
433 >>> print html | Transformer('.//body').append(' Some new body text.') |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
434 <html><head><title>Some Title</title></head><body>Some <em>body</em> |
5e7604c2d60d
Added new 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 text. Some new body text.</body></html> |
5e7604c2d60d
Added new 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 |
744 | 437 :param content: Either a callable, an iterable of events, or a string |
438 to insert. | |
501
5e7604c2d60d
Added new 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 :rtype: `Transformer` |
5e7604c2d60d
Added new 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 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
441 return self.apply(AppendTransformation(content)) |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
443 #{ Attribute manipulation |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
444 |
517
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
445 def attr(self, name, value): |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
446 """Add, replace or delete an attribute on selected elements. |
501
5e7604c2d60d
Added new 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 |
517
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
448 If `value` evaulates to `None` the attribute will be deleted from the |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
449 element: |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
450 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
451 >>> html = HTML('<html><head><title>Some Title</title></head>' |
517
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
452 ... '<body>Some <em class="before">body</em> <em>text</em>.</body>' |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
453 ... '</html>') |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
454 >>> print html | Transformer('body/em').attr('class', None) |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
455 <html><head><title>Some Title</title></head><body>Some <em>body</em> |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
456 <em>text</em>.</body></html> |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
457 |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
458 Otherwise the attribute will be set to `value`: |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
459 |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
460 >>> print html | Transformer('body/em').attr('class', 'emphasis') |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
461 <html><head><title>Some Title</title></head><body>Some <em |
517
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
462 class="emphasis">body</em> <em class="emphasis">text</em>.</body></html> |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
463 |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
464 If `value` is a callable it will be called with the attribute name and |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
465 the `START` event for the matching element. Its return value will then |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
466 be used to set the attribute: |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
467 |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
468 >>> def print_attr(name, event): |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
469 ... attrs = event[1][1] |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
470 ... print attrs |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
471 ... return attrs.get(name) |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
472 >>> print html | Transformer('body/em').attr('class', print_attr) |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
473 Attrs([(QName(u'class'), u'before')]) |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
474 Attrs() |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
475 <html><head><title>Some Title</title></head><body>Some <em |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
476 class="before">body</em> <em>text</em>.</body></html> |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
477 |
503 | 478 :param name: the name of the attribute |
517
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
479 :param value: the value that should be set for the attribute. |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
480 :rtype: `Transformer` |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
481 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
482 return self.apply(AttrTransformation(name, value)) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
483 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
484 #{ Buffer operations |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
485 |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
486 def copy(self, buffer, accumulate=False): |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
487 """Copy selection into buffer. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
488 |
744 | 489 The buffer is replaced by each *contiguous* selection before being passed |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
490 to the next transformation. If accumulate=True, further selections will |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
491 be appended to the buffer rather than replacing it. |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
492 |
501
5e7604c2d60d
Added new 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 >>> from genshi.builder import tag |
506
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
494 >>> buffer = StreamBuffer() |
501
5e7604c2d60d
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
5e7604c2d60d
Added new 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 ... '<body>Some <em>body</em> text.</body></html>') |
782
ae44bf4d1638
Fix wrong XPath example in `Transformer` docstring that breaks with the more correct XPath processing on the GSoC branch.
cmlenz
parents:
752
diff
changeset
|
497 >>> print html | Transformer('head/title/text()').copy(buffer) \\ |
514
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
498 ... .end().select('body').prepend(tag.h1(buffer)) |
509 | 499 <html><head><title>Some Title</title></head><body><h1>Some |
500 Title</h1>Some <em>body</em> text.</body></html> | |
501 | |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
502 This example illustrates that only a single contiguous selection will |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
503 be buffered: |
509 | 504 |
505 >>> print html | Transformer('head/title/text()').copy(buffer) \\ | |
514
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
506 ... .end().select('body/em').copy(buffer).end().select('body') \\ |
509 | 507 ... .prepend(tag.h1(buffer)) |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
508 <html><head><title>Some Title</title></head><body><h1>Some |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
509 Title</h1>Some <em>body</em> text.</body></html> |
509 | 510 >>> print buffer |
511 <em>body</em> | |
512 | |
519
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
513 Element attributes can also be copied for later use: |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
514 |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
515 >>> html = HTML('<html><head><title>Some Title</title></head>' |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
516 ... '<body><em>Some</em> <em class="before">body</em>' |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
517 ... '<em>text</em>.</body></html>') |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
518 >>> buffer = StreamBuffer() |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
519 >>> def apply_attr(name, entry): |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
520 ... return list(buffer)[0][1][1].get('class') |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
521 >>> print html | Transformer('body/em[@class]/@class').copy(buffer) \\ |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
522 ... .end().buffer().select('body/em[not(@class)]') \\ |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
523 ... .attr('class', apply_attr) |
519
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
524 <html><head><title>Some Title</title></head><body><em |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
525 class="before">Some</em> <em class="before">body</em><em |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
526 class="before">text</em>.</body></html> |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
527 |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
528 |
507 | 529 :param buffer: the `StreamBuffer` in which the selection should be |
530 stored | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
531 :rtype: `Transformer` |
752 | 532 :note: Copy (and cut) copy each individual selected object into the |
744 | 533 buffer before passing to the next transform. For example, the |
534 XPath ``*|text()`` will select all elements and text, each | |
535 instance of which will be copied to the buffer individually | |
536 before passing to the next transform. This has implications for | |
537 how ``StreamBuffer`` objects can be used, so some | |
538 experimentation may be required. | |
539 | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
540 """ |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
541 return self.apply(CopyTransformation(buffer, accumulate)) |
501
5e7604c2d60d
Added new 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 |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
543 def cut(self, buffer, accumulate=False): |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
544 """Copy selection into buffer and remove the selection from the stream. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
546 >>> from genshi.builder import tag |
506
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
547 >>> buffer = StreamBuffer() |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
548 >>> html = HTML('<html><head><title>Some Title</title></head>' |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
549 ... '<body>Some <em>body</em> text.</body></html>') |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
550 >>> print html | Transformer('.//em/text()').cut(buffer) \\ |
514
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
551 ... .end().select('.//em').after(tag.h1(buffer)) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
552 <html><head><title>Some Title</title></head><body>Some |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
553 <em/><h1>body</h1> text.</body></html> |
5e7604c2d60d
Added new 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
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
555 Specifying accumulate=True, appends all selected intervals onto the |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
556 buffer. Combining this with the .buffer() operation allows us operate |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
557 on all copied events rather than per-segment. See the documentation on |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
558 buffer() for more information. |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
559 |
507 | 560 :param buffer: the `StreamBuffer` in which the selection should be |
561 stored | |
501
5e7604c2d60d
Added new 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 :rtype: `Transformer` |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
563 :note: this transformation will buffer the entire input stream |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
564 """ |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
565 return self.apply(CutTransformation(buffer, accumulate)) |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
566 |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
567 def buffer(self): |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
568 """Buffer the entire stream (can consume a considerable amount of |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
569 memory). |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
570 |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
571 Useful in conjunction with copy(accumulate=True) and |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
572 cut(accumulate=True) to ensure that all marked events in the entire |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
573 stream are copied to the buffer before further transformations are |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
574 applied. |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
575 |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
576 For example, to move all <note> elements inside a <notes> tag at the |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
577 top of the document: |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
578 |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
579 >>> doc = HTML('<doc><notes></notes><body>Some <note>one</note> ' |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
580 ... 'text <note>two</note>.</body></doc>') |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
581 >>> buffer = StreamBuffer() |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
582 >>> print doc | Transformer('body/note').cut(buffer, accumulate=True) \\ |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
583 ... .end().buffer().select('notes').prepend(buffer) |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
584 <doc><notes><note>one</note><note>two</note></notes><body>Some text |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
585 .</body></doc> |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
586 |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
587 """ |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
588 return self.apply(list) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
589 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
590 #{ Miscellaneous operations |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
591 |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
592 def filter(self, filter): |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
593 """Apply a normal stream filter to the selection. The filter is called |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
594 once for each contiguous block of marked events. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
595 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
596 >>> from genshi.filters.html import HTMLSanitizer |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
597 >>> html = HTML('<html><body>Some text<script>alert(document.cookie)' |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
598 ... '</script> and some more text</body></html>') |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
599 >>> print html | Transformer('body/*').filter(HTMLSanitizer()) |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
600 <html><body>Some text and some more text</body></html> |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
601 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
602 :param filter: The stream filter to apply. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
603 :rtype: `Transformer` |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
604 """ |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
605 return self.apply(FilterTransformation(filter)) |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
606 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
607 def map(self, function, kind): |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
608 """Applies a function to the ``data`` element of events of ``kind`` in |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
609 the selection. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
610 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
611 >>> html = HTML('<html><head><title>Some Title</title></head>' |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
612 ... '<body>Some <em>body</em> text.</body></html>') |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
613 >>> print html | Transformer('head/title').map(unicode.upper, TEXT) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
614 <html><head><title>SOME TITLE</title></head><body>Some <em>body</em> |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
615 text.</body></html> |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
616 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
617 :param function: the function to apply |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
618 :param kind: the kind of event the function should be applied to |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
619 :rtype: `Transformer` |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
620 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
621 return self.apply(MapTransformation(function, kind)) |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
622 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
623 def substitute(self, pattern, replace, count=1): |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
624 """Replace text matching a regular expression. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
625 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
626 Refer to the documentation for ``re.sub()`` for details. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
627 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
628 >>> html = HTML('<html><body>Some text, some more text and ' |
728 | 629 ... '<b>some bold text</b>\\n' |
630 ... '<i>some italicised text</i></body></html>') | |
631 >>> print html | Transformer('body/b').substitute('(?i)some', 'SOME') | |
632 <html><body>Some text, some more text and <b>SOME bold text</b> | |
633 <i>some italicised text</i></body></html> | |
634 >>> tags = tag.html(tag.body('Some text, some more text and\\n', | |
668 | 635 ... Markup('<b>some bold text</b>'))) |
728 | 636 >>> print tags.generate() | Transformer('body').substitute( |
637 ... '(?i)some', 'SOME') | |
638 <html><body>SOME text, some more text and | |
639 <b>SOME bold text</b></body></html> | |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
640 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
641 :param pattern: A regular expression object or string. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
642 :param replace: Replacement pattern. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
643 :param count: Number of replacements to make in each text fragment. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
644 :rtype: `Transformer` |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
645 """ |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
646 return self.apply(SubstituteTransformation(pattern, replace, count)) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
647 |
578 | 648 def rename(self, name): |
649 """Rename matching elements. | |
650 | |
651 >>> html = HTML('<html><body>Some text, some more text and ' | |
652 ... '<b>some bold text</b></body></html>') | |
653 >>> print html | Transformer('body/b').rename('strong') | |
654 <html><body>Some text, some more text and <strong>some bold text</strong></body></html> | |
655 """ | |
656 return self.apply(RenameTransformation(name)) | |
657 | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
658 def trace(self, prefix='', fileobj=None): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
659 """Print events as they pass through the transform. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
660 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
661 >>> html = HTML('<body>Some <em>test</em> text</body>') |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
662 >>> print html | Transformer('em').trace() |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
663 (None, ('START', (QName(u'body'), Attrs()), (None, 1, 0))) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
664 (None, ('TEXT', u'Some ', (None, 1, 6))) |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
665 ('ENTER', ('START', (QName(u'em'), Attrs()), (None, 1, 11))) |
501
5e7604c2d60d
Added new 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 ('INSIDE', ('TEXT', u'test', (None, 1, 15))) |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
667 ('EXIT', ('END', QName(u'em'), (None, 1, 19))) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
668 (None, ('TEXT', u' text', (None, 1, 24))) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
669 (None, ('END', QName(u'body'), (None, 1, 29))) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
670 <body>Some <em>test</em> text</body> |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
671 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
672 :param prefix: a string to prefix each event with in the output |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
673 :param fileobj: the writable file-like object to write to; defaults to |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
674 the standard output stream |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
675 :rtype: `Transformer` |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
676 """ |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
677 return self.apply(TraceTransformation(prefix, fileobj=fileobj)) |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 # Internal methods |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
681 def _mark(self, stream): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
682 for event in stream: |
514
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
683 yield OUTSIDE, event |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
685 def _unmark(self, stream): |
5e7604c2d60d
Added new 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 for mark, event in stream: |
744 | 687 kind = event[0] |
688 if not (kind is None or kind is ATTR or kind is BREAK): | |
519
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
689 yield event |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
692 class SelectTransformation(object): |
501
5e7604c2d60d
Added new 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 """Select and mark events that match an XPath expression.""" |
503 | 694 |
501
5e7604c2d60d
Added new 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 def __init__(self, path): |
5e7604c2d60d
Added new 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 """Create selection. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
697 |
503 | 698 :param path: an XPath expression (as string) or a `Path` object |
501
5e7604c2d60d
Added new 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 """ |
503 | 700 if not isinstance(path, Path): |
701 path = Path(path) | |
702 self.path = path | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
703 |
5e7604c2d60d
Added new 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 def __call__(self, stream): |
5e7604c2d60d
Added new 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 """Apply the transform filter to the marked stream. |
5e7604c2d60d
Added new 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 |
503 | 707 :param stream: the marked event stream to filter |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
708 """ |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
709 namespaces = {} |
5e7604c2d60d
Added new 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 variables = {} |
5e7604c2d60d
Added new 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 test = self.path.test() |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
712 stream = iter(stream) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
713 for mark, event in stream: |
514
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
714 if mark is None: |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
715 yield mark, event |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
716 continue |
501
5e7604c2d60d
Added new 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 result = test(event, {}, {}) |
519
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
718 # XXX This is effectively genshi.core._ensure() for transform |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
719 # streams. |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
720 if result is True: |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
721 if event[0] is START: |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
722 yield ENTER, event |
501
5e7604c2d60d
Added new 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 depth = 1 |
5e7604c2d60d
Added new 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 while depth > 0: |
5e7604c2d60d
Added new 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 mark, subevent = stream.next() |
5e7604c2d60d
Added new 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 if subevent[0] is START: |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
727 depth += 1 |
5e7604c2d60d
Added new 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 elif subevent[0] is END: |
5e7604c2d60d
Added new 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 depth -= 1 |
5e7604c2d60d
Added new 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 if depth == 0: |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
731 yield EXIT, subevent |
501
5e7604c2d60d
Added new 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 else: |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
733 yield INSIDE, subevent |
5e7604c2d60d
Added new 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 test(subevent, {}, {}, updateonly=True) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
735 else: |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
736 yield OUTSIDE, event |
519
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
737 elif isinstance(result, Attrs): |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
738 # XXX Selected *attributes* are given a "kind" of None to |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
739 # indicate they are not really part of the stream. |
744 | 740 yield ATTR, (ATTR, (QName(event[1][0] + '@*'), result), event[2]) |
519
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
741 yield None, event |
744 | 742 elif isinstance(result, tuple): |
743 yield OUTSIDE, result | |
501
5e7604c2d60d
Added new 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 elif result: |
744 | 745 # XXX Assume everything else is "text"? |
519
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
746 yield None, (TEXT, unicode(result), (None, -1, -1)) |
501
5e7604c2d60d
Added new 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 else: |
5e7604c2d60d
Added new 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 yield None, event |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
751 class InvertTransformation(object): |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
752 """Invert selection so that marked events become unmarked, and vice versa. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 Specificaly, all input marks are converted to null marks, and all input |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
755 null marks are converted to OUTSIDE marks. |
5e7604c2d60d
Added new 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 """ |
5e7604c2d60d
Added new 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 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
758 def __call__(self, stream): |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
759 """Apply the transform filter to the marked stream. |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
760 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
761 :param stream: the marked event stream to filter |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
762 """ |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
763 for mark, event in stream: |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
764 if mark: |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
765 yield None, event |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
766 else: |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
767 yield OUTSIDE, event |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 |
514
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
770 class EndTransformation(object): |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
771 """End the current selection.""" |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
772 |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
773 def __call__(self, stream): |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
774 """Apply the transform filter to the marked stream. |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
775 |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
776 :param stream: the marked event stream to filter |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
777 """ |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
778 for mark, event in stream: |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
779 yield OUTSIDE, event |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
780 |
5e7db1a72772
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
781 |
504
a2dca8066a5a
Renamed 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 class EmptyTransformation(object): |
a2dca8066a5a
Renamed 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 """Empty selected elements of all content.""" |
a2dca8066a5a
Renamed 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 |
a2dca8066a5a
Renamed 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 def __call__(self, stream): |
a2dca8066a5a
Renamed 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 """Apply the transform filter to the marked stream. |
a2dca8066a5a
Renamed 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 |
a2dca8066a5a
Renamed 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 :param stream: the marked event stream to filter |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
789 """ |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
790 for mark, event in stream: |
744 | 791 yield mark, event |
792 if mark is ENTER: | |
793 for mark, event in stream: | |
794 if mark is EXIT: | |
795 yield mark, event | |
796 break | |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
797 |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
798 |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
799 class RemoveTransformation(object): |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
800 """Remove selection from the stream.""" |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
801 |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
802 def __call__(self, stream): |
a2dca8066a5a
Renamed 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 """Apply the transform filter to the marked stream. |
a2dca8066a5a
Renamed 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 |
a2dca8066a5a
Renamed 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 :param stream: the marked event stream to filter |
a2dca8066a5a
Renamed 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 """ |
a2dca8066a5a
Renamed 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 for mark, event in stream: |
a2dca8066a5a
Renamed 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 if mark is None: |
a2dca8066a5a
Renamed 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 yield mark, event |
a2dca8066a5a
Renamed 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 |
a2dca8066a5a
Renamed 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 |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
812 class UnwrapTransformation(object): |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
813 """Remove outtermost enclosing elements from selection.""" |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
814 |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
815 def __call__(self, stream): |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
816 """Apply the transform filter to the marked stream. |
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
817 |
a2dca8066a5a
Renamed 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 :param stream: the marked event stream to filter |
a2dca8066a5a
Renamed 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 """ |
a2dca8066a5a
Renamed 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 for mark, event in stream: |
a2dca8066a5a
Renamed 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 if mark not in (ENTER, EXIT): |
a2dca8066a5a
Renamed 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 yield mark, event |
a2dca8066a5a
Renamed 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 |
a2dca8066a5a
Renamed 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 |
a2dca8066a5a
Renamed 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 class WrapTransformation(object): |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
826 """Wrap selection in an element.""" |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
827 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
828 def __init__(self, element): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
829 if isinstance(element, Element): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
830 self.element = element |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
831 else: |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
832 self.element = Element(element) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
833 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
834 def __call__(self, stream): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
835 for mark, event in stream: |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
836 if mark: |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
837 element = list(self.element.generate()) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
838 for prefix in element[:-1]: |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
839 yield None, prefix |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
840 yield mark, event |
744 | 841 start = mark |
842 stopped = False | |
843 for mark, event in stream: | |
844 if start is ENTER and mark is EXIT: | |
845 yield mark, event | |
846 stopped = True | |
847 break | |
501
5e7604c2d60d
Added new 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 if not mark: |
5e7604c2d60d
Added new 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 break |
5e7604c2d60d
Added new 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 yield mark, event |
744 | 851 else: |
852 stopped = True | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
853 yield None, element[-1] |
744 | 854 if not stopped: |
855 yield mark, event | |
501
5e7604c2d60d
Added new 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 else: |
5e7604c2d60d
Added new 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 yield mark, event |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
860 class TraceTransformation(object): |
501
5e7604c2d60d
Added new 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 """Print events as they pass through the transform.""" |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
862 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
863 def __init__(self, prefix='', fileobj=None): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
864 """Trace constructor. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
865 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
866 :param prefix: text to prefix each traced line with. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
867 :param fileobj: the writable file-like object to write to |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
868 """ |
5e7604c2d60d
Added new 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 self.prefix = prefix |
5e7604c2d60d
Added new 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 self.fileobj = fileobj or sys.stdout |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
872 def __call__(self, stream): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
873 """Apply the transform filter to the marked stream. |
5e7604c2d60d
Added new 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 |
503 | 875 :param stream: the marked event stream to filter |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
876 """ |
5e7604c2d60d
Added new 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 for event in stream: |
5e7604c2d60d
Added new 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 print>>self.fileobj, self.prefix + str(event) |
5e7604c2d60d
Added new 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 yield event |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
881 |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
882 class FilterTransformation(object): |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
883 """Apply a normal stream filter to the selection. The filter is called once |
744 | 884 for each selection.""" |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
885 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
886 def __init__(self, filter): |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
887 """Create the transform. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
888 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
889 :param filter: The stream filter to apply. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
890 """ |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
891 self.filter = filter |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
892 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
893 def __call__(self, stream): |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
894 """Apply the transform filter to the marked stream. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
895 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
896 :param stream: The marked event stream to filter |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
897 """ |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
898 def flush(queue): |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
899 if queue: |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
900 for event in self.filter(queue): |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
901 yield OUTSIDE, event |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
902 del queue[:] |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
903 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
904 queue = [] |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
905 for mark, event in stream: |
744 | 906 if mark is ENTER: |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
907 queue.append(event) |
744 | 908 for mark, event in stream: |
909 queue.append(event) | |
910 if mark is EXIT: | |
911 break | |
577
e86edca143fb
Fix for #136, where transformer marks were not being stripped correctly when
athomas
parents:
576
diff
changeset
|
912 for queue_event in flush(queue): |
e86edca143fb
Fix for #136, where transformer marks were not being stripped correctly when
athomas
parents:
576
diff
changeset
|
913 yield queue_event |
744 | 914 elif mark is OUTSIDE: |
915 stopped = True | |
916 queue.append(event) | |
917 for mark, event in stream: | |
918 if mark is not OUTSIDE: | |
919 break | |
920 queue.append(event) | |
921 else: | |
922 stopped = True | |
923 for queue_event in flush(queue): | |
924 yield queue_event | |
925 if not stopped: | |
926 yield None, event | |
927 else: | |
928 yield mark, event | |
929 for queue_event in flush(queue): | |
930 yield queue_event | |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
931 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
932 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
933 class MapTransformation(object): |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
934 """Apply a function to the `data` element of events of ``kind`` in the |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
935 selection. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
936 """ |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
937 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
938 def __init__(self, function, kind): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
939 """Create the transform. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
940 |
503 | 941 :param function: the function to apply; the function must take one |
942 argument, the `data` element of each selected event | |
943 :param kind: the stream event ``kind`` to apply the `function` to | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
944 """ |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
945 self.function = function |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
946 self.kind = kind |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
947 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
948 def __call__(self, stream): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
949 """Apply the transform filter to the marked stream. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
950 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
951 :param stream: The marked event stream to filter |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
952 """ |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
953 for mark, (kind, data, pos) in stream: |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
954 if mark and self.kind in (None, kind): |
501
5e7604c2d60d
Added new 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 yield mark, (kind, self.function(data), pos) |
5e7604c2d60d
Added new 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 else: |
5e7604c2d60d
Added new 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 yield mark, (kind, data, pos) |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
960 class SubstituteTransformation(object): |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
961 """Replace text matching a regular expression. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
962 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
963 Refer to the documentation for ``re.sub()`` for details. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
964 """ |
744 | 965 def __init__(self, pattern, replace, count=0): |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
966 """Create the transform. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
967 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
968 :param pattern: A regular expression object, or string. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
969 :param replace: Replacement pattern. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
970 :param count: Number of replacements to make in each text fragment. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
971 """ |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
972 if isinstance(pattern, basestring): |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
973 self.pattern = re.compile(pattern) |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
974 else: |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
975 self.pattern = pattern |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
976 self.count = count |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
977 self.replace = replace |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
978 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
979 def __call__(self, stream): |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
980 """Apply the transform filter to the marked stream. |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
981 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
982 :param stream: The marked event stream to filter |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
983 """ |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
984 for mark, (kind, data, pos) in stream: |
728 | 985 if mark is not None and kind is TEXT: |
668 | 986 new_data = self.pattern.sub(self.replace, data, self.count) |
987 if isinstance(data, Markup): | |
988 data = Markup(new_data) | |
989 else: | |
990 data = new_data | |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
991 yield mark, (kind, data, pos) |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
992 |
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
993 |
578 | 994 class RenameTransformation(object): |
995 """Rename matching elements.""" | |
996 def __init__(self, name): | |
997 """Create the transform. | |
998 | |
999 :param name: New element name. | |
1000 """ | |
1001 self.name = QName(name) | |
1002 | |
1003 def __call__(self, stream): | |
1004 """Apply the transform filter to the marked stream. | |
1005 | |
1006 :param stream: The marked event stream to filter | |
1007 """ | |
1008 for mark, (kind, data, pos) in stream: | |
1009 if mark is ENTER: | |
1010 data = self.name, data[1] | |
1011 elif mark is EXIT: | |
1012 data = self.name | |
1013 yield mark, (kind, data, pos) | |
1014 | |
1015 | |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
1016 class InjectorTransformation(object): |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1017 """Abstract base class for transformations that inject content into a |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1018 stream. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1019 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
1020 >>> class Top(InjectorTransformation): |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1021 ... def __call__(self, stream): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1022 ... for event in self._inject(): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1023 ... yield event |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1024 ... for event in stream: |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1025 ... yield event |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1026 >>> html = HTML('<body>Some <em>test</em> text</body>') |
533
4d486f15c986
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
1027 >>> print html | Transformer('.//em').apply(Top('Prefix ')) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1028 Prefix <body>Some <em>test</em> text</body> |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1029 """ |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1030 def __init__(self, content): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1031 """Create a new injector. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1032 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1033 :param content: An iterable of Genshi stream events, or a string to be |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1034 injected. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1035 """ |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1036 self.content = content |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1037 |
5e7604c2d60d
Added new 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 def _inject(self): |
744 | 1039 content = self.content |
1040 if callable(content): | |
1041 content = content() | |
1042 for event in _ensure(content): | |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
1043 yield None, event |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
1046 class ReplaceTransformation(InjectorTransformation): |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1047 """Replace selection with content.""" |
503 | 1048 |
501
5e7604c2d60d
Added new 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 def __call__(self, stream): |
5e7604c2d60d
Added new 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 """Apply the transform filter to the marked stream. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 :param stream: The marked event stream to filter |
5e7604c2d60d
Added new 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 """ |
744 | 1054 stream = PushBackStream(stream) |
501
5e7604c2d60d
Added new 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 for mark, event in stream: |
5e7604c2d60d
Added new 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 if mark is not None: |
744 | 1057 start = mark |
501
5e7604c2d60d
Added new 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 for subevent in self._inject(): |
5e7604c2d60d
Added new 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 yield subevent |
744 | 1060 for mark, event in stream: |
1061 if start is ENTER: | |
1062 if mark is EXIT: | |
1063 break | |
1064 elif mark != start: | |
1065 stream.push((mark, event)) | |
501
5e7604c2d60d
Added new 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 break |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1067 else: |
5e7604c2d60d
Added new 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 yield mark, event |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1069 |
5e7604c2d60d
Added new 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 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
1071 class BeforeTransformation(InjectorTransformation): |
501
5e7604c2d60d
Added new 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 """Insert content before selection.""" |
503 | 1073 |
501
5e7604c2d60d
Added new 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 def __call__(self, stream): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1075 """Apply the transform filter to the marked stream. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 :param stream: The marked event stream to filter |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1078 """ |
744 | 1079 stream = PushBackStream(stream) |
501
5e7604c2d60d
Added new 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 for mark, event in stream: |
575
d2e7fc367384
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
1081 if mark is not None: |
744 | 1082 start = mark |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1083 for subevent in self._inject(): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1084 yield subevent |
575
d2e7fc367384
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
1085 yield mark, event |
744 | 1086 for mark, event in stream: |
1087 if mark != start and start is not ENTER: | |
1088 stream.push((mark, event)) | |
575
d2e7fc367384
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
1089 break |
d2e7fc367384
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
1090 yield mark, event |
744 | 1091 if start is ENTER and mark is EXIT: |
1092 break | |
1093 else: | |
1094 yield mark, event | |
501
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
1097 class AfterTransformation(InjectorTransformation): |
501
5e7604c2d60d
Added new 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 """Insert content after selection.""" |
503 | 1099 |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1100 def __call__(self, stream): |
5e7604c2d60d
Added new 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 """Apply the transform filter to the marked stream. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1102 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1103 :param stream: The marked event stream to filter |
5e7604c2d60d
Added new 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 """ |
744 | 1105 stream = PushBackStream(stream) |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1106 for mark, event in stream: |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1107 yield mark, event |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1108 if mark: |
744 | 1109 start = mark |
1110 for mark, event in stream: | |
1111 if start is not ENTER and mark != start: | |
1112 stream.push((mark, event)) | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1113 break |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1114 yield mark, event |
744 | 1115 if start is ENTER and mark is EXIT: |
1116 break | |
501
5e7604c2d60d
Added new 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 for subevent in self._inject(): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1118 yield subevent |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1120 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
1121 class PrependTransformation(InjectorTransformation): |
501
5e7604c2d60d
Added new 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 """Prepend content to the inside of selected elements.""" |
503 | 1123 |
501
5e7604c2d60d
Added new 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 def __call__(self, stream): |
5e7604c2d60d
Added new 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 """Apply the transform filter to the marked stream. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1126 |
5e7604c2d60d
Added new 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 :param stream: The marked event stream to filter |
5e7604c2d60d
Added new 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 """ |
5e7604c2d60d
Added new 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 for mark, event in stream: |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1130 yield mark, event |
744 | 1131 if mark is ENTER: |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1132 for subevent in self._inject(): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1133 yield subevent |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
1136 class AppendTransformation(InjectorTransformation): |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1137 """Append content after the content of selected elements.""" |
503 | 1138 |
501
5e7604c2d60d
Added new 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 def __call__(self, stream): |
5e7604c2d60d
Added new 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 """Apply the transform filter to the marked stream. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1142 :param stream: The marked event stream to filter |
5e7604c2d60d
Added new 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 """ |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1144 for mark, event in stream: |
5e7604c2d60d
Added new 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 yield mark, event |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
1146 if mark is ENTER: |
744 | 1147 for mark, event in stream: |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
1148 if mark is EXIT: |
501
5e7604c2d60d
Added new 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 break |
5e7604c2d60d
Added new 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 yield mark, event |
5e7604c2d60d
Added new 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 for subevent in self._inject(): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1152 yield subevent |
5e7604c2d60d
Added new 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 yield mark, event |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 |
517
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1156 class AttrTransformation(object): |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1157 """Set an attribute on selected elements.""" |
503 | 1158 |
1159 def __init__(self, name, value): | |
501
5e7604c2d60d
Added new 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 """Construct transform. |
5e7604c2d60d
Added new 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 |
503 | 1162 :param name: name of the attribute that should be set |
1163 :param value: the value to set | |
501
5e7604c2d60d
Added new 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 """ |
503 | 1165 self.name = name |
501
5e7604c2d60d
Added new 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 self.value = value |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1167 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1168 def __call__(self, stream): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1169 """Apply the transform filter to the marked stream. |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 :param stream: The marked event stream to filter |
5e7604c2d60d
Added new 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 """ |
517
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1173 callable_value = callable(self.value) |
501
5e7604c2d60d
Added new 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 for mark, (kind, data, pos) in stream: |
502
11842b2d5941
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
1175 if mark is ENTER: |
517
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1176 if callable_value: |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1177 value = self.value(self.name, (kind, data, pos)) |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1178 else: |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1179 value = self.value |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1180 if value is None: |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1181 attrs = data[1] - [QName(self.name)] |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1182 else: |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1183 attrs = data[1] | [(QName(self.name), value)] |
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1184 data = (data[0], attrs) |
501
5e7604c2d60d
Added new 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 yield mark, (kind, data, pos) |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1186 |
5e7604c2d60d
Added new 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 |
5e7604c2d60d
Added new 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 |
506
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1189 class StreamBuffer(Stream): |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1190 """Stream event buffer used for cut and copy transformations.""" |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1191 |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1192 def __init__(self): |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1193 """Create the buffer.""" |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1194 Stream.__init__(self, []) |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1195 |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1196 def append(self, event): |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1197 """Add an event to the buffer. |
517
df5b79f27e9d
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1198 |
506
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1199 :param event: the markup event to add |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1200 """ |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1201 self.events.append(event) |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1202 |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1203 def reset(self): |
744 | 1204 """Empty the buffer of events.""" |
506
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1205 del self.events[:] |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1206 |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1207 |
504
a2dca8066a5a
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
1208 class CopyTransformation(object): |
501
5e7604c2d60d
Added new 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 """Copy selected events into a buffer for later insertion.""" |
503 | 1210 |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1211 def __init__(self, buffer, accumulate=False): |
506
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1212 """Create the copy transformation. |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1213 |
506
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1214 :param buffer: the `StreamBuffer` in which the selection should be |
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1215 stored |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1216 """ |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1217 if not accumulate: |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1218 buffer.reset() |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1219 self.buffer = buffer |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1220 self.accumulate = accumulate |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1221 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1222 def __call__(self, stream): |
506
0ea38a6cf173
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1223 """Apply the transformation to the marked stream. |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1224 |
503 | 1225 :param stream: the marked event stream to filter |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1226 """ |
744 | 1227 stream = PushBackStream(stream) |
1228 | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1229 for mark, event in stream: |
5e7604c2d60d
Added new 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 if mark: |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1231 if not self.accumulate: |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1232 self.buffer.reset() |
744 | 1233 events = [(mark, event)] |
1234 self.buffer.append(event) | |
1235 start = mark | |
1236 for mark, event in stream: | |
1237 if start is not ENTER and mark != start: | |
1238 stream.push((mark, event)) | |
1239 break | |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1240 events.append((mark, event)) |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1241 self.buffer.append(event) |
744 | 1242 if start is ENTER and mark is EXIT: |
1243 break | |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1244 for i in events: |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1245 yield i |
744 | 1246 else: |
1247 yield mark, event | |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1248 |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1249 |
519
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1250 class CutTransformation(object): |
501
5e7604c2d60d
Added new 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 """Cut selected events into a buffer for later insertion and remove the |
503 | 1252 selection. |
1253 """ | |
1254 | |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1255 def __init__(self, buffer, accumulate=False): |
519
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1256 """Create the cut transformation. |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1257 |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1258 :param buffer: the `StreamBuffer` in which the selection should be |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1259 stored |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1260 """ |
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1261 self.buffer = buffer |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1262 self.accumulate = accumulate |
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1263 |
519
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1264 |
501
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1265 def __call__(self, stream): |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1266 """Apply the transform filter to the marked stream. |
5e7604c2d60d
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1267 |
503 | 1268 :param stream: the marked event stream to filter |
501
5e7604c2d60d
Added new 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 """ |
744 | 1270 attributes = [] |
1271 stream = PushBackStream(stream) | |
1272 broken = False | |
1273 if not self.accumulate: | |
1274 self.buffer.reset() | |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1275 for mark, event in stream: |
519
9e11fa7f4603
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1276 if mark: |
744 | 1277 # Send a BREAK event if there was no other event sent between |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1278 if not self.accumulate: |
744 | 1279 if not broken and self.buffer: |
1280 yield BREAK, (BREAK, None, None) | |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1281 self.buffer.reset() |
744 | 1282 self.buffer.append(event) |
1283 start = mark | |
1284 if mark is ATTR: | |
1285 attributes.extend([name for name, _ in event[1][1]]) | |
1286 for mark, event in stream: | |
1287 if start is mark is ATTR: | |
1288 attributes.extend([name for name, _ in event[1][1]]) | |
1289 # Handle non-element contiguous selection | |
1290 if start is not ENTER and mark != start: | |
1291 # Operating on the attributes of a START event | |
1292 if start is ATTR: | |
1293 kind, data, pos = event | |
1294 assert kind is START | |
1295 data = (data[0], data[1] - attributes) | |
1296 attributes = None | |
1297 stream.push((mark, (kind, data, pos))) | |
1298 else: | |
1299 stream.push((mark, event)) | |
1300 break | |
734
ea2566b2f226
Fixed some unintuitive behaviour in `Transformer.{cut,copy}`.
athomas
parents:
728
diff
changeset
|
1301 self.buffer.append(event) |
744 | 1302 if start is ENTER and mark is EXIT: |
1303 break | |
1304 broken = False | |
1305 else: | |
1306 broken = True | |
1307 yield mark, event | |
1308 if not broken and self.buffer: | |
1309 yield BREAK, (BREAK, None, None) |