codekasten/tmpl2html/templatewriter.py

65 lines
1.9 KiB
Python
Raw Normal View History

2006-07-22 03:51:35 +02:00
#!/usr/bin/env python
import string, os
class TemplateWriter:
templatefile = "gerippe.tmpl"
tmpldir = "./placeholder"
contentdir = "./content"
tmplfileext = ".tmpl"
outfileext = ".html"
def __init__(self):
pass
def string_replace(self, infile, outfile):
template = open(infile).read()
open(outfile,"w").write(template)
text = {}
for tmpl in os.listdir(self.tmpldir):
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 os.listdir("./"):
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 os.listdir(self.contentdir):
print " building: "+html+self.outfileext
entries = ""
for entry in os.listdir(self.contentdir+"/"+html):
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