cryptonas-website/templatewriter.py
lars a5bc96fb7e content update
templatewriter improved
2006-12-20 01:37:42 +00:00

81 lines
2.3 KiB
Python
Executable file

#!/usr/bin/env python
import string, os
class TemplateWriter:
templatefile = "gerippe.tmpl"
tmpldir = "./placeholder"
contentdir = "./content"
tmplfileext = ".tmpl"
outfileext = ".html"
ignore_items = [ ".svn" ]
def __init__(self):
pass
def get_sorted(self, list):
result = list[:]
result.sort()
return result
def string_replace(self, infile, outfile):
template = open(infile).read()
open(outfile,"w").write(template)
text = {}
for tmpl in self.get_sorted(os.listdir(self.tmpldir)):
if tmpl in self.ignore_items:
continue
tmplfile = str(self.tmpldir+"/"+tmpl)
if not os.path.isfile(tmplfile):
print " str.repl: cancelling "+tmplfile+" - not a file"
else:
template = open(outfile).read()
print " str.repl: using "+tmplfile
newcontent = open(tmplfile).read()
text[tmpl] = string.replace(template,"<!-- $"+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(os.listdir("./")):
if tmplfile in self.ignore_items:
continue
if tmplfile.rfind(self.tmplfileext) >= 1:
infile = tmplfile
(tmplfilename, tmplfileext)=os.path.splitext(tmplfile)
outfile = tmplfilename + self.outfileext
print "building: "+tmplfile+" -> "+outfile
self.string_replace(infile, outfile)
return
def build_sites_from_gerippe(self):
print "Let's build some html files from: "+self.templatefile
for html in self.get_sorted(os.listdir(self.contentdir)):
if html in self.ignore_items:
continue
print " building: "+html+self.outfileext
entries = ""
for entry in self.get_sorted(os.listdir(self.contentdir+"/"+html)):
if entry in self.ignore_items:
continue
print " adding entry: "+entry
entries += open(self.contentdir+"/"+html+"/"+entry).read()
template = open(self.templatefile).read()
text = string.replace(template,"<!-- $entries$ -->",entries)
infile = html+".tmp"
open(infile,"w").write(text)
outfile = html+self.outfileext
self.string_replace(infile, outfile)
return
if __name__ == "__main__":
foo = TemplateWriter()
foo.build_sites_from_gerippe()
#foo.string_replace("gerippe.tmpl", "gerippe.html) # for single tests
#foo.build_sites() # oldfashiond version