cryptonas-website/templatewriter.py

89 lines
2.5 KiB
Python
Raw Normal View History

2006-12-20 01:43:15 +01:00
#!/usr/bin/env python
import os, re
2006-12-20 01:43:15 +01:00
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'~$' ]
2006-12-20 01:43:15 +01:00
def __init__(self):
pass
2006-12-20 02:37:42 +01:00
def get_sorted(self, flist):
result = flist[:]
2006-12-20 02:37:42 +01:00
result.sort()
return result
2006-12-20 01:43:15 +01:00
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
2006-12-20 01:43:15 +01:00
def string_replace(self, infile, outfile):
template = open(infile).read()
open(outfile, "w").write(template)
2006-12-20 01:43:15 +01:00
text = {}
for tmpl in self.get_sorted(self.get_filtered(os.listdir(self.tmpldir))):
tmplfile = str(os.path.join(self.tmpldir, tmpl))
2006-12-20 01:43:15 +01:00
if not os.path.isfile(tmplfile):
print " str.repl: cancelling %s - not a file" % tmplfile
2006-12-20 01:43:15 +01:00
else:
template = open(outfile).read()
print " str.repl: using %s" % tmplfile
2006-12-20 01:43:15 +01:00
newcontent = open(tmplfile).read()
text[tmpl] = template.replace("<!-- $%s$ -->" % tmpl, newcontent)
open(outfile, "w").write(text[tmpl])
2006-12-20 01:43:15 +01:00
return
2006-12-20 01:43:15 +01:00
def build_sites(self):
'''use all files ending with .tmpl'''
for tmplfile in self.get_sorted(self.get_filtered(os.listdir("./"))):
2006-12-20 01:43:15 +01:00
if tmplfile.rfind(self.tmplfileext) >= 1:
infile = tmplfile
(tmplfilename, tmplfileext)=os.path.splitext(tmplfile)
outfile = tmplfilename + self.outfileext
print "building: %s -> %s" % (tmplfile, outfile)
2006-12-20 01:43:15 +01:00
self.string_replace(infile, outfile)
return
2006-12-20 01:43:15 +01:00
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)
2006-12-20 01:43:15 +01:00
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()
2006-12-20 01:43:15 +01:00
template = open(self.templatefile).read()
text = template.replace("<!-- $entries$ -->", entries)
2006-12-20 01:43:15 +01:00
infile = html+".tmp"
open(infile,"w").write(text)
outfile = html+self.outfileext
self.string_replace(infile, outfile)
os.unlink(infile)
2006-12-20 01:43:15 +01:00
return
2006-12-20 01:43:15 +01:00
if __name__ == "__main__":
TemplateWriter().build_sites_from_gerippe()