2006-12-20 01:43:15 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2006-12-24 00:46:53 +01:00
|
|
|
import os, re
|
2006-12-20 01:43:15 +01:00
|
|
|
|
|
|
|
class TemplateWriter:
|
|
|
|
templatefile = "gerippe.tmpl"
|
|
|
|
tmpldir = "./placeholder"
|
|
|
|
contentdir = "./content"
|
|
|
|
tmplfileext = ".tmpl"
|
|
|
|
outfileext = ".html"
|
2006-12-24 00:46:53 +01:00
|
|
|
## 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
|
|
|
|
|
|
|
|
2006-12-24 00:46:53 +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
|
|
|
|
2006-12-24 00:46:53 +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()
|
2006-12-24 00:46:53 +01:00
|
|
|
open(outfile, "w").write(template)
|
2006-12-20 01:43:15 +01:00
|
|
|
text = {}
|
2006-12-24 00:46:53 +01:00
|
|
|
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):
|
2006-12-24 00:46:53 +01:00
|
|
|
print " str.repl: cancelling %s - not a file" % tmplfile
|
2006-12-20 01:43:15 +01:00
|
|
|
else:
|
|
|
|
template = open(outfile).read()
|
2006-12-24 00:46:53 +01:00
|
|
|
print " str.repl: using %s" % tmplfile
|
2006-12-20 01:43:15 +01:00
|
|
|
newcontent = open(tmplfile).read()
|
2006-12-24 00:46:53 +01:00
|
|
|
text[tmpl] = template.replace("<!-- $%s$ -->" % tmpl, newcontent)
|
|
|
|
open(outfile, "w").write(text[tmpl])
|
2006-12-20 01:43:15 +01:00
|
|
|
return
|
|
|
|
|
2006-12-24 00:46:53 +01:00
|
|
|
|
2006-12-20 01:43:15 +01:00
|
|
|
def build_sites(self):
|
|
|
|
'''use all files ending with .tmpl'''
|
2006-12-24 00:46:53 +01:00
|
|
|
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
|
2006-12-24 00:46:53 +01:00
|
|
|
print "building: %s -> %s" % (tmplfile, outfile)
|
2006-12-20 01:43:15 +01:00
|
|
|
self.string_replace(infile, outfile)
|
|
|
|
return
|
|
|
|
|
2006-12-24 00:46:53 +01:00
|
|
|
|
2006-12-20 01:43:15 +01:00
|
|
|
def build_sites_from_gerippe(self):
|
2006-12-24 00:46:53 +01:00
|
|
|
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 = ""
|
2006-12-24 00:46:53 +01:00
|
|
|
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()
|
2006-12-24 00:46:53 +01:00
|
|
|
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)
|
2006-12-24 00:46:53 +01:00
|
|
|
os.unlink(infile)
|
2006-12-20 01:43:15 +01:00
|
|
|
return
|
2006-12-24 00:46:53 +01:00
|
|
|
|
2006-12-20 01:43:15 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2006-12-24 00:46:53 +01:00
|
|
|
TemplateWriter().build_sites_from_gerippe()
|
|
|
|
|