Christopher Lenz wrote:
I've been hacking away yesterday at "yet another HTML builder" lib
which would probably be useful here:
http://projects.edgewall.com/trac/attachment/wiki/ChristopherLenz/html.py
Example usage:
...
This is intended to be used in the various places in Trac where we
generate little HTML snippets.
I like it very much!
I tried it for my moderately complex TracIni macro,
and it nearly worked out-of-the-box.
A few suggestions:
* add an easy way to add list of nodes.
Fragment.__get_item__ should accept iterable,
as in the example below (TBODY(rows))
* make it easy to play well with wiki_to_... functions
Fragment.append should know about Markup,
so that a Markup object doesn't get converted
to a string before being escaped.in order
-- Christian
1) excerpt of TracIni macro which creates an HTML table:
...
rows = []
for section, configs, section_doc in default_config:
...
row = TR()[TH(rowspan=nconfigs*2)[section]]
for name, value, since, doc in configs:
...
html_doc = wiki_to_html(doc, self.env, req)
row.append(TD(class_="name")[name])
row.append(TD(class_="defaultvalue")[value])
rows.append(row)
rows.append(TR()[TD(colspan="2", class_="doc")[html_doc]])
row = TR()
if not rows:
return ''
add_stylesheet(req, 'common/css/about.css')
return str(DIV(class_="about_config")[
TABLE()[THEAD()[TR()[TH(class_="section")["Section"],
TH(class_="name")["Name"],
TH(class_="defaultvalue")["Default
Value"]]],
TBODY()[rows]]])
2) patch to html.py which is needed by the above code
--- trac/util/html.orig.py 2006-03-10 12:05:55.296875000 +0100
+++ trac/util/html.py 2006-03-10 13:38:32.031250000 +0100
@@ -282,12 +282,15 @@
def append(self, node):
"""Append an element or string as child node."""
- if isinstance(node, Element):
+ if isinstance(node, (Markup, Element)):
self.children.append(node)
elif isinstance(node, Fragment):
self.children += node.children
elif node:
- self.children.append(node)
+ try:
+ self.children += iter(node)
+ except TypeError:
+ self.children.append(node)
def __getitem__(self, nodes):
"""Add child nodes to the element."""
_______________________________________________
Trac-dev mailing list
[email protected]
http://lists.edgewall.com/mailman/listinfo/trac-dev