diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-02-14 21:07:41 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-02-14 21:09:33 +0100 |
commit | 197f0a626dc333a4956a541ce556abb87bda8b46 (patch) | |
tree | 8f0750968d865f4a52894a3aff05babf905ca3dc | |
parent | 456a61874e68847d1a7099831fa6247e6cbdc276 (diff) | |
download | www-197f0a626dc333a4956a541ce556abb87bda8b46.tar.gz www-197f0a626dc333a4956a541ce556abb87bda8b46.zip |
make template compilation a LOT faster
By not doing template parsing and starting the python interpreter every
time, templating only takes about a second on my machine now.
-rw-r--r-- | Makefile | 2 | ||||
-rwxr-xr-x | template.py | 59 | ||||
-rwxr-xr-x | template.sh | 17 |
3 files changed, 32 insertions, 46 deletions
@@ -33,4 +33,4 @@ locale: locale-update locale-compile | |||
33 | # Run the jinga2 templating engine to expand templates to HTML | 33 | # Run the jinga2 templating engine to expand templates to HTML |
34 | # incorporating translations. | 34 | # incorporating translations. |
35 | template: locale-compile | 35 | template: locale-compile |
36 | ./template.sh | 36 | ./template.py |
diff --git a/template.py b/template.py index f872ad3e..179e32a7 100755 --- a/template.py +++ b/template.py | |||
@@ -8,45 +8,48 @@ | |||
8 | # Note that the gettext files need to be prepared first. This script | 8 | # Note that the gettext files need to be prepared first. This script |
9 | # is thus to be invoked via the Makefile. | 9 | # is thus to be invoked via the Makefile. |
10 | import os | 10 | import os |
11 | import os.path | ||
11 | import sys | 12 | import sys |
12 | import re | 13 | import re |
13 | import gettext | 14 | import gettext |
14 | import jinja2 | 15 | import jinja2 |
16 | import glob | ||
17 | import codecs | ||
15 | 18 | ||
16 | if len(sys.argv) < 3: | 19 | env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), |
17 | sys.exit("Usage: " + __file__ + " <template-file> <locale> <output-file>") | 20 | extensions=["jinja2.ext.i18n"], |
21 | autoescape=False) | ||
18 | 22 | ||
19 | in_file = sys.argv[1] | ||
20 | locale = sys.argv[2] | ||
21 | 23 | ||
22 | name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups() | 24 | for in_file in glob.glob("*.j2"): |
25 | name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups() | ||
26 | tmpl = env.get_template(in_file) | ||
23 | 27 | ||
24 | tr = gettext.translation("messages", | 28 | def self_localized(other_locale): |
25 | localedir="locale", | 29 | """ |
26 | languages=[locale]) | 30 | Return URL for the current page in another locale. |
31 | """ | ||
32 | return "../" + other_locale + "/" + in_file.rstrip(".j2") | ||
27 | 33 | ||
28 | env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), | 34 | def url_localized(filename): |
29 | extensions=["jinja2.ext.i18n"], | 35 | return "../" + locale + "/" + filename |
30 | autoescape=False) | ||
31 | env.install_gettext_translations(tr, newstyle=True) | ||
32 | 36 | ||
33 | tmpl = env.get_template(in_file) | 37 | def url(x): |
38 | # TODO: look at the app root environment variable | ||
39 | # TODO: check if file exists | ||
40 | return "../" + x | ||
34 | 41 | ||
35 | def self_localized(other_locale): | 42 | for l in glob.glob("locale/*/"): |
36 | """ | 43 | locale = os.path.basename(l[:-1]) |
37 | Return URL for the current page in another locale. | ||
38 | """ | ||
39 | return "../" + other_locale + "/" + in_file.rstrip(".j2") | ||
40 | 44 | ||
41 | def url_localized(filename): | 45 | tr = gettext.translation("messages", |
42 | return "../" + locale + "/" + filename | 46 | localedir="locale", |
47 | languages=[locale]) | ||
43 | 48 | ||
44 | def url(x): | 49 | env.install_gettext_translations(tr, newstyle=True) |
45 | # TODO: look at the app root environment variable | ||
46 | # TODO: check if file exists | ||
47 | return "../" + x | ||
48 | 50 | ||
49 | import codecs | 51 | |
50 | f = codecs.open("./" + locale + "/" + in_file.rstrip(".j2"), "w", "utf-8") | 52 | content = tmpl.render(lang=locale, url=url, self_localized=self_localized, url_localized=url_localized) |
51 | f.write(tmpl.render(lang=locale, url=url, self_localized=self_localized, url_localized=url_localized)) | 53 | out_name = "./" + locale + "/" + in_file.rstrip(".j2") |
52 | f.close() | 54 | with codecs.open(out_name, "w", "utf-8") as f: |
55 | f.write(content) | ||
diff --git a/template.sh b/template.sh deleted file mode 100755 index 040f3a00..00000000 --- a/template.sh +++ /dev/null | |||
@@ -1,17 +0,0 @@ | |||
1 | #!/bin/sh | ||
2 | # This file is in the public domain. | ||
3 | # | ||
4 | # Wrapper around 'template.py', running it on all | ||
5 | # of our jinja2 input files for all languages for which | ||
6 | # we have translations. | ||
7 | # | ||
8 | # Note that the gettext files need to be prepared first. This script | ||
9 | # is thus to be invoked via the Makefile. | ||
10 | for f in $(git ls-files *.j2); do | ||
11 | for ld in locale/*/; do | ||
12 | l=$(basename $ld) | ||
13 | mkdir -p $(basename $l) | ||
14 | echo "$f: $l" | ||
15 | python template.py $f $l | ||
16 | done | ||
17 | done | ||