-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-website.py
executable file
·90 lines (65 loc) · 2.46 KB
/
build-website.py
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
#!/usr/bin/python
from __future__ import with_statement
"""Builds the website from django templates and language files.
Uses the django template system to create static files for the website. This
allows easy inclusion of common content, but removes the need for (e.g.) PHP
includes that don't really do anything dynamic.
You can build the website for all languages, by doing:
$ python build-website.py
This requires the python-django package on debian.
"""
__author__ = "Ximin Luo <infinity0@gmx.com>"
__license__ = "GPL v3"
# more links on django template syntax:
# http://www.djangobook.com/en/beta/chapter04/
# http://docs.djangoproject.com/en/dev/topics/templates/
# this would normally be defined in the settings module, but we've set that to __main__, as above
TEMPLATE_DIRS = ('src',)
OUTPUT_DIR = "../opensukey.org"
WEBSITE_FILES = ["index.html","what.html","articles.html","blog.html","contact.html","donate.html"]
STATIC_DIRS = ["css", "img", "js"]
STRINGS_FILE = "l10n/strings.csv"
###############################################################################
import sys, os, shutil
os.environ['DJANGO_SETTINGS_MODULE'] = '__main__'
import csv
import itertools
from django.template.loader import get_template
from django.template import Context, Template
def load_strings():
strings = None
with open(STRINGS_FILE) as fp:
row_iter = csv.reader(fp)
langs = row_iter.next()[1:]
strings = dict((lang, {}) for lang in langs)
for row in row_iter:
if len(row) < 2: continue
key = row[0]
default_value = row[1]
for lang, value in itertools.izip_longest(langs, row[1:], fillvalue=""):
strings[lang][key] = value if value.strip() else default_value
return strings
def build_lang(lang, strings):
out_dir = os.path.join(OUTPUT_DIR, lang)
shutil.rmtree(out_dir, ignore_errors=True)
if not os.path.exists(out_dir):
os.mkdir(out_dir)
print >>sys.stderr, "building to %s..." % out_dir
for fn in WEBSITE_FILES:
t = get_template("%s.djt" % fn)
c = Context({"strings": strings})
with open(os.path.join(out_dir, fn), 'w') as fp:
print >>fp, t.render(c)
def main(*args):
shutil.rmtree(OUTPUT_DIR, ignore_errors=True)
os.mkdir(OUTPUT_DIR)
STRINGS = load_strings()
if not args: args = STRINGS.keys()
for arg in args:
build_lang(arg, STRINGS[arg])
for tree in STATIC_DIRS:
out_dir = os.path.join(OUTPUT_DIR, tree)
print >>sys.stderr, "copying to %s..." % out_dir
shutil.copytree(tree, out_dir)
if __name__ == "__main__":
sys.exit(main(*sys.argv[1:]))