diff options
author | Florian Dold <florian@dold.me> | 2021-05-04 21:53:30 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-05-04 21:53:30 +0200 |
commit | 9f19bf98677b7e99fccad018c7d76710e6417e63 (patch) | |
tree | 156762f02f04597e38836cdda6df1a6c883dfe6b | |
parent | 99a0de7cac0033db10a0bed9f476900ece1e402a (diff) | |
download | www_shared-9f19bf98677b7e99fccad018c7d76710e6417e63.tar.gz www_shared-9f19bf98677b7e99fccad018c7d76710e6417e63.zip |
make localized self URLs absolute
-rw-r--r-- | site.py | 30 |
1 files changed, 20 insertions, 10 deletions
@@ -36,7 +36,7 @@ from inc.textproc import cut_news_text, cut_article | |||
36 | from inc.fileproc import copy_files, copy_tree | 36 | from inc.fileproc import copy_files, copy_tree |
37 | from inc.make_rss import * | 37 | from inc.make_rss import * |
38 | 38 | ||
39 | class gen_site: | 39 | class SiteGenerator: |
40 | def __init__(self, debug): | 40 | def __init__(self, debug): |
41 | self.debug = debug | 41 | self.debug = debug |
42 | 42 | ||
@@ -84,7 +84,8 @@ class gen_site: | |||
84 | _ = Path(".") | 84 | _ = Path(".") |
85 | q = list(_.glob("**/*.j2")) | 85 | q = list(_.glob("**/*.j2")) |
86 | print(q) | 86 | print(q) |
87 | # for in_file in glob.glob(root + "/*.j2"): | 87 | abs_cwd = Path(".").resolve() |
88 | |||
88 | for in_file in Path(".").glob(root + "/*.j2"): | 89 | for in_file in Path(".").glob(root + "/*.j2"): |
89 | in_file = str(in_file) | 90 | in_file = str(in_file) |
90 | if self.debug > 1: | 91 | if self.debug > 1: |
@@ -93,16 +94,22 @@ class gen_site: | |||
93 | in_file.rstrip(".j2")).groups() | 94 | in_file.rstrip(".j2")).groups() |
94 | tmpl = env.get_template(in_file) | 95 | tmpl = env.get_template(in_file) |
95 | 96 | ||
96 | def self_localized(other_locale): | 97 | def self_localized(other_locale, relative=False): |
97 | """ | 98 | """ |
98 | Return URL for the current page in another locale. | 99 | Return URL for the current page in another locale. |
99 | """ | 100 | """ |
100 | if root == "news": | 101 | abs_file = Path(in_file).resolve() |
101 | return "../../" + other_locale + "/news/" + in_file.replace( | 102 | baseurl = os.environ.get("BASEURL") |
102 | root + '/', '').rstrip(".j2") | 103 | if relative or not baseurl: |
104 | if root == "news": | ||
105 | return "../../" + other_locale + "/news/" + in_file.replace( | ||
106 | root + '/', '').rstrip(".j2") | ||
107 | else: | ||
108 | return "../" + other_locale + "/" + in_file.replace( | ||
109 | root + '/', '').rstrip(".j2") | ||
103 | else: | 110 | else: |
104 | return "../" + other_locale + "/" + in_file.replace( | 111 | return baseurl + other_locale + "/" + str(Path(abs_file).relative_to(abs_cwd)).rstrip(".j2") |
105 | root + '/', '').rstrip(".j2") | 112 | |
106 | 113 | ||
107 | def url_localized(filename): | 114 | def url_localized(filename): |
108 | if root == "news": | 115 | if root == "news": |
@@ -147,7 +154,6 @@ class gen_site: | |||
147 | # locale = os.path.basename(l[:-1]) | 154 | # locale = os.path.basename(l[:-1]) |
148 | locale = l | 155 | locale = l |
149 | 156 | ||
150 | |||
151 | try: | 157 | try: |
152 | tr = gettext.translation("messages", | 158 | tr = gettext.translation("messages", |
153 | localedir="locale", | 159 | localedir="locale", |
@@ -203,5 +209,9 @@ class gen_site: | |||
203 | if self.debug > 1: | 209 | if self.debug > 1: |
204 | print(Path.cwd()) | 210 | print(Path.cwd()) |
205 | f.write(content) | 211 | f.write(content) |
206 | except e as Error: | 212 | except: |
207 | print(e) | 213 | print(e) |
214 | |||
215 | # Deprecated alias. Should be removed once all sites have | ||
216 | # been updated. | ||
217 | gen_site = SiteGenerator | ||