summaryrefslogtreecommitdiff
path: root/scripts/template.py
blob: a4d4e9ef98ed5f05a51d99091cedeb86e74b9c1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
#Filename: newtemp.py
#Author: stayawake@lavabit.com


import os
import os.path
import sys
import getopt
import time
import datetime
from string import Template
import ConfigParser

def readdir(directory):
	return_value = {"content.html":"", "keywords":"", "desc_de":"", "desc_en":""}
	
	for c in return_value:
		f = open(os.path.join(directory, c), "r")
		return_value[c] = f.read()
		f.close
	return return_value

def substitute(target, prefix, htmltemplate, datenow, topic, content):
	
	#create template-string
	htmltext = Template(htmltemplate)
	htmltext = htmltext.substitute(
		template_date = datenow,
		template_chattopic = topic,
		template_keywords = content["keywords"],
		template_content = content["content.html"],
		template_desc_de = content["desc_de"],
		template_desc_en = content["desc_en"]
	)

	#write it to the outputfile
	outputfile = open(os.path.join(target, page + ".html"), "w")
	outputfile.write(htmltext)
	outputfile.close()


def readarguments(argv):
	
	global configfile
	global verbose

	try:
		opts, args = getopt.getopt(argv, "c:vh", ["config=", "verbose", "help"])
	except getopt.GetoptError:
		print "There is a problem with your argument(s)"
		sys.exit(2)
	for opt, arg in opts:
		if opt in ("-c", "--config"):
			configfile = arg
		elif opt in ("-v", "--verbose"):
			verbose = True
		elif opt in ("-h", "--help"):
			print "Please read the non-existing man page for further assistance"
			sys.exit(0)


configfile = "template.conf"
verbose = False
basedir = os.path.dirname(os.path.abspath(__file__))

readarguments(sys.argv[1:])

config = ConfigParser.ConfigParser()
if not os.path.isabs(configfile):
	configfile = os.path.join(basedir, configfile)
config.read(configfile)
path = config.get("Default", "path")
if not os.path.isabs(path):
	path = os.path.join(basedir, path)

target = config.get("Default", "target")
if not os.path.isabs(target):
	target = os.path.join(basedir, target)

pagepath = os.path.join(path, "pages")
templatepath = os.path.join(path, "template")
topicpath = os.path.join(path, "topic")

if verbose:
	print "Pagepath: %s" % pagepath
	print "Templatepath: %s" % templatepath
	print "topicpath: %s" % topicpath

#set template
f = open(os.path.join(templatepath, "template.html"), "r")
htmltemplate = f.read()
f.close
if verbose:
	print "template read"

#set topic
f = open(os.path.join(topicpath, "topic"), "r")
topic = f.read()
f.close
if verbose:
	print "topic read"

#set date
datenow = time.strftime("%Y-%m-%dT%H:%M:%S +0100")
if verbose:
	print "Set date: " + datenow

for page in os.listdir(pagepath):
	prefix = os.path.join(pagepath, page)
	page_data = readdir(prefix)
	substitute(target, prefix, htmltemplate, datenow, topic, page_data)
	if verbose:
		print "%s.html written" % prefix
# vi: noexpandtab:tabstop=8:shiftwidth=8