diff options
Diffstat (limited to 'template.py')
-rwxr-xr-x | template.py | 60 |
1 files changed, 56 insertions, 4 deletions
diff --git a/template.py b/template.py index a38a97a8..94dac8cf 100755 --- a/template.py +++ b/template.py | |||
@@ -30,7 +30,10 @@ import codecs | |||
30 | import jinja2 | 30 | import jinja2 |
31 | import i18nfix | 31 | import i18nfix |
32 | 32 | ||
33 | env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), | 33 | # TODO: Turn repetition into a class. |
34 | |||
35 | env = jinja2.Environment(loader=jinja2.FileSystemLoader( | ||
36 | os.path.dirname(__file__)), | ||
34 | extensions=["jinja2.ext.i18n"], | 37 | extensions=["jinja2.ext.i18n"], |
35 | lstrip_blocks=True, | 38 | lstrip_blocks=True, |
36 | trim_blocks=True, | 39 | trim_blocks=True, |
@@ -40,7 +43,13 @@ env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__ | |||
40 | if (os.getenv("DEBUG")): | 43 | if (os.getenv("DEBUG")): |
41 | print(sys.path) | 44 | print(sys.path) |
42 | 45 | ||
43 | langs_full = {"en": "English", "fr": "Français", "it": "Italiano", "es": "Español", "de": "Deutsch"} | 46 | langs_full = { |
47 | "en": "English", | ||
48 | "fr": "Français", | ||
49 | "it": "Italiano", | ||
50 | "es": "Español", | ||
51 | "de": "Deutsch" | ||
52 | } | ||
44 | 53 | ||
45 | for in_file in glob.glob("template/*.j2"): | 54 | for in_file in glob.glob("template/*.j2"): |
46 | name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups() | 55 | name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups() |
@@ -50,7 +59,8 @@ for in_file in glob.glob("template/*.j2"): | |||
50 | """ | 59 | """ |
51 | Return URL for the current page in another locale. | 60 | Return URL for the current page in another locale. |
52 | """ | 61 | """ |
53 | return "../" + other_locale + "/" + in_file.replace('template/', '').rstrip(".j2") | 62 | return "../" + other_locale + "/" + in_file.replace('template/', |
63 | '').rstrip(".j2") | ||
54 | 64 | ||
55 | def url_localized(filename): | 65 | def url_localized(filename): |
56 | return "../" + locale + "/" + filename | 66 | return "../" + locale + "/" + filename |
@@ -85,7 +95,49 @@ for in_file in glob.glob("template/*.j2"): | |||
85 | url_localized=url_localized, | 95 | url_localized=url_localized, |
86 | svg_localized=svg_localized, | 96 | svg_localized=svg_localized, |
87 | filename=name + "." + ext) | 97 | filename=name + "." + ext) |
88 | out_name = "./rendered/" + locale + "/" + in_file.replace('template/', '').rstrip(".j2") | 98 | out_name = "./rendered/" + locale + "/" + in_file.replace( |
99 | 'template/', '').rstrip(".j2") | ||
89 | os.makedirs("./rendered/" + locale, exist_ok=True) | 100 | os.makedirs("./rendered/" + locale, exist_ok=True) |
90 | with codecs.open(out_name, "w", encoding='utf-8') as f: | 101 | with codecs.open(out_name, "w", encoding='utf-8') as f: |
91 | f.write(content) | 102 | f.write(content) |
103 | |||
104 | |||
105 | for in_file in glob.glob("news/*.j2"): | ||
106 | name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups() | ||
107 | tmpl = env.get_template(in_file) | ||
108 | |||
109 | def self_localized(other_locale): | ||
110 | """ | ||
111 | Return URL for the current page in another locale. | ||
112 | """ | ||
113 | return "../" + other_locale + "/" + in_file.replace('news/', | ||
114 | '').rstrip(".j2") | ||
115 | |||
116 | def url_localized(filename): | ||
117 | return "../" + locale + "/" + filename | ||
118 | |||
119 | def svg_localized(filename): | ||
120 | lf = filename + "." + locale + ".svg" | ||
121 | if locale == "en" or not os.path.isfile(lf): | ||
122 | return "../" + filename + ".svg" | ||
123 | else: | ||
124 | return "../" + lf | ||
125 | |||
126 | def url(x): | ||
127 | # TODO: look at the app root environment variable | ||
128 | # TODO: check if file exists | ||
129 | return "../" + x | ||
130 | |||
131 | for f in glob.glob("locale/*/"): | ||
132 | locale = os.path.basename(f[:-1]) | ||
133 | content = tmpl.render(lang=locale, | ||
134 | lang_full=langs_full[locale], | ||
135 | url=url, | ||
136 | self_localized=self_localized, | ||
137 | url_localized=url_localized, | ||
138 | svg_localized=svg_localized, | ||
139 | filename=name + "." + ext) | ||
140 | out_name = "./rendered/" + locale + "/news/" + in_file.replace('news/', '').rstrip(".j2") | ||
141 | os.makedirs("./rendered/" + locale + "/news/", exist_ok=True) | ||
142 | with codecs.open(out_name, "w", encoding='utf-8') as f: | ||
143 | f.write(content) | ||