This is an example program that uses the Cheetah template substitution engine for Python.

The way it works is you put a bunch of pictures inside various subfolders. This program looks for .JPGs in those folders and builds a static web site automatically. First, let's look at a couple example template files:

piclayout.htm

<html>
#include "template/head.htm"
<body bgcolor="white">
<table>
<tr>
#include "template/pictoc.htm"
#include "template/gutter.htm"
#include "template/pic.htm"
</tr>
</table>
</body>
</html>

pictoc.htm

<td valign="top" width="180">
#include direct "template/logo.htm"
<font color="blue" align="right">
(<a href="../misc/pictures.htm">SEE MORE PICTURES</a>)<br>
(<a href="${PicInfo.prev}.htm">Previous</a>
 | <a href="${PicInfo.next}.htm">Next</a>)<br>
</font>
<br>
<small>$DirInfo.title<br><br>
#for $entry in $DirInfo.pics
#if ($entry.fn == $PicInfo.fn)
  $entry.title<br>
#else
  <a href="${entry.fn}.htm">$entry.title</a><br>
#end if
#end for
</small></td>

And here's the Python code that glues it all together.

buildsite.py

from Cheetah.Template import Template
import os, glob

def make_output_dir(dir):
    dir = 'output/htm/' + dir 
    os.mkdir(dir)   

def output_htm_file(dir,fn):
    file = 'output/htm/' + dir + '/' + fn + '.htm'
    try:
        f = open(file,'w')
    except IOError:
        make_output_dir(dir)
        f = open(file,'w')
    return f

def GeneratePage(template, dir, output_fn, nameSpace):
    templateObj = Template(None, nameSpace, None, file=template)
    f = output_htm_file(dir, output_fn)
    f.write(templateObj.respond())
    f.close

class PicInfo:
    def generate_page(self, DirInfo):
        dir = DirInfo.dir
        self.srcfile = '../../img/' + dir + '/' + self.fn + '.JPG'
        nameSpace = {
            'PicInfo'   : self,
            'DirInfo'   : DirInfo,
        }
        GeneratePage("template/piclayout.htm", dir, self.fn, nameSpace)

class DirInfo:
    def __init__(self, dir):
        self.dir = dir

    def read_config_py(self):
        file = 'config/' + self.dir + '.py'
        self.title = self.dir
        self.pics = []
        self.PicDict = {}
        self.desc = "(newly added pictures)"
        try:
            f = open(file,'r')
            exec f
            f.close
            for pd in pics:
                pic         = PicInfo()
                pic.fn      = pd['fn']
                pic.title   = pd['title']
                pic.caption = pd['caption']
                self.PicDict[pic.fn] = 1
                self.pics.append(pic)
        except IOError:
            pass
    
    def get_files(self):
        jpgs = glob.glob('output/img/'+self.dir+'/*.JPG')
        if jpgs.count == 0:
            raise "Empty imgs dir "+self.dir
        jpgs.sort()
        for f in jpgs:
            (fn, ext) = os.path.splitext(os.path.basename(f))
            if not self.PicDict.has_key(fn): 
                pic = PicInfo()
                pic.fn      = fn
                pic.title   = fn
                pic.caption = ""
                self.pics.append(pic)
        self.fn = '../' + self.dir + '/' + self.pics[0].fn

    def prev_next(self):
        cnt = len(self.pics)
        i = 0
        j = cnt - 1  
        while i < cnt:
            self.pics[j].next = self.pics[i].fn
            self.pics[i].prev = self.pics[j].fn
            j = i 
            i = i + 1

    def generate_all_pages(self):
        self.read_config_py()
        self.get_files()
        self.prev_next()
        for pic in self.pics:
            pic.generate_page(self)

class SiteBuilder:
    def BuildSite(self):
        self.DirList = []
        dirs = glob.glob('output\img\*')   # BUG? "output/img/" shurely?
        dirs.sort()
        # You can override the sort order of dirs here.
        # Example:
        # dirs = ['millers', 'bentons']
        if dirs.count == 0:
            raise "No image dirs"
        for d in dirs:
            print 'Building files for ',d,'...',
            dirInfo = DirInfo(os.path.basename(d))
            dirInfo.generate_all_pages()
            self.DirList.append(dirInfo)
            print 'done'
        self.build_main_page()

    def build_main_page(self):
        GeneratePage("template/mainlayout.htm", "misc", "pictures", 
            {'DirList': self.DirList})

sb = SiteBuilder()
sb.BuildSite()

If you are interested in using CheetahTemplate, and have questions, ask me or MikeOrr for more info. -- SteveHowell


Sorry, I no longer have the complete source for this code. -- SteveHowell

CheetahExample (last edited 2008-03-04 08:33:25 by localhost)