[ann] markup.py - HTML/XML generator

D

Daniel Nogradi

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.sourceforget.net/

Oh, and the code is in the public domain.
 
G

Gerard Flanagan

Daniel said:
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
 
D

Daniel Nogradi

Just in case the thought of not having a millionth implementation of a
(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


Well, I'm glad you like it! And thanks for correcting the link, I
didn't even notice, so the correct link is

http://markup.sourceforge.net/

But I guess that was pretty obvious :) Your implementation also looks
all right, the only thing is that it has dependencies outside the
stdlib which some people might find not so nice.
 
F

Fredrik Lundh

Gerard said:
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!

just wait until you figure out what iteritems do ;-)

:::

I'm sure you already know it, but dictionaries aren't ordered, so that
navigation bar may not come out as you'd expect.
{'/cart': 'Cart', '/shop': 'Shop', '/home': 'Home'}

</F>
 
G

Gerard Flanagan

Fredrik said:
just wait until you figure out what iteritems do ;-)

:::

I'm sure you already know it, but dictionaries aren't ordered, so that
navigation bar may not come out as you'd expect.

{'/cart': 'Cart', '/shop': 'Shop', '/home': 'Home'}

Yes, ran into that yesterday! I knew dictionaries weren't ordered but
I assumed for simple tests like above, without any
additions/deletions, they would 'line up right' - seems that can't be
guaranteed.

All the best.

Gerard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top