Mercurial > genshi > genshi-test
annotate genshi/filters/transform.py @ 605:bc5faca93699
Text templates now default to rendering as plain text; it is no longer necessary to explicitly specify the "text" method to the `render()` or `serialize()` method of the generated markup stream. See tickets #62 and #118.
author | cmlenz |
---|---|
date | Mon, 27 Aug 2007 19:04:20 +0000 |
parents | ba660d6032d7 |
children | ee48a06a16d6 |
rev | line source |
---|---|
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
2 # |
531 | 3 # Copyright (C) 2007 Edgewall Software |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
4 # All rights reserved. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
5 # |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
6 # This software is licensed as described in the file COPYING, which |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
7 # you should have received as part of this distribution. The terms |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
8 # are also available at http://genshi.edgewall.org/wiki/License. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
9 # |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
10 # This software consists of voluntary contributions made by many |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
11 # individuals. For the exact contribution history, see the revision |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
12 # history and logs, available at http://genshi.edgewall.org/log/. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
13 |
503 | 14 """A filter for functional-style transformations of markup streams. |
15 | |
16 The `Transformer` filter provides a variety of transformations that can be | |
17 applied to parts of streams that match given XPath expressions. These | |
18 transformations can be chained to achieve results that would be comparitively | |
19 tedious to achieve by writing stream filters by hand. The approach of chaining | |
20 node selection and transformation has been inspired by the `jQuery`_ Javascript | |
21 library. | |
22 | |
23 .. _`jQuery`: http://jquery.com/ | |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
24 |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
25 For example, the following transformation removes the ``<title>`` element from |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
26 the ``<head>`` of the input document: |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
27 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
28 >>> from genshi.builder import tag |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
29 >>> html = HTML('''<html> |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
30 ... <head><title>Some Title</title></head> |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
31 ... <body> |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
32 ... Some <em>body</em> text. |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
33 ... </body> |
2db10ef79e66
Renamed 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
dfb45908fadc
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
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
36 ... .unwrap().wrap(tag.u) |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
37 <html> |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
38 <head><title>Some Title</title></head> |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
39 <body> |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
40 Some <u>BODY</u> text. |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
41 </body> |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
42 </html> |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
43 |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
44 The ``Transformer`` support a large number of useful transformations out of the |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
45 box, but custom transformations can be added easily. |
576 | 46 |
47 :since: version 0.5 | |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
48 """ |
3073ac688651
Added new 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
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
50 import re |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
51 import sys |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
52 |
3073ac688651
Added new 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 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
54 from genshi.core import Stream, Attrs, QName, TEXT, START, END, _ensure |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
55 from genshi.path import Path |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
56 |
518
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
517
diff
changeset
|
57 __all__ = ['Transformer', 'StreamBuffer', 'InjectorTransformation', 'ENTER', |
97cdadc17e20
Attributes selected with an XPath are now returned as an `Attrs()` object in
athomas
parents:
517
diff
changeset
|
58 'EXIT', 'INSIDE', 'OUTSIDE'] |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
59 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
60 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
61 class TransformMark(str): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
62 """A mark on a transformation stream.""" |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
63 __slots__ = [] |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
64 _instances = {} |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
65 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
66 def __new__(cls, val): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
67 return cls._instances.setdefault(val, str.__new__(cls, val)) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
68 |
3073ac688651
Added new 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
53b478e3f3e2
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
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
72 entered.""" |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
73 |
501
3073ac688651
Added new 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
53b478e3f3e2
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
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
77 |
501
3073ac688651
Added new 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
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
81 |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
82 ATTR = TransformMark('ATTR') |
9f1d90d6abd4
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
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
84 |
502
53b478e3f3e2
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
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
87 exited.""" |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
88 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
89 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
90 class Transformer(object): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
91 """Stream filter that can apply a variety of different transformations to |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
92 a stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
93 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
94 This is achieved by selecting the events to be transformed using XPath, |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
95 then applying the transformations to the events matched by the path |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
96 expression. Each marked event is in the form (mark, (kind, data, pos)), |
503 | 97 where mark can be any of `ENTER`, `INSIDE`, `EXIT`, `OUTSIDE`, or `None`. |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
98 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
99 The first three marks match `START` and `END` events, and any events |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
100 contained `INSIDE` any selected XML/HTML element. A non-element match |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
101 outside a `START`/`END` container (e.g. ``text()``) will yield an `OUTSIDE` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
102 mark. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
103 |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
104 >>> html = HTML('<html><head><title>Some Title</title></head>' |
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
105 ... '<body>Some <em>body</em> text.</body></html>') |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
106 |
503 | 107 Transformations act on selected stream events matching an XPath expression. |
108 Here's an example of removing some markup (the title, in this case) | |
109 selected by an expression: | |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
110 |
503 | 111 >>> print html | Transformer('head/title').remove() |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
112 <html><head/><body>Some <em>body</em> text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
113 |
503 | 114 Inserted content can be passed in the form of a string, or a markup event |
115 stream, which includes streams generated programmatically via the | |
116 `builder` module: | |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
117 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
118 >>> from genshi.builder import tag |
503 | 119 >>> print html | Transformer('body').prepend(tag.h1('Document Title')) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
120 <html><head><title>Some Title</title></head><body><h1>Document |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
121 Title</h1>Some <em>body</em> text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
122 |
3073ac688651
Added new 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 Each XPath expression determines the set of tags that will be acted upon by |
503 | 124 subsequent transformations. In this example we select the ``<title>`` text, |
125 copy it into a buffer, then select the ``<body>`` element and paste the | |
126 copied text into the body as ``<h1>`` enclosed text: | |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
127 |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
128 >>> buffer = StreamBuffer() |
503 | 129 >>> print html | Transformer('head/title/text()').copy(buffer) \\ |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
130 ... .end().select('body').prepend(tag.h1(buffer)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
131 <html><head><title>Some Title</title></head><body><h1>Some Title</h1>Some |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
132 <em>body</em> text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
133 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
134 Transformations can also be assigned and reused, although care must be |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
135 taken when using buffers, to ensure that buffers are cleared between |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
136 transforms: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
137 |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
138 >>> emphasis = Transformer('body//em').attr('class', 'emphasis') |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
139 >>> print html | emphasis |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
140 <html><head><title>Some Title</title></head><body>Some <em |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
141 class="emphasis">body</em> text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
142 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
143 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
144 __slots__ = ['transforms'] |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
145 |
578 | 146 def __init__(self, path='.'): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
147 """Construct a new transformation filter. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
148 |
503 | 149 :param path: an XPath expression (as string) or a `Path` instance |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
150 """ |
575
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
151 self.transforms = [SelectTransformation(path)] |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
152 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
153 def __call__(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
154 """Apply the transform filter to the marked stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
155 |
503 | 156 :param stream: the marked event stream to filter |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
157 :return: the transformed stream |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
158 :rtype: `Stream` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
159 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
160 transforms = self._mark(stream) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
161 for link in self.transforms: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
162 transforms = link(transforms) |
605
bc5faca93699
Text templates now default to rendering as plain text; it is no longer necessary to explicitly specify the "text" method to the `render()` or `serialize()` method of the generated markup stream. See tickets #62 and #118.
cmlenz
parents:
578
diff
changeset
|
163 return Stream(self._unmark(transforms), |
bc5faca93699
Text templates now default to rendering as plain text; it is no longer necessary to explicitly specify the "text" method to the `render()` or `serialize()` method of the generated markup stream. See tickets #62 and #118.
cmlenz
parents:
578
diff
changeset
|
164 serializer=getattr(stream, 'serializer', None)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
165 |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
166 def apply(self, function): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
167 """Apply a transformation to the stream. |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
168 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
169 Transformations can be chained, similar to stream filters. Any callable |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
170 accepting a marked stream can be used as a transform. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
171 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
172 As an example, here is a simple `TEXT` event upper-casing transform: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
173 |
3073ac688651
Added new 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 >>> def upper(stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
175 ... for mark, (kind, data, pos) in stream: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
176 ... if mark and kind is TEXT: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
177 ... yield mark, (kind, data.upper(), pos) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
178 ... else: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
179 ... yield mark, (kind, data, pos) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
180 >>> short_stream = HTML('<body>Some <em>test</em> text</body>') |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
181 >>> print short_stream | Transformer('.//em/text()').apply(upper) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
182 <body>Some <em>TEST</em> text</body> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
183 """ |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
184 transformer = Transformer() |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
185 transformer.transforms = self.transforms[:] |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
186 if isinstance(function, Transformer): |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
187 transformer.transforms.extend(function.transforms) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
188 else: |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
189 transformer.transforms.append(function) |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
190 return transformer |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
191 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
192 #{ Selection operations |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
193 |
3073ac688651
Added new 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 def select(self, path): |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
195 """Mark events matching the given XPath expression, within the current |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
196 selection. |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
197 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
198 >>> html = HTML('<body>Some <em>test</em> text</body>') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
199 >>> print html | Transformer().select('.//em').trace() |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
200 (None, ('START', (QName(u'body'), Attrs()), (None, 1, 0))) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
201 (None, ('TEXT', u'Some ', (None, 1, 6))) |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
202 ('ENTER', ('START', (QName(u'em'), Attrs()), (None, 1, 11))) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
203 ('INSIDE', ('TEXT', u'test', (None, 1, 15))) |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
204 ('EXIT', ('END', QName(u'em'), (None, 1, 19))) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
205 (None, ('TEXT', u' text', (None, 1, 24))) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
206 (None, ('END', QName(u'body'), (None, 1, 29))) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
207 <body>Some <em>test</em> text</body> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
208 |
503 | 209 :param path: an XPath expression (as string) or a `Path` instance |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
210 :return: the stream augmented by transformation marks |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
211 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
212 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
213 return self.apply(SelectTransformation(path)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
214 |
3073ac688651
Added new 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 def invert(self): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
216 """Invert selection so that marked events become unmarked, and vice |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
217 versa. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
218 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
219 Specificaly, all marks are converted to null marks, and all null marks |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
220 are converted to OUTSIDE marks. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
221 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
222 >>> html = HTML('<body>Some <em>test</em> text</body>') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
223 >>> print html | Transformer('//em').invert().trace() |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
224 ('OUTSIDE', ('START', (QName(u'body'), Attrs()), (None, 1, 0))) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
225 ('OUTSIDE', ('TEXT', u'Some ', (None, 1, 6))) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
226 (None, ('START', (QName(u'em'), Attrs()), (None, 1, 11))) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
227 (None, ('TEXT', u'test', (None, 1, 15))) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
228 (None, ('END', QName(u'em'), (None, 1, 19))) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
229 ('OUTSIDE', ('TEXT', u' text', (None, 1, 24))) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
230 ('OUTSIDE', ('END', QName(u'body'), (None, 1, 29))) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
231 <body>Some <em>test</em> text</body> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
232 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
233 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
234 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
235 return self.apply(InvertTransformation()) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
236 |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
237 def end(self): |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
238 """End current selection, allowing all events to be selected. |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
239 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
240 Example: |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
241 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
242 >>> html = HTML('<body>Some <em>test</em> text</body>') |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
243 >>> print html | Transformer('//em').end().trace() |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
244 ('OUTSIDE', ('START', (QName(u'body'), Attrs()), (None, 1, 0))) |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
245 ('OUTSIDE', ('TEXT', u'Some ', (None, 1, 6))) |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
246 ('OUTSIDE', ('START', (QName(u'em'), Attrs()), (None, 1, 11))) |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
247 ('OUTSIDE', ('TEXT', u'test', (None, 1, 15))) |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
248 ('OUTSIDE', ('END', QName(u'em'), (None, 1, 19))) |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
249 ('OUTSIDE', ('TEXT', u' text', (None, 1, 24))) |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
250 ('OUTSIDE', ('END', QName(u'body'), (None, 1, 29))) |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
251 <body>Some <em>test</em> text</body> |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
252 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
253 :return: the stream augmented by transformation marks |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
254 :rtype: `Transformer` |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
255 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
256 return self.apply(EndTransformation()) |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
257 |
501
3073ac688651
Added new 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 #{ Deletion operations |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
259 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
260 def empty(self): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
261 """Empty selected elements of all content. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
262 |
3073ac688651
Added new 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 Example: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
264 |
3073ac688651
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
266 ... '<body>Some <em>body</em> text.</body></html>') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
267 >>> print html | Transformer('.//em').empty() |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
268 <html><head><title>Some Title</title></head><body>Some <em/> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
269 text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
270 |
3073ac688651
Added new 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` |
3073ac688651
Added new 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
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
273 return self.apply(EmptyTransformation()) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
274 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
275 def remove(self): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
276 """Remove selection from the stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
277 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
278 Example: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
279 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
280 >>> html = HTML('<html><head><title>Some Title</title></head>' |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
281 ... '<body>Some <em>body</em> text.</body></html>') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
282 >>> print html | Transformer('.//em').remove() |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
283 <html><head><title>Some Title</title></head><body>Some |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
284 text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
285 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
286 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
287 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
288 return self.apply(RemoveTransformation()) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
289 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
290 #{ Direct element operations |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
291 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
292 def unwrap(self): |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
293 """Remove outermost enclosing elements from selection. |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
294 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
295 Example: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
296 |
3073ac688651
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
298 ... '<body>Some <em>body</em> text.</body></html>') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
299 >>> print html | Transformer('.//em').unwrap() |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
300 <html><head><title>Some Title</title></head><body>Some body |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
301 text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
302 |
3073ac688651
Added new 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 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
304 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
305 return self.apply(UnwrapTransformation()) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
306 |
3073ac688651
Added new 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 def wrap(self, element): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
308 """Wrap selection in an element. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
309 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
310 >>> html = HTML('<html><head><title>Some Title</title></head>' |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
311 ... '<body>Some <em>body</em> text.</body></html>') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
312 >>> print html | Transformer('.//em').wrap('strong') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
313 <html><head><title>Some Title</title></head><body>Some |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
314 <strong><em>body</em></strong> text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
315 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
316 :param element: either a tag name (as string) or an `Element` object |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
317 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
318 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
319 return self.apply(WrapTransformation(element)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
320 |
3073ac688651
Added new 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 #{ Content insertion operations |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
322 |
3073ac688651
Added new 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 def replace(self, content): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
324 """Replace selection with content. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
325 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
326 >>> html = HTML('<html><head><title>Some Title</title></head>' |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
327 ... '<body>Some <em>body</em> text.</body></html>') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
328 >>> print html | Transformer('.//title/text()').replace('New Title') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
329 <html><head><title>New Title</title></head><body>Some <em>body</em> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
330 text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
331 |
3073ac688651
Added new 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 :param content: Either an iterable of events or a string to insert. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
333 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
334 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
335 return self.apply(ReplaceTransformation(content)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
336 |
3073ac688651
Added new 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 def before(self, content): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
338 """Insert content before selection. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
339 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
340 In this example we insert the word 'emphasised' before the <em> opening |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
341 tag: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
342 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
343 >>> html = HTML('<html><head><title>Some Title</title></head>' |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
344 ... '<body>Some <em>body</em> text.</body></html>') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
345 >>> print html | Transformer('.//em').before('emphasised ') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
346 <html><head><title>Some Title</title></head><body>Some emphasised |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
347 <em>body</em> text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
348 |
3073ac688651
Added new 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 :param content: Either an iterable of events or a string to insert. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
350 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
351 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
352 return self.apply(BeforeTransformation(content)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
353 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
354 def after(self, content): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
355 """Insert content after selection. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
356 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
357 Here, we insert some text after the </em> closing tag: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
358 |
3073ac688651
Added new 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 >>> html = HTML('<html><head><title>Some Title</title></head>' |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
360 ... '<body>Some <em>body</em> text.</body></html>') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
361 >>> print html | Transformer('.//em').after(' rock') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
362 <html><head><title>Some Title</title></head><body>Some <em>body</em> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
363 rock text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
364 |
3073ac688651
Added new 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 :param content: Either an iterable of events or a string to insert. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
366 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
367 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
368 return self.apply(AfterTransformation(content)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
369 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
370 def prepend(self, content): |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
371 """Insert content after the ENTER event of the selection. |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
372 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
373 Inserting some new text at the start of the <body>: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
374 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
375 >>> html = HTML('<html><head><title>Some Title</title></head>' |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
376 ... '<body>Some <em>body</em> text.</body></html>') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
377 >>> print html | Transformer('.//body').prepend('Some new body text. ') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
378 <html><head><title>Some Title</title></head><body>Some new body text. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
379 Some <em>body</em> text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
380 |
3073ac688651
Added new 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 :param content: Either an iterable of events or a string to insert. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
382 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
383 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
384 return self.apply(PrependTransformation(content)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
385 |
3073ac688651
Added new 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 def append(self, content): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
387 """Insert content before the END event of the selection. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
388 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
389 >>> html = HTML('<html><head><title>Some Title</title></head>' |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
390 ... '<body>Some <em>body</em> text.</body></html>') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
391 >>> print html | Transformer('.//body').append(' Some new body text.') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
392 <html><head><title>Some Title</title></head><body>Some <em>body</em> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
393 text. Some new body text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
394 |
3073ac688651
Added new 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 :param content: Either an iterable of events or a string to insert. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
396 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
397 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
398 return self.apply(AppendTransformation(content)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
399 |
3073ac688651
Added new 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 #{ Attribute manipulation |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
401 |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
402 def attr(self, name, value): |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
403 """Add, replace or delete an attribute on selected elements. |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
404 |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
405 If `value` evaulates to `None` the attribute will be deleted from the |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
406 element: |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
407 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
408 >>> html = HTML('<html><head><title>Some Title</title></head>' |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
409 ... '<body>Some <em class="before">body</em> <em>text</em>.</body>' |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
410 ... '</html>') |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
411 >>> print html | Transformer('body/em').attr('class', None) |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
412 <html><head><title>Some Title</title></head><body>Some <em>body</em> |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
413 <em>text</em>.</body></html> |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
414 |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
415 Otherwise the attribute will be set to `value`: |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
416 |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
417 >>> print html | Transformer('body/em').attr('class', 'emphasis') |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
418 <html><head><title>Some Title</title></head><body>Some <em |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
419 class="emphasis">body</em> <em class="emphasis">text</em>.</body></html> |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
420 |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
421 If `value` is a callable it will be called with the attribute name and |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
422 the `START` event for the matching element. Its return value will then |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
423 be used to set the attribute: |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
424 |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
425 >>> def print_attr(name, event): |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
426 ... attrs = event[1][1] |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
427 ... print attrs |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
428 ... return attrs.get(name) |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
429 >>> print html | Transformer('body/em').attr('class', print_attr) |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
430 Attrs([(QName(u'class'), u'before')]) |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
431 Attrs() |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
432 <html><head><title>Some Title</title></head><body>Some <em |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
433 class="before">body</em> <em>text</em>.</body></html> |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
434 |
503 | 435 :param name: the name of the attribute |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
436 :param value: the value that should be set for the attribute. |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
437 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
438 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
439 return self.apply(AttrTransformation(name, value)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
440 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
441 #{ Buffer operations |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
442 |
3073ac688651
Added new 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 def copy(self, buffer): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
444 """Copy selection into buffer. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
445 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
446 >>> from genshi.builder import tag |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
447 >>> buffer = StreamBuffer() |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
448 >>> html = HTML('<html><head><title>Some Title</title></head>' |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
449 ... '<body>Some <em>body</em> text.</body></html>') |
509 | 450 >>> print html | Transformer('title/text()').copy(buffer) \\ |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
451 ... .end().select('body').prepend(tag.h1(buffer)) |
509 | 452 <html><head><title>Some Title</title></head><body><h1>Some |
453 Title</h1>Some <em>body</em> text.</body></html> | |
454 | |
455 To ensure that a transformation can be reused deterministically, the | |
456 contents of ``buffer`` is replaced by the ``copy()`` operation: | |
457 | |
458 >>> print buffer | |
459 Some Title | |
460 >>> print html | Transformer('head/title/text()').copy(buffer) \\ | |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
461 ... .end().select('body/em').copy(buffer).end().select('body') \\ |
509 | 462 ... .prepend(tag.h1(buffer)) |
463 <html><head><title>Some | |
464 Title</title></head><body><h1><em>body</em></h1>Some <em>body</em> | |
465 text.</body></html> | |
466 >>> print buffer | |
467 <em>body</em> | |
468 | |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
469 Element attributes can also be copied for later use: |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
470 |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
471 >>> html = HTML('<html><head><title>Some Title</title></head>' |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
472 ... '<body><em>Some</em> <em class="before">body</em>' |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
473 ... '<em>text</em>.</body></html>') |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
474 >>> buffer = StreamBuffer() |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
475 >>> def apply_attr(name, entry): |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
476 ... return list(buffer)[0][1][1].get('class') |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
477 >>> print html | Transformer('body/em[@class]/@class').copy(buffer) \\ |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
478 ... .end().select('body/em[not(@class)]').attr('class', apply_attr) |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
479 <html><head><title>Some Title</title></head><body><em |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
480 class="before">Some</em> <em class="before">body</em><em |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
481 class="before">text</em>.</body></html> |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
482 |
501
3073ac688651
Added new 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 |
507 | 484 :param buffer: the `StreamBuffer` in which the selection should be |
485 stored | |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
486 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
487 :note: this transformation will buffer the entire input stream |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
488 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
489 return self.apply(CopyTransformation(buffer)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
490 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
491 def cut(self, buffer): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
492 """Copy selection into buffer and remove the selection from the stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
493 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
494 >>> from genshi.builder import tag |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
495 >>> buffer = StreamBuffer() |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
496 >>> html = HTML('<html><head><title>Some Title</title></head>' |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
497 ... '<body>Some <em>body</em> text.</body></html>') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
498 >>> print html | Transformer('.//em/text()').cut(buffer) \\ |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
499 ... .end().select('.//em').after(tag.h1(buffer)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
500 <html><head><title>Some Title</title></head><body>Some |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
501 <em/><h1>body</h1> text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
502 |
507 | 503 :param buffer: the `StreamBuffer` in which the selection should be |
504 stored | |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
505 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
506 :note: this transformation will buffer the entire input stream |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
507 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
508 return self.apply(CutTransformation(buffer)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
509 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
510 #{ Miscellaneous operations |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
511 |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
512 def filter(self, filter): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
513 """Apply a normal stream filter to the selection. The filter is called |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
514 once for each contiguous block of marked events. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
515 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
516 >>> from genshi.filters.html import HTMLSanitizer |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
517 >>> html = HTML('<html><body>Some text<script>alert(document.cookie)' |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
518 ... '</script> and some more text</body></html>') |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
519 >>> print html | Transformer('body/*').filter(HTMLSanitizer()) |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
520 <html><body>Some text and some more text</body></html> |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
521 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
522 :param filter: The stream filter to apply. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
523 :rtype: `Transformer` |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
524 """ |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
525 return self.apply(FilterTransformation(filter)) |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
526 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
527 def map(self, function, kind): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
528 """Applies a function to the ``data`` element of events of ``kind`` in |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
529 the selection. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
530 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
531 >>> html = HTML('<html><head><title>Some Title</title></head>' |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
532 ... '<body>Some <em>body</em> text.</body></html>') |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
533 >>> print html | Transformer('head/title').map(unicode.upper, TEXT) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
534 <html><head><title>SOME TITLE</title></head><body>Some <em>body</em> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
535 text.</body></html> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
536 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
537 :param function: the function to apply |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
538 :param kind: the kind of event the function should be applied to |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
539 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
540 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
541 return self.apply(MapTransformation(function, kind)) |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
542 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
543 def substitute(self, pattern, replace, count=1): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
544 """Replace text matching a regular expression. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
545 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
546 Refer to the documentation for ``re.sub()`` for details. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
547 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
548 >>> html = HTML('<html><body>Some text, some more text and ' |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
549 ... '<b>some bold text</b></body></html>') |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
550 >>> print html | Transformer('body').substitute('(?i)some', 'SOME') |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
551 <html><body>SOME text, some more text and <b>SOME bold text</b></body></html> |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
552 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
553 :param pattern: A regular expression object or string. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
554 :param replace: Replacement pattern. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
555 :param count: Number of replacements to make in each text fragment. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
556 :rtype: `Transformer` |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
557 """ |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
558 return self.apply(SubstituteTransformation(pattern, replace, count)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
559 |
578 | 560 def rename(self, name): |
561 """Rename matching elements. | |
562 | |
563 >>> html = HTML('<html><body>Some text, some more text and ' | |
564 ... '<b>some bold text</b></body></html>') | |
565 >>> print html | Transformer('body/b').rename('strong') | |
566 <html><body>Some text, some more text and <strong>some bold text</strong></body></html> | |
567 """ | |
568 return self.apply(RenameTransformation(name)) | |
569 | |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
570 def trace(self, prefix='', fileobj=None): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
571 """Print events as they pass through the transform. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
572 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
573 >>> html = HTML('<body>Some <em>test</em> text</body>') |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
574 >>> print html | Transformer('em').trace() |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
575 (None, ('START', (QName(u'body'), Attrs()), (None, 1, 0))) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
576 (None, ('TEXT', u'Some ', (None, 1, 6))) |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
577 ('ENTER', ('START', (QName(u'em'), Attrs()), (None, 1, 11))) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
578 ('INSIDE', ('TEXT', u'test', (None, 1, 15))) |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
579 ('EXIT', ('END', QName(u'em'), (None, 1, 19))) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
580 (None, ('TEXT', u' text', (None, 1, 24))) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
581 (None, ('END', QName(u'body'), (None, 1, 29))) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
582 <body>Some <em>test</em> text</body> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
583 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
584 :param prefix: a string to prefix each event with in the output |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
585 :param fileobj: the writable file-like object to write to; defaults to |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
586 the standard output stream |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
587 :rtype: `Transformer` |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
588 """ |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
589 return self.apply(TraceTransformation(prefix, fileobj=fileobj)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
590 |
3073ac688651
Added new 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 # Internal methods |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
592 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
593 def _mark(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
594 for event in stream: |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
595 yield OUTSIDE, event |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
596 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
597 def _unmark(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
598 for mark, event in stream: |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
599 if event[0] is not None: |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
600 yield event |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
601 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
602 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
603 class SelectTransformation(object): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
604 """Select and mark events that match an XPath expression.""" |
503 | 605 |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
606 def __init__(self, path): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
607 """Create selection. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
608 |
503 | 609 :param path: an XPath expression (as string) or a `Path` object |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
610 """ |
503 | 611 if not isinstance(path, Path): |
612 path = Path(path) | |
613 self.path = path | |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
614 |
3073ac688651
Added new 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 def __call__(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
616 """Apply the transform filter to the marked stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
617 |
503 | 618 :param stream: the marked event stream to filter |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
619 """ |
3073ac688651
Added new 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 namespaces = {} |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
621 variables = {} |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
622 test = self.path.test() |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
623 stream = iter(stream) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
624 for mark, event in stream: |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
625 if mark is None: |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
626 yield mark, event |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
627 continue |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
628 result = test(event, {}, {}) |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
629 # XXX This is effectively genshi.core._ensure() for transform |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
630 # streams. |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
631 if result is True: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
632 if event[0] is START: |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
633 yield ENTER, event |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
634 depth = 1 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
635 while depth > 0: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
636 mark, subevent = stream.next() |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
637 if subevent[0] is START: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
638 depth += 1 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
639 elif subevent[0] is END: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
640 depth -= 1 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
641 if depth == 0: |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
642 yield EXIT, subevent |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
643 else: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
644 yield INSIDE, subevent |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
645 test(subevent, {}, {}, updateonly=True) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
646 else: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
647 yield OUTSIDE, event |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
648 elif isinstance(result, Attrs): |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
649 # XXX Selected *attributes* are given a "kind" of None to |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
650 # indicate they are not really part of the stream. |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
651 yield ATTR, (None, (QName(event[1][0] + '@*'), result), event[2]) |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
652 yield None, event |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
653 elif result: |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
654 yield None, (TEXT, unicode(result), (None, -1, -1)) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
655 else: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
656 yield None, event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
657 |
3073ac688651
Added new 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 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
659 class InvertTransformation(object): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
660 """Invert selection so that marked events become unmarked, and vice versa. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
661 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
662 Specificaly, all input marks are converted to null marks, and all input |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
663 null marks are converted to OUTSIDE marks. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
664 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
665 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
666 def __call__(self, stream): |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
667 """Apply the transform filter to the marked stream. |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
668 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
669 :param stream: the marked event stream to filter |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
670 """ |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
671 for mark, event in stream: |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
672 if mark: |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
673 yield None, event |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
674 else: |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
675 yield OUTSIDE, event |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
676 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
677 |
514
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
678 class EndTransformation(object): |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
679 """End the current selection.""" |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
680 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
681 def __call__(self, stream): |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
682 """Apply the transform filter to the marked stream. |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
683 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
684 :param stream: the marked event stream to filter |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
685 """ |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
686 for mark, event in stream: |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
687 yield OUTSIDE, event |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
688 |
e293bbb40507
Fixed a bug where select() operated on the entire stream rather than only on
athomas
parents:
509
diff
changeset
|
689 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
690 class EmptyTransformation(object): |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
691 """Empty selected elements of all content.""" |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
692 |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
693 def __call__(self, stream): |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
694 """Apply the transform filter to the marked stream. |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
695 |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
696 :param stream: the marked event stream to filter |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
697 """ |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
698 for mark, event in stream: |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
699 if mark not in (INSIDE, OUTSIDE): |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
700 yield mark, event |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
701 |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
702 |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
703 class RemoveTransformation(object): |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
704 """Remove selection from the stream.""" |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
705 |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
706 def __call__(self, stream): |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
707 """Apply the transform filter to the marked stream. |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
708 |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
709 :param stream: the marked event stream to filter |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
710 """ |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
711 for mark, event in stream: |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
712 if mark is None: |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
713 yield mark, event |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
714 |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
715 |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
716 class UnwrapTransformation(object): |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
717 """Remove outtermost enclosing elements from selection.""" |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
718 |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
719 def __call__(self, stream): |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
720 """Apply the transform filter to the marked stream. |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
721 |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
722 :param stream: the marked event stream to filter |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
723 """ |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
724 for mark, event in stream: |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
725 if mark not in (ENTER, EXIT): |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
726 yield mark, event |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
727 |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
728 |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
729 class WrapTransformation(object): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
730 """Wrap selection in an element.""" |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
731 |
3073ac688651
Added new 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 def __init__(self, element): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
733 if isinstance(element, Element): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
734 self.element = element |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
735 else: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
736 self.element = Element(element) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
737 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
738 def __call__(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
739 for mark, event in stream: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
740 if mark: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
741 element = list(self.element.generate()) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
742 for prefix in element[:-1]: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
743 yield None, prefix |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
744 yield mark, event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
745 while True: |
575
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
746 try: |
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
747 mark, event = stream.next() |
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
748 except StopIteration: |
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
749 yield None, element[-1] |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
750 if not mark: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
751 break |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
752 yield mark, event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
753 yield None, element[-1] |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
754 yield mark, event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
755 else: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
756 yield mark, event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
757 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
758 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
759 class TraceTransformation(object): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
760 """Print events as they pass through the transform.""" |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
761 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
762 def __init__(self, prefix='', fileobj=None): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
763 """Trace constructor. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
764 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
765 :param prefix: text to prefix each traced line with. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
766 :param fileobj: the writable file-like object to write to |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
767 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
768 self.prefix = prefix |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
769 self.fileobj = fileobj or sys.stdout |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
770 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
771 def __call__(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
772 """Apply the transform filter to the marked stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
773 |
503 | 774 :param stream: the marked event stream to filter |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
775 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
776 for event in stream: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
777 print>>self.fileobj, self.prefix + str(event) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
778 yield event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
779 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
780 |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
781 class FilterTransformation(object): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
782 """Apply a normal stream filter to the selection. The filter is called once |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
783 for each contiguous block of marked events.""" |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
784 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
785 def __init__(self, filter): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
786 """Create the transform. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
787 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
788 :param filter: The stream filter to apply. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
789 """ |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
790 self.filter = filter |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
791 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
792 def __call__(self, stream): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
793 """Apply the transform filter to the marked stream. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
794 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
795 :param stream: The marked event stream to filter |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
796 """ |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
797 def flush(queue): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
798 if queue: |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
799 for event in self.filter(queue): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
800 yield OUTSIDE, event |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
801 del queue[:] |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
802 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
803 queue = [] |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
804 for mark, event in stream: |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
805 if mark: |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
806 queue.append(event) |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
807 else: |
577
5796de680d94
Fix for #136, where transformer marks were not being stripped correctly when
athomas
parents:
576
diff
changeset
|
808 for queue_event in flush(queue): |
5796de680d94
Fix for #136, where transformer marks were not being stripped correctly when
athomas
parents:
576
diff
changeset
|
809 yield queue_event |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
810 yield None, event |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
811 for event in flush(queue): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
812 yield event |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
813 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
814 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
815 class MapTransformation(object): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
816 """Apply a function to the `data` element of events of ``kind`` in the |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
817 selection. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
818 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
819 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
820 def __init__(self, function, kind): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
821 """Create the transform. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
822 |
503 | 823 :param function: the function to apply; the function must take one |
824 argument, the `data` element of each selected event | |
825 :param kind: the stream event ``kind`` to apply the `function` to | |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
826 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
827 self.function = function |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
828 self.kind = kind |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
829 |
3073ac688651
Added new 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 def __call__(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
831 """Apply the transform filter to the marked stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
832 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
833 :param stream: The marked event stream to filter |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
834 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
835 for mark, (kind, data, pos) in stream: |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
836 if mark and self.kind in (None, kind): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
837 yield mark, (kind, self.function(data), pos) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
838 else: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
839 yield mark, (kind, data, pos) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
840 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
841 |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
842 class SubstituteTransformation(object): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
843 """Replace text matching a regular expression. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
844 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
845 Refer to the documentation for ``re.sub()`` for details. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
846 """ |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
847 def __init__(self, pattern, replace, count=1): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
848 """Create the transform. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
849 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
850 :param pattern: A regular expression object, or string. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
851 :param replace: Replacement pattern. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
852 :param count: Number of replacements to make in each text fragment. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
853 """ |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
854 if isinstance(pattern, basestring): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
855 self.pattern = re.compile(pattern) |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
856 else: |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
857 self.pattern = pattern |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
858 self.count = count |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
859 self.replace = replace |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
860 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
861 def __call__(self, stream): |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
862 """Apply the transform filter to the marked stream. |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
863 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
864 :param stream: The marked event stream to filter |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
865 """ |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
866 for mark, (kind, data, pos) in stream: |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
867 if kind is TEXT: |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
868 data = self.pattern.sub(self.replace, data, self.count) |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
869 yield mark, (kind, data, pos) |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
870 |
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
871 |
578 | 872 class RenameTransformation(object): |
873 """Rename matching elements.""" | |
874 def __init__(self, name): | |
875 """Create the transform. | |
876 | |
877 :param name: New element name. | |
878 """ | |
879 self.name = QName(name) | |
880 | |
881 def __call__(self, stream): | |
882 """Apply the transform filter to the marked stream. | |
883 | |
884 :param stream: The marked event stream to filter | |
885 """ | |
886 for mark, (kind, data, pos) in stream: | |
887 if mark is ENTER: | |
888 data = self.name, data[1] | |
889 elif mark is EXIT: | |
890 data = self.name | |
891 yield mark, (kind, data, pos) | |
892 | |
893 | |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
894 class InjectorTransformation(object): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
895 """Abstract base class for transformations that inject content into a |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
896 stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
897 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
898 >>> class Top(InjectorTransformation): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
899 ... def __call__(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
900 ... for event in self._inject(): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
901 ... yield event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
902 ... for event in stream: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
903 ... yield event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
904 >>> html = HTML('<body>Some <em>test</em> text</body>') |
533
dfb45908fadc
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
athomas
parents:
531
diff
changeset
|
905 >>> print html | Transformer('.//em').apply(Top('Prefix ')) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
906 Prefix <body>Some <em>test</em> text</body> |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
907 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
908 def __init__(self, content): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
909 """Create a new injector. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
910 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
911 :param content: An iterable of Genshi stream events, or a string to be |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
912 injected. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
913 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
914 self.content = content |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
915 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
916 def _inject(self): |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
917 for event in _ensure(self.content): |
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
918 yield None, event |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
919 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
920 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
921 class ReplaceTransformation(InjectorTransformation): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
922 """Replace selection with content.""" |
503 | 923 |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
924 def __call__(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
925 """Apply the transform filter to the marked stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
926 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
927 :param stream: The marked event stream to filter |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
928 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
929 for mark, event in stream: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
930 if mark is not None: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
931 for subevent in self._inject(): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
932 yield subevent |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
933 while True: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
934 mark, event = stream.next() |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
935 if mark is None: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
936 yield mark, event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
937 break |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
938 else: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
939 yield mark, event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
940 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
941 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
942 class BeforeTransformation(InjectorTransformation): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
943 """Insert content before selection.""" |
503 | 944 |
501
3073ac688651
Added new 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 def __call__(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
946 """Apply the transform filter to the marked stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
947 |
3073ac688651
Added new 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 :param stream: The marked event stream to filter |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
949 """ |
3073ac688651
Added new 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 for mark, event in stream: |
575
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
951 if mark is not None: |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
952 for subevent in self._inject(): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
953 yield subevent |
575
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
954 yield mark, event |
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
955 while True: |
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
956 mark, event = stream.next() |
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
957 if not mark: |
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
958 break |
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
959 yield mark, event |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
960 yield mark, event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
961 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
962 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
963 class AfterTransformation(InjectorTransformation): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
964 """Insert content after selection.""" |
503 | 965 |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
966 def __call__(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
967 """Apply the transform filter to the marked stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
968 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
969 :param stream: The marked event stream to filter |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
970 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
971 for mark, event in stream: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
972 yield mark, event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
973 if mark: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
974 while True: |
575
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
975 try: |
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
976 mark, event = stream.next() |
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
977 except StopIteration: |
7950b266c97d
Ensure that content gets added after the end of a stream.
athomas
parents:
533
diff
changeset
|
978 break |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
979 if not mark: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
980 break |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
981 yield mark, event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
982 for subevent in self._inject(): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
983 yield subevent |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
984 yield mark, event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
985 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
986 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
987 class PrependTransformation(InjectorTransformation): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
988 """Prepend content to the inside of selected elements.""" |
503 | 989 |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
990 def __call__(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
991 """Apply the transform filter to the marked stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
992 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
993 :param stream: The marked event stream to filter |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
994 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
995 for mark, event in stream: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
996 yield mark, event |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
997 if mark in (ENTER, OUTSIDE): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
998 for subevent in self._inject(): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
999 yield subevent |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1000 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1001 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
1002 class AppendTransformation(InjectorTransformation): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1003 """Append content after the content of selected elements.""" |
503 | 1004 |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1005 def __call__(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1006 """Apply the transform filter to the marked stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1007 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1008 :param stream: The marked event stream to filter |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1009 """ |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1010 for mark, event in stream: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1011 yield mark, event |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
1012 if mark is ENTER: |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1013 while True: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1014 mark, event = stream.next() |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
1015 if mark is EXIT: |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1016 break |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1017 yield mark, event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1018 for subevent in self._inject(): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1019 yield subevent |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1020 yield mark, event |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1021 |
3073ac688651
Added new 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 |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1023 class AttrTransformation(object): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1024 """Set an attribute on selected elements.""" |
503 | 1025 |
1026 def __init__(self, name, value): | |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1027 """Construct transform. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1028 |
503 | 1029 :param name: name of the attribute that should be set |
1030 :param value: the value to set | |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1031 """ |
503 | 1032 self.name = name |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1033 self.value = value |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1034 |
3073ac688651
Added new 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 def __call__(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1036 """Apply the transform filter to the marked stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1037 |
3073ac688651
Added new 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 :param stream: The marked event stream to filter |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1039 """ |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1040 callable_value = callable(self.value) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1041 for mark, (kind, data, pos) in stream: |
502
53b478e3f3e2
Minor doc and naming improvements for the new transformation filter.
cmlenz
parents:
501
diff
changeset
|
1042 if mark is ENTER: |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1043 if callable_value: |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1044 value = self.value(self.name, (kind, data, pos)) |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1045 else: |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1046 value = self.value |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1047 if value is None: |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1048 attrs = data[1] - [QName(self.name)] |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1049 else: |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1050 attrs = data[1] | [(QName(self.name), value)] |
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1051 data = (data[0], attrs) |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1052 yield mark, (kind, data, pos) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1053 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1054 |
3073ac688651
Added new 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 |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1056 class StreamBuffer(Stream): |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1057 """Stream event buffer used for cut and copy transformations.""" |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1058 |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1059 def __init__(self): |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1060 """Create the buffer.""" |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1061 Stream.__init__(self, []) |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1062 |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1063 def append(self, event): |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1064 """Add an event to the buffer. |
517
c98e9dcbedbb
More flexible transformation attribute set and delete. Attribute selections are
athomas
parents:
515
diff
changeset
|
1065 |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1066 :param event: the markup event to add |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1067 """ |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1068 self.events.append(event) |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1069 |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1070 def reset(self): |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1071 """Reset the buffer so that it's empty.""" |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1072 del self.events[:] |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1073 |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1074 |
504
2db10ef79e66
Renamed the transformation classes so that their role is clarified; extended the example in the package docstring to show chaining.
cmlenz
parents:
503
diff
changeset
|
1075 class CopyTransformation(object): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1076 """Copy selected events into a buffer for later insertion.""" |
503 | 1077 |
501
3073ac688651
Added new 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 def __init__(self, buffer): |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1079 """Create the copy transformation. |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1080 |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1081 :param buffer: the `StreamBuffer` in which the selection should be |
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1082 stored |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1083 """ |
3073ac688651
Added new 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 self.buffer = buffer |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1085 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1086 def __call__(self, stream): |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1087 """Apply the transformation to the marked stream. |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1088 |
503 | 1089 :param stream: the marked event stream to filter |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1090 """ |
506
f1bfa8a09d99
Use explicit buffer class in transformer filter, and revert the breaking change to the builder module.
cmlenz
parents:
504
diff
changeset
|
1091 self.buffer.reset() |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1092 stream = list(stream) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1093 for mark, event in stream: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1094 if mark: |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1095 self.buffer.append(event) |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1096 return stream |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1097 |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1098 |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1099 class CutTransformation(object): |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1100 """Cut selected events into a buffer for later insertion and remove the |
503 | 1101 selection. |
1102 """ | |
1103 | |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1104 def __init__(self, buffer): |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1105 """Create the cut transformation. |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1106 |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1107 :param buffer: the `StreamBuffer` in which the selection should be |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1108 stored |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1109 """ |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1110 self.buffer = buffer |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1111 |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1112 def __call__(self, stream): |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1113 """Apply the transform filter to the marked stream. |
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1114 |
503 | 1115 :param stream: the marked event stream to filter |
501
3073ac688651
Added new markup transformation filter contributed by Alec Thomas (#122). This provides gorgeous jQuery-inspired stream transformation capabilities based on XPath expressions.
cmlenz
parents:
diff
changeset
|
1116 """ |
519
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1117 out_stream = [] |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1118 attributes = None |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1119 for mark, (kind, data, pos) in stream: |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1120 if attributes: |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1121 assert kind is START |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1122 data = (data[0], data[1] - attributes) |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1123 attributes = None |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1124 if mark: |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1125 # There is some magic here. ATTR marked events are pushed into |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1126 # the stream *before* the START event they originated from. |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1127 # This allows cut() to strip out the attributes from START |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1128 # event as would be expected. |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1129 if mark is ATTR: |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1130 self.buffer.append((kind, data, pos)) |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1131 attributes = [name for name, _ in data[1]] |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1132 else: |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1133 self.buffer.append((kind, data, pos)) |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1134 else: |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1135 out_stream.append((mark, (kind, data, pos))) |
9f1d90d6abd4
Cut and copy transformer operations can now operate on selected attributes.
athomas
parents:
518
diff
changeset
|
1136 return out_stream |