aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-02-14 21:07:41 +0100
committerFlorian Dold <florian.dold@gmail.com>2017-02-14 21:09:33 +0100
commit197f0a626dc333a4956a541ce556abb87bda8b46 (patch)
tree8f0750968d865f4a52894a3aff05babf905ca3dc
parent456a61874e68847d1a7099831fa6247e6cbdc276 (diff)
downloadwww-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--Makefile2
-rwxr-xr-xtemplate.py59
-rwxr-xr-xtemplate.sh17
3 files changed, 32 insertions, 46 deletions
diff --git a/Makefile b/Makefile
index 24f3de50..2f39d063 100644
--- a/Makefile
+++ b/Makefile
@@ -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.
35template: locale-compile 35template: 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.
10import os 10import os
11import os.path
11import sys 12import sys
12import re 13import re
13import gettext 14import gettext
14import jinja2 15import jinja2
16import glob
17import codecs
15 18
16if len(sys.argv) < 3: 19env = 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
19in_file = sys.argv[1]
20locale = sys.argv[2]
21 23
22name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups() 24for 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
24tr = 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
28env = 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)
31env.install_gettext_translations(tr, newstyle=True)
32 36
33tmpl = 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
35def 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
41def url_localized(filename): 45 tr = gettext.translation("messages",
42 return "../" + locale + "/" + filename 46 localedir="locale",
47 languages=[locale])
43 48
44def 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
49import codecs 51
50f = 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)
51f.write(tmpl.render(lang=locale, url=url, self_localized=self_localized, url_localized=url_localized)) 53 out_name = "./" + locale + "/" + in_file.rstrip(".j2")
52f.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.
10for 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
17done