HtmlBuilder

HTML generator.

Requirements: ElementTree.

source , coloured source, py2html


 >>> from htmlbuilder import html
 >>>
 >>> banner = html.div( id='banner' )
 >>> banner.a( href='/index.html' ).h1( 'Header One' )
 <_HtmlElement h1 at b35960>
 >>> print banner
 <div id="banner"><a href="/index.html"><h1>Header One</h1></a></div>

Example

The following script generates the website of Our Lady of Bethlehem Abbey.


00001 #!/usr/bin/env python
00002 from htmlbuilder import html
00003 from textile import textile
00004 import os
00005 
00006 SITE_DIR = '/usr/local/www/bethlehem'
00007 
00008 DATA_DIR = '/usr/home/gerard/bethlehem/content'
00009 
00010 VERSIONS = [ 'en', 'pl' ]
00011 
00012 ENCODINGS = { 'en': 'latin-1', 'pl': 'ISO 8859-2' }
00013 
00014 BANNER_TITLE = { 'en': 'Our Lady of Bethlehem Abbey',
00015                 'pl': 'Opqctuo Naj&#347;wi&#281;tszej Maryi Panny z Betlejem'}
00016 
00017 MAIN_LINKS = ['index', 'history', 'life/index',
00018                         'guesthouse','repository','contact']
00019 
00020 MAIN_TITLES = {
00021                 'en':
00022                         ['Welcome', 'History', 'Life',
00023                         'Guests', 'Repository', 'Contact'],
00024                 'pl':
00025                         ['Witamy', 'Historia', 'Nasze Zycie',
00026                         'Dom Go&#347;cinny', 'Sklep', 'Kontakt']
00027                 }
00028 
00029 LIFE_LINKS = [ 'life/index', 'life/divine_office', 'life/personal_prayer',
00030                  'life/stability', 'life/imitation_of_christ',
00031                  'life/silence', 'life/poverty_and_work',
00032                  'life/vocation', 'life/lay_brothers', 'life/formation', 'life/timetable']
00033 
00034 LIFE_TITLES = {
00035                 'en':
00036                         ['The Rule of St. Benedict', 'The Divine Office', 'Personal Prayer',
00037                         'Stability', 'Imitation of Christ', 'Silence and Solitude',
00038                         'Poverty and Work', 'Vocation', 'Lay Brothers', 'Formation', 'Timetable'],
00039                 'pl':
00040                         ['The Rule of St. Benedict', 'The Divine Office', 'Personal Prayer',
00041                         'Stability', 'Imitation of Christ', 'Silence and Solitude',
00042                         'Poverty and Work', 'Vocation', 'Lay Brothers', 'Formation', 'Timetable'],
00043                 }
00044 
00045 
00046 def all_pages( lang ):
00047     return  zip( MAIN_LINKS, MAIN_TITLES[lang] ) + zip( LIFE_LINKS, LIFE_TITLES[lang] )
00048 
00049 def build_main_menu(lang="en",current="index"):
00050     menu = html.ul(id="navlist")
00051     for title,link in zip(MAIN_TITLES[lang],MAIN_LINKS):
00052         if link == current:
00053             link = '/%s/%s.html' % ( lang, link )
00054             menu.li(class_="active").a( class_="current", href=link).literal( title )
00055         else:
00056             link = '/%s/%s.html' % ( lang, link )
00057             menu.li.a(href=link).literal( title )
00058     return menu
00059 
00060 def build_life_sub_menu(lang="en", current="life/index"):
00061     submenu = html.table(style="margin-left: 1em;")
00062     row = submenu.tr
00063     i = 0
00064     for title, link in zip( LIFE_TITLES[lang], LIFE_LINKS ):
00065         i += 1
00066         css = "margin: 0.2em;padding: 0.4em;border: solid 1px black;"
00067         if link == current:
00068             css += "background:white;"
00069         else:
00070             css += "background:gainsboro;"
00071         row.td(style=css).a(str(i), href='/%s/%s.html' % (lang,link) )
00072         row.td.literal('&nbsp;')
00073     row.pop()
00074     return submenu
00075 
00076 def textile_to_html( filepath, encoding_ ):
00077     try:
00078         f = open ( filepath, 'r' )
00079         try:
00080             text = f.read()
00081             return html.literal(textile( text, encoding=encoding_))
00082         finally:
00083             f.close()
00084     except Exception, e:
00085         return html.literal( "Couldn\'t open file %s<br>error: %s" % (filepath, e) )
00086 
00087 def create_page_template( pagetitle ):
00088         title =  'Our Lady of Bethlehem - %s' % pagetitle
00089         page = html.page( title, styles=['/css/screen.css'] )
00090         container = page.div(id='container')
00091         banner = container.div( id="banner" ).div( class_="banner-text" )
00092         banner.placeholder("BANNER-TEXT")
00093         container.placeholder("MAINMENU")
00094         main = container.div(id='content')
00095         main.placeholder("SUBMENU")
00096         main.placeholder("CONTENT")
00097         main.hr
00098         main.placeholder("SUBMENU2")
00099         return page
00100 
00101 def write_page( outfile, pagetitle, bannertext, menu, submenu, content ):
00102         page = create_page_template( pagetitle )
00103         page %= [{"BANNER-TEXT": bannertext, "MAINMENU": menu, "SUBMENU": submenu,
00104                 "CONTENT": content, "SUBMENU2": submenu}]
00105         dir = os.path.dirname( outfile )
00106         if not os.path.exists( dir ):
00107                 os.makedirs( dir )
00108         print "Writing \'%s\'..." % outfile,
00109         f = open( outfile, "w" )
00110         try:
00111             page.tidywrite( f )
00112         finally:
00113                 f.close()
00114         print "Done."
00115 
00116 def write_index_page():
00117         bannertext = BANNER_TITLE['en']
00118         mainmenu = build_main_menu('en', 'index')
00119         submenu = ''
00120         contentfile = os.path.join( DATA_DIR, 'en', 'index.txt' )
00121         htmlfile = os.path.join( SITE_DIR, 'index.html' )
00122         content = textile_to_html( contentfile, ENCODINGS['en'] )
00123         write_page( htmlfile, 'Home', bannertext, mainmenu, submenu, content )
00124 
00125 if __name__ == '__main__':
00126     write_index_page()
00127     for lang in VERSIONS:
00128         bannertext = html.literal( BANNER_TITLE[lang] )
00129         for link, title in all_pages(lang):
00130             contentfile = os.path.join( DATA_DIR, lang, link + '.txt' )
00131             htmlfile = os.path.join( SITE_DIR, lang, link + '.html' )
00132             if link.startswith('life/'):
00133                 mainmenu = build_main_menu( lang, 'life/index' )
00134                 submenu =  build_life_sub_menu( lang, link )
00135             else:
00136                 mainmenu = build_main_menu( lang, link )
00137                 submenu = ''
00138             content = textile_to_html( contentfile, ENCODINGS[lang] )
00139             write_page( htmlfile, title, bannertext, mainmenu, submenu, content )