annotate genshi/filters/transform.py @ 933:feba07fc925b

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