[Web-SIG] html-gen-ish libraries; what's best?

Jacob Smullyan smulloni at smullyan.org
Wed Apr 5 05:40:35 CEST 2006


On Tue, Apr 04, 2006 at 05:26:28PM -0500, Ian Bicking wrote:
> Opinions?

I have an opinion about a related matter, namely, a convenience syntax
for generating nodes.  

One thing I like a lot about stan is its general thrust towards having
a concise, usable, sexp-ish syntax for this task that permits one to
view the document you are creating within Python almost as if it were
markup itself.  However, stan quite deliberately tortures Python
syntax with zany overloading, and while that can be enjoyed for its
own sake (I'd hardly want to discourage zaniness in general, we need
more of it), I don't consider it optimal.  For my work I sometimes use
a very simple module with which I can generate fragments like so:

   node=N('html', 
          A(xmlns='http://www.w3.org/1999/xhtml'),
          ('head',
           N('link', rel='stylesheet', href="styles.css"),
           ('title', 'This is a title')),
          ('body',
           ('p', A('class', 'major'), sometext)))
          
N is short for Node, A for Attribute; tuples are coerced to nodes, and
constructors are flexible so that attributes can be specified either
as keyword arguments, where convenient, or positionally (via the
Attribute class). 
          
My little implementation is a quick hack using elementtree's
SimpleXMLWriter as a back end; if anyone wants it I can post it, but
it is feature-poor and pretty obvious.  My plea is not for it, but
that any such hypothetical Beast of Breed should expose an sexp-ish
way to create documents and fragments (it needn't be the only way),
for which the syntax above might be a conversation starter, and that
such a syntax should behave like Python, and not override access
syntax to do mutation, etc., so that mixing the syntax with Python
will be natural, legible, and free from cognitive dissonance.

Cheers,

js

-- 
Jacob Smullyan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/web-sig/attachments/20060404/a2a43382/attachment.pgp 


More information about the Web-SIG mailing list