forked from Tox-Archive/Tox-Website.old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildsite.py
executable file
·81 lines (73 loc) · 2.97 KB
/
buildsite.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
#!/usr/bin/env python
from __future__ import print_function
import os
import pystache
import re
import shutil
import sys
try:
import simplejson
except ImportError:
import json as simplejson
AUTHORITATIVE = "en"
def do_format_template(name, template):
basename = ".".join(name.split(".")[:-1])
if not basename:
return
print("Enumerating languages... ", end="")
language_files = [lf for lf in os.listdir(os.getcwd())
if lf.endswith(".json") and lf.startswith(basename + ".")]
print("{0} found.".format(len(language_files)))
print("Using {0}.{1}.json as a base.".format(basename, AUTHORITATIVE))
with open("{0}.{1}.json".format(basename, AUTHORITATIVE), "r") as af:
main_values = simplejson.load(af)
renderer = pystache.renderer.Renderer()
lang_values = {}
lang_list = []
for language in language_files:
language = language.split(".")[-2]
if not re.match(r"^[A-Za-z0-9_.]+$", language):
continue
with open("{0}.{1}.json".format(basename, language), "r") as lf:
try:
values = simplejson.load(lf)
except ValueError:
print("Error in {0}.{1}.json!".format(basename, language))
raise
for key in values:
if isinstance(values[key], str):
values[key] = values[key].replace("\n", "<br>")
lang_list.append({"lang_name": values["_language"],
"lang_shortcode": language,
"ind": values["_ind"]})
temp = main_values.copy()
temp.update(values)
lang_values[language] = temp
lang_list.sort(key=lambda x: x["lang_shortcode"])
lang_list[-1]["last"] = 1
for language in language_files:
language = language.split(".")[-2]
with open("site/{1}.html".format(basename, language), "w") as outfile:
if sys.version_info.major == 2:
outfile.write(renderer.render(template, lang_values[language],
languages=lang_list).encode("utf-8"))
else:
outfile.write(renderer.render(template, lang_values[language],
languages=lang_list))
def main():
if not os.path.isdir(os.path.join(os.getcwd(), "site")):
os.mkdir("site")
if os.path.isdir(os.path.join("site", "assets")):
shutil.rmtree(os.path.join("site", "assets"))
shutil.copytree("assets", os.path.join("site", "assets"))
templates = [tp_name for tp_name in os.listdir(os.getcwd())
if tp_name.endswith(".mustache")]
for template_name in templates:
with open(template_name) as tmp_file:
print("Building template {0}.".format(template_name))
if sys.version_info.major == 2:
do_format_template(template_name, tmp_file.read().decode("utf-8"))
else:
do_format_template(template_name, tmp_file.read())
if __name__ == "__main__":
main()