ITemplateStreamFilter and Genshi Transforms

12 views
Skip to first unread message

Scott Sharkey

unread,
Dec 17, 2007, 9:36:34 AM12/17/07
to trac...@googlegroups.com
Hi All,

I'm trying to make a plugin that changes some specific terms on the
various web pages (just experimenting with the interface right now,
though) and have some strange behaviour with Genshi transforms and the
ITemplateStreamFilter interface. Here's my basic plugin:

from genshi.filters import Transformer

class UIPlugin(Component):
"""Substitute various terms on various templates.

This plugin uses the ITemplateStreamFilter to filter various
terms on specific pages, replacing ticket with task, milestone
with job, etc..
"""

implements(ITemplateStreamFilter)

# ITemplateStreamFilter methods

def filter_stream(self, req, method, filename, stream, data):
filter =
Transformer('//img[@src="/chrome/common/trac_logo_mini.png"]')
return stream | filter.substitute('common', 'site')

I'm getting a page (wiki front), but the contents are being interpreted
as plain text instead of html. I am basing the above plugin on the
ticket_clone example, but I'm clearly missing something. Any
suggestions on what I'm doing wrong?

-Scott

Christian Boos

unread,
Dec 17, 2007, 12:13:18 PM12/17/07
to trac...@googlegroups.com
Hello Scott,

Scott Sharkey wrote:
> Hi All,
>
> I'm trying to make a plugin that changes some specific terms on the
> various web pages (just experimenting with the interface right now,
> though) and have some strange behaviour with Genshi transforms and the
> ITemplateStreamFilter interface. Here's my basic plugin:
>
> from genshi.filters import Transformer
>
> class UIPlugin(Component):
> """Substitute various terms on various templates.
>
> This plugin uses the ITemplateStreamFilter to filter various
> terms on specific pages, replacing ticket with task, milestone
> with job, etc..
> """
>
> implements(ITemplateStreamFilter)
>
> # ITemplateStreamFilter methods
>
> def filter_stream(self, req, method, filename, stream, data):
> filter =
> Transformer('//img[@src="/chrome/common/trac_logo_mini.png"]')
> return stream | filter.substitute('common', 'site')
>

(please next time provide the sample plugin as an attachment, thanks!)

> I'm getting a page (wiki front), but the contents are being interpreted
> as plain text instead of html. I am basing the above plugin on the
> ticket_clone example, but I'm clearly missing something. Any
> suggestions on what I'm doing wrong?

The HTML-escaped text you're seeing actually comes from a Genshi bug [1].
Now the other problem you have in your example is that substitute() is
not (currently?) supposed to work on the attribute values of the matched
element, only on the text content.

Other than that, and for your general use case (replacing ticket with
task, milestone with job, etc.), the plugin should work fine once the
bug is fixed.

-- Christian

[1] - http://genshi.edgewall.org/ticket/168

Alec Thomas

unread,
Dec 17, 2007, 12:30:02 PM12/17/07
to trac...@googlegroups.com
On 18/12/2007, Christian Boos <cb...@neuf.fr> wrote:
> The HTML-escaped text you're seeing actually comes from a Genshi bug [1].
> Now the other problem you have in your example is that substitute() is
> not (currently?) supposed to work on the attribute values of the matched
> element, only on the text content.

Replying to Christian for context, but this is for Scott:

The function you want is "attr()":

http://genshi.edgewall.org/wiki/ApiDocs/genshi.filters.transform#genshi.filters.transform:Transformer:attr

--
Evolution: Taking care of those too stupid to take care of themselves.

Scott Sharkey

unread,
Dec 17, 2007, 2:08:11 PM12/17/07
to trac...@googlegroups.com
Christian Boos wrote:

> Scott Sharkey wrote:
>> I'm trying to make a plugin that changes some specific terms on the
>> various web pages (just experimenting with the interface right now,
>> though) and have some strange behaviour with Genshi transforms and the
>> ITemplateStreamFilter interface. Here's my basic plugin:

Plugin deleted.

> (please next time provide the sample plugin as an attachment, thanks!)

Will do - sorry!

>> I'm getting a page (wiki front), but the contents are being interpreted
>> as plain text instead of html. I am basing the above plugin on the
>> ticket_clone example, but I'm clearly missing something. Any
>> suggestions on what I'm doing wrong?
>
> The HTML-escaped text you're seeing actually comes from a Genshi bug [1].
> Now the other problem you have in your example is that substitute() is
> not (currently?) supposed to work on the attribute values of the matched
> element, only on the text content.

I actually figured that out almost immediately after I sent the message
(isn't that always the way!). Wasn't aware of the bug, but I got it
working for me by rearranging the selection criteria.

> Other than that, and for your general use case (replacing ticket with
> task, milestone with job, etc.), the plugin should work fine once the
> bug is fixed.

Thanks a BUNCH!
-Scott


Scott Sharkey

unread,
Dec 17, 2007, 2:09:11 PM12/17/07
to trac...@googlegroups.com
Alec Thomas wrote:
> On 18/12/2007, Christian Boos <cb...@neuf.fr> wrote:
>> The HTML-escaped text you're seeing actually comes from a Genshi bug [1].
>> Now the other problem you have in your example is that substitute() is
>> not (currently?) supposed to work on the attribute values of the matched
>> element, only on the text content.
>
> Replying to Christian for context, but this is for Scott:
>
> The function you want is "attr()":
>
> http://genshi.edgewall.org/wiki/ApiDocs/genshi.filters.transform#genshi.filters.transform:Transformer:attr
>

Yes, I went back and studied the genshi.filters.transform doc's again,
and realized I had the wrong selection.

Thanks!
-Scott

Reply all
Reply to author
Forward
0 new messages