span.fullpost {display:inline;}

Friday, June 5, 2009

Working with Genshi

Figuring out this Trac stuff is getting annoying. Why aren't there any explanations anywhere!? Everything takes so much longer than it should... we're stuck trying to piece together what to do through looking at different people's plug-ins, which are all written differently, and usually not commented at all.

Today I was looking at some Genshi stuff, which some people use to get content into their Trac pages. You have your plugin implement ITemplateStreamFilter (from trac.web.api). There is a function called Transformer (in genshi.filters.transform) that will search the template of a page for specific lines of HTML, and then you can add things before or after the line that it finds. The content that you insert into the page is constructed using tag (in genshi.builder). Here's an example of a filter_stream method that will display a list of items with title "Item List:" after the div id="main" line in the template:
def filter_stream(self, req, method, filename, stream, data):
items = []
items.append(tag.li("Apples"))
items.append(tag.li("Oranges"))
items.append(tag.li("Bananas"))
item_list = tag.ul(tag.lh("Item List:"), items)
return stream | Transformer('//div[@id="main"]').prepend(item_list)

This will put the following into the template:
<div id="main">
<ul>
<lh>Item List:</lh>
<li>Apples</li>
<li>Oranges</li>
<li>Bananas</li>
</ul>
</div>

An alternate way of doing this requires you to import HTML from genshi:
def filter_stream(self, req, method, filename, stream, data):
html = HTML('''<ul>
<lh>Item List:</lh>
<li>Apples</li>
<li>Oranges</li>
<li>Bananas</li>
</ul>''')
return stream | Transformer('//div[@id="main"]').prepend(html)

All of this seems really easy now that I know how to do it, but figuring it out took a really long time. Sigh.

Now, I'm trying to use the second method to get javascript into the Trac pages. It works for simple scripts, but the scripts I have for generating graphs with JSViz aren't working for some reason. I will tackle this problem next week!

0 Comments:

Post a Comment

<< Home