annotate genshi/filters/transform.py @ 784:67d324a62cc0 experimental-match-fastpaths

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