summaryrefslogtreecommitdiff
path: root/scripts/template.py
blob: 91cfdd3617af6f94b689fd1a32fed7c9f5187667 (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
116
117
118
119
120
121
122
123
#!/usr/bin/env python
#Filename: newtemp.py
#Author: stayawake@lavabit.com
# ---
# Copyright (C) 2011 Christian Franke <nobody@nowhere.ws>
#
# Permission is hereby granted to use and distribute this
# file for any purpose, provided the above copyright notice
# is kept in place.
#

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

import plugin

class Page:
	def __init__(self, name):
		self.name = name
		data = {
			"content.html":"",
			"keywords":"",
			"desc_de":"",
			"desc_en":"",
			"head.html":"",
		}

		for c in data:
			try:
				with open(os.path.join(pagepath, name, c), "r") as f:
					data[c] = f.read()
			except Exception:
				if c != "head.html":
					sys.excepthook(*sys.exc_info())

		self._data = plugin.plugin_manager.process_content(data)

	@property
	def html(self):
		#create template-string
		htmltext = Template(htmltemplate)
		htmltext = htmltext.substitute(
			template_date = datenow,
			template_chattopic = topic,
			template_keywords = self._data["keywords"],
			template_content = self._data["content.html"],
			template_desc_de = self._data["desc_de"],
			template_desc_en = self._data["desc_en"],
			template_head = self._data["head.html"]
		)
		return htmltext

	def render_and_save(self):
		#write it to the outputfile
		outputfile = open(os.path.join(targetdir, self.name + '.html'), "w")
		outputfile.write(self.html)
		outputfile.close()

configfile = "template.conf"
verbose = False
opts, args = getopt.getopt(sys.argv[1:], "c:vh", ["config=", "verbose", "help"])
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)

basedir = os.path.dirname(os.path.abspath(__file__))

def makeabs(path):
	if not os.path.isabs(path):
		return os.path.join(basedir, path)
	return path

configfile = makeabs(configfile)
config = ConfigParser.ConfigParser()
config.read(configfile)

path = makeabs(config.get("Default", "path"))
targetdir = makeabs(config.get("Default", "target"))
pagepath = os.path.join(path, "pages")
templatepath = os.path.join(path, "template")
topicpath = os.path.join(path, "topic")

#set template
htmltemplate = open(os.path.join(templatepath, "template.html"), "r").read()
topic = open(os.path.join(topicpath, "topic"), "r").read()
datenow = time.strftime("%Y-%m-%dT%H:%M:%S +0100")

if __name__ == '__main__':
	if verbose:
		print "Pagepath: %s" % pagepath
		print "Templatepath: %s" % templatepath
		print "topicpath: %s" % topicpath
		print "datenow: %s" % datenow

	pluginlist = config.items('Plugins')
	for pluginname, pluginarg in pluginlist:
		modinfo = imp.find_module(pluginname)
		try:
			imp.load_module(pluginname, *modinfo)
		finally:
			modinfo[0].close()

if __name__ == '__main__':
	for page in os.listdir(pagepath):
		if page.endswith('_template'):
			continue
		Page(page).render_and_save()
		if verbose:
			print "%s.html written" % page

# vi: noexpandtab:tabstop=8:shiftwidth=8