improved templatewrite code (ignores swap files, svn, ...)
added "details" page added FAQ changed a lot of text
This commit is contained in:
parent
54559af141
commit
2daeb925a1
26 changed files with 868 additions and 334 deletions
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import string, os
|
||||
import os, re
|
||||
|
||||
class TemplateWriter:
|
||||
templatefile = "gerippe.tmpl"
|
||||
|
@ -8,73 +8,81 @@ class TemplateWriter:
|
|||
contentdir = "./content"
|
||||
tmplfileext = ".tmpl"
|
||||
outfileext = ".html"
|
||||
ignore_items = [ ".svn" ]
|
||||
## 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, list):
|
||||
result = list[:]
|
||||
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)
|
||||
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)
|
||||
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 "+tmplfile+" - not a file"
|
||||
print " str.repl: cancelling %s - not a file" % tmplfile
|
||||
else:
|
||||
template = open(outfile).read()
|
||||
print " str.repl: using "+tmplfile
|
||||
print " str.repl: using %s" % tmplfile
|
||||
newcontent = open(tmplfile).read()
|
||||
text[tmpl] = string.replace(template,"<!-- $"+tmpl+"$ -->",newcontent)
|
||||
open(outfile,"w").write(text[tmpl])
|
||||
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(os.listdir("./")):
|
||||
if tmplfile in self.ignore_items:
|
||||
continue
|
||||
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: "+tmplfile+" -> "+outfile
|
||||
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: "+self.templatefile
|
||||
for html in self.get_sorted(os.listdir(self.contentdir)):
|
||||
if html in self.ignore_items:
|
||||
continue
|
||||
print " building: "+html+self.outfileext
|
||||
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(os.listdir(self.contentdir+"/"+html)):
|
||||
if entry in self.ignore_items:
|
||||
continue
|
||||
print " adding entry: "+entry
|
||||
entries += open(self.contentdir+"/"+html+"/"+entry).read()
|
||||
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 = string.replace(template,"<!-- $entries$ -->",entries)
|
||||
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__":
|
||||
foo = TemplateWriter()
|
||||
foo.build_sites_from_gerippe()
|
||||
#foo.string_replace("gerippe.tmpl", "gerippe.html) # for single tests
|
||||
#foo.build_sites() # oldfashiond version
|
||||
TemplateWriter().build_sites_from_gerippe()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue