diff options
Diffstat (limited to 'demo/template.py')
-rwxr-xr-x | demo/template.py | 107 |
1 files changed, 66 insertions, 41 deletions
diff --git a/demo/template.py b/demo/template.py index 05599e4..01d778b 100755 --- a/demo/template.py +++ b/demo/template.py @@ -17,66 +17,91 @@ import glob import codecs import os import os.path +import click +import shutil +from pathlib import Path -env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), - extensions=["jinja2.ext.i18n"], - lstrip_blocks=True, - trim_blocks=True, - - undefined=jinja2.StrictUndefined, - autoescape=False) +env = jinja2.Environment( + loader=jinja2.FileSystemLoader(os.path.realpath(os.path.dirname(__file__))), + extensions=["jinja2.ext.i18n"], + lstrip_blocks=True, + trim_blocks=True, + undefined=jinja2.StrictUndefined, + autoescape=False, +) default_ctx = {} default_ctx["merchant_blog_url"] = os.environ.get("TALER_ENV_URL_MERCHANT_BLOG", "#") -default_ctx["merchant_donations_url"] = os.environ.get("TALER_ENV_URL_MERCHANT_DONATIONS", "#") -default_ctx["merchant_survey_url"] = os.environ.get("TALER_ENV_URL_MERCHANT_SURVEY", "#") +default_ctx["merchant_donations_url"] = os.environ.get( + "TALER_ENV_URL_MERCHANT_DONATIONS", "#" +) +default_ctx["merchant_survey_url"] = os.environ.get( + "TALER_ENV_URL_MERCHANT_SURVEY", "#" +) default_ctx["intro_url"] = os.environ.get("TALER_ENV_URL_INTRO", "#") default_ctx["bank_url"] = os.environ.get("TALER_ENV_URL_BANK", "#") default_ctx["auditor_url"] = os.environ.get("TALER_ENV_URL_AUDITOR", "#") default_ctx["backoffice_url"] = os.environ.get("TALER_ENV_URL_BACKOFFICE", "#") -for in_file in glob.glob("*.j2"): - name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups() - tmpl = env.get_template(in_file) +@click.command() +@click.argument("outdir") +def generate_site(outdir): + """Generate the site from templates.""" + out_path = Path.cwd().joinpath(outdir) + os.chdir(os.path.dirname(os.path.realpath(__file__))) + os.makedirs(out_path.as_posix(), exist_ok=True) + for in_file in glob.glob("*.j2"): + name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups() + tmpl = env.get_template(in_file) + + def self_localized(other_locale): + """ + Return URL for the current page in another locale. + """ + return "../" + other_locale + "/" + in_file.rstrip(".j2") - def self_localized(other_locale): - """ - Return URL for the current page in another locale. - """ - return "../" + other_locale + "/" + in_file.rstrip(".j2") + def url_localized(filename): + return "../" + locale + "/" + filename - def url_localized(filename): - return "../" + locale + "/" + filename + def url(x): + # TODO: look at the app root environment variable + # TODO: check if file exists + return "../" + x - def url(x): - # TODO: look at the app root environment variable - # TODO: check if file exists - return "../" + x + for l in ("en", "de", "it", "es"): + locale = os.path.basename(l) - for l in ("en", "de", "it", "es"): - locale = os.path.basename(l) + if os.path.isdir(os.path.join("./locale/", locale)): + tr = gettext.translation( + "messages", localedir="locale", languages=[locale] + ) - if os.path.isdir(os.path.join("./locale/", locale)): - tr = gettext.translation("messages", - localedir="locale", - languages=[locale]) + env.install_gettext_translations(tr, newstyle=True) + else: + print("warning: locale {} not found".format(locale)) - env.install_gettext_translations(tr, newstyle=True) - else: - print("warning: locale {} not found".format(locale)) - - ctx = dict( + ctx = dict( lang=locale, url=url, self_localized=self_localized, url_localized=url_localized, - filename=name + "." + ext) - ctx.update(default_ctx) + filename=name + "." + ext, + ) + ctx.update(default_ctx) + + content = tmpl.render(**ctx) + out_name = (out_path / locale / in_file.rstrip(".j2")).as_posix() + os.makedirs((out_path / locale).as_posix(), exist_ok=True) + + with codecs.open(out_name, "w", "utf-8") as f: + f.write(content) + + for p in Path.cwd().glob("static/**/*.css"): + target = out_path / p.relative_to(Path.cwd()) + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copy(p, target) - content = tmpl.render(**ctx) - out_name = "./" + locale + "/" + in_file.rstrip(".j2") - os.makedirs("./" + locale, exist_ok=True) - with codecs.open(out_name, "w", "utf-8") as f: - f.write(content) +if __name__ == "__main__": + generate_site() |