pybaker
pybaker is a Builder clone for generating XML.
introduction
Anonymous blocks (see PEP 343) are of course insanely cool. whytheluckystiff explains it better in this short film:
So what are we going to use it for? For making a Builder clone of course! Since what the world needs is yet another templating language...
The usage of this library is different from the original Ruby Builder; since Python has no #method_missing (that I know of) you need to explicitly call the element method... or use the equivalent tag method which is not really correctly named but much shorter to type.
example code
This code:
head = Element("head") with head as h: with h.tag("title") as t: t.text("pybaker") foo = XmlDocument("html", doctype='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', attributes={ 'xmlns':"http://www.w3.org/1999/xhtml", 'lang':"en", 'xml:lang':"en"}) foo.contents.append(head) with foo as f: with foo.tag("body") as body: with body.tag("p") as p: p.text("blah blah & yadda yadda") p.comment("replace with praise for baker.py") p.tag("br") p.text("that's all!")
... will produce something like this: (I put in a few line endings.)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><title>builder.py</title></head> <body> <p>blah blah & yadda yadda<!-- replace with praise for baker.py --><br />that's all!</p> </body></html>
Well... have fun!