cryptonas-website/templatewriter.py
lars 2daeb925a1 improved templatewrite code (ignores swap files, svn, ...)
added "details" page
added FAQ
changed a lot of text
2006-12-23 23:46:53 +00:00

88 lines
2.5 KiB
Python
Executable file

#!/usr/bin/env python
import os, re
class TemplateWriter:
templatefile = "gerippe.tmpl"
tmpldir = "./placeholder"
contentdir = "./content"
tmplfileext = ".tmpl"
outfileext = ".html"
## regular expressions of not-wanted file/directory names
## for now: no svn, no vi-swap files, no backup files
ignore_items = [ r'\.svn', r'\.swp$', r'~$' ]
def __init__(self):
pass
def get_sorted(self, flist):
result = flist[:]
result.sort()
return result
def get_filtered(self, flist):
result = []
for item in flist:
found = False
for expression in self.ignore_items:
if re.search(expression, item):
found = True
continue
if not found:
result.append(item)
return result
def string_replace(self, infile, outfile):
template = open(infile).read()
open(outfile, "w").write(template)
text = {}
for tmpl in self.get_sorted(self.get_filtered(os.listdir(self.tmpldir))):
tmplfile = str(os.path.join(self.tmpldir, tmpl))
if not os.path.isfile(tmplfile):
print " str.repl: cancelling %s - not a file" % tmplfile
else:
template = open(outfile).read()
print " str.repl: using %s" % tmplfile
newcontent = open(tmplfile).read()
text[tmpl] = template.replace("<!-- $%s$ -->" % tmpl, newcontent)
open(outfile, "w").write(text[tmpl])
return
def build_sites(self):
'''use all files ending with .tmpl'''
for tmplfile in self.get_sorted(self.get_filtered(os.listdir("./"))):
if tmplfile.rfind(self.tmplfileext) >= 1:
infile = tmplfile
(tmplfilename, tmplfileext)=os.path.splitext(tmplfile)
outfile = tmplfilename + self.outfileext
print "building: %s -> %s" % (tmplfile, outfile)
self.string_replace(infile, outfile)
return
def build_sites_from_gerippe(self):
print "Let's build some html files from: %s" % self.templatefile
for html in self.get_sorted(self.get_filtered(os.listdir(self.contentdir))):
print " building: %s%s" % (html, self.outfileext)
entries = ""
for entry in self.get_sorted(self.get_filtered(os.listdir(os.path.join(self.contentdir, html)))):
print " adding entry: %s " % entry
entries += open(os.path.join(self.contentdir, html, entry)).read()
template = open(self.templatefile).read()
text = template.replace("<!-- $entries$ -->", entries)
infile = html+".tmp"
open(infile,"w").write(text)
outfile = html+self.outfileext
self.string_replace(infile, outfile)
os.unlink(infile)
return
if __name__ == "__main__":
TemplateWriter().build_sites_from_gerippe()