markup.py - HTML/XML generator

Gerard Flanagan grflanagan at yahoo.co.uk
Mon Mar 20 12:27:09 CET 2006


Daniel Nogradi wrote:

> Just in case the thought of not having a millionth implementation of a
> HTML/XML generator for Python makes the world as we know it a
> miserable place, well then your suffering might be over soon since
> exactly the one millionth implementation is out. You can download
> markup.py from
>
> http://markup.sourceforge.net/
>

(Corrected the link).

You opened a door I was pushing!

I wrote a HTML generator a few weeks ago to generate a static site, but
it was only a little less tortuous to use than writing out the HTML by
hand.  I wanted to do things the way  'markup.py' does but couldn't
manage it. Then I read your code, added straightforward '__getattr__'
and '__call__' methods to my own code, and now I need only half the
'LOC' that I did before.

Before I did this:

    page = HtmlPage('Test Page')
    left_div = page.append( HtmlElement('div', id='left') )
    main_div = page.append( HtmlElement('div', id='main') )
    navbar = HtmlElement('ul', css='navbar')
    for href,link in {'/home':'Home', '/shop':'Shop',
'/cart':'Cart'}.iteritems():
        li = navbar.append( HtmlElement('li') )
        li.append( HtmlElement('a',link, href=href) )
    left_div.append(navbar)
    main_div.append( HtmlElement('h1','Header') )
    main_div.append( HtmlElement('p','<Text Goes Here>') )

Now I can do this:

    page = HtmlPage('Test Page')
    navbar = page.div(id='left').ul(css='navbar')
    for href,link in {'/home':'Home', '/shop':'Shop',
'/cart':'Cart'}.iteritems():
        navbar.li.a(link,href=href)
    page.div(id='main').h1('Header').p('<Text Goes Here>')


So *that's* what '__call__' does - I am enlightened!

Thanks a lot.

Here's mine:

http://www.gflanagan.net/site/python/htmlbuilder/htmlbuilder.py

(So, 'More than one million ways to do it' then...)

Gerard




More information about the Python-list mailing list