Mercurial > genshi > mirror
comparison doc/filters.txt @ 533:4d486f15c986 trunk
Thanks to Dave Abrahams for pointing out some deficiencies in the transformer
filter:
- `apply()` has been renamed to `map()`.
- `filter()` applies a normal stream filter to the selection.
- To reduce confusion with normal stream filter pipelines the `__or__`
operator, which previously applied a custom transform, has been renamed to
`apply()`.
Also added a `substitute()` transformation that applies regex replacement to
`TEXT` events.
author | athomas |
---|---|
date | Fri, 22 Jun 2007 16:42:38 +0000 |
parents | 9e11fa7f4603 |
children | 0fcb1c0d7c20 |
comparison
equal
deleted
inserted
replaced
531:9ce91447fc6b | 533:4d486f15c986 |
---|---|
162 ... <body> | 162 ... <body> |
163 ... Some <em>body</em> text. | 163 ... Some <em>body</em> text. |
164 ... </body> | 164 ... </body> |
165 ... </html>''') | 165 ... </html>''') |
166 | 166 |
167 >>> print html | Transformer('body/em').apply(unicode.upper, TEXT) \ | 167 >>> print html | Transformer('body/em').map(unicode.upper, TEXT) \ |
168 ... .unwrap().wrap(tag.u).end() \ | 168 ... .unwrap().wrap(tag.u).end() \ |
169 ... .select('body/u') \ | 169 ... .select('body/u') \ |
170 ... .prepend('underlined ') | 170 ... .prepend('underlined ') |
171 <html> | 171 <html> |
172 <head><title>Some Title</title></head> | 172 <head><title>Some Title</title></head> |
179 | 179 |
180 1. matches any `<em>` element anywhere in the body, | 180 1. matches any `<em>` element anywhere in the body, |
181 2. uppercases any text nodes in the element, | 181 2. uppercases any text nodes in the element, |
182 3. strips off the `<em>` start and close tags, | 182 3. strips off the `<em>` start and close tags, |
183 4. wraps the content in a `<u>` tag, and | 183 4. wraps the content in a `<u>` tag, and |
184 5. inserts the text `underlind` inside the `<u>` tag. | 184 5. inserts the text `underlined` inside the `<u>` tag. |
185 | 185 |
186 A number of commonly useful transformations are available for this filter. | 186 A number of commonly useful transformations are available for this filter. |
187 Please consult the API documentation a complete list. | 187 Please consult the API documentation a complete list. |
188 | 188 |
189 In addition, you can also perform custom transformations. For example, the | 189 In addition, you can also perform custom transformations. For example, the |
207 | 207 |
208 A transformation can be any callable object that accepts an augmented event | 208 A transformation can be any callable object that accepts an augmented event |
209 stream. In this case we define a class, so that we can initialize it with the | 209 stream. In this case we define a class, so that we can initialize it with the |
210 tag name. | 210 tag name. |
211 | 211 |
212 Custom transformations can be applied using the `|` operator on the transformer | 212 Custom transformations can be applied using the `apply()` method of a |
213 instance: | 213 transformer instance: |
214 | 214 |
215 .. code-block:: pycon | 215 .. code-block:: pycon |
216 | 216 |
217 >>> xform = Transformer('body//em').apply(unicode.upper, TEXT) | 217 >>> xform = Transformer('body//em').map(unicode.upper, TEXT) \ |
218 >>> xform |= RenameTransformation('u') | 218 >>> xform = xform.apply(RenameTransformation('u')) |
219 >>> print html | xform | 219 >>> print html | xform |
220 <html> | 220 <html> |
221 <head><title>Some Title</title></head> | 221 <head><title>Some Title</title></head> |
222 <body> | 222 <body> |
223 Some <u>BODY</u> text. | 223 Some <u>BODY</u> text. |