diff options
author | Andreas HABEGGER <andreas.habegger@bfh.ch> | 2023-11-20 11:24:48 +0100 |
---|---|---|
committer | Andreas HABEGGER <andreas.habegger@bfh.ch> | 2023-11-20 11:24:48 +0100 |
commit | 72771f14dfe85a6444cb48f6cdfce1abc5473f51 (patch) | |
tree | 63721cec2d14e5f0afeec523b6d27e482ee55593 | |
parent | 218f02cfc0453a0d4cc067ba9256123a675040ce (diff) | |
parent | 1d496cd5b009d8d420176b00831c4bbd717a4a09 (diff) | |
download | www_shared-72771f14dfe85a6444cb48f6cdfce1abc5473f51.tar.gz www_shared-72771f14dfe85a6444cb48f6cdfce1abc5473f51.zip |
MC: YAML multifile reader for translations and configuration properties
-rw-r--r-- | README | 10 | ||||
-rwxr-xr-x | list-languages | 16 | ||||
-rw-r--r-- | sitegen/site.py | 13 |
3 files changed, 34 insertions, 5 deletions
@@ -18,6 +18,16 @@ of one website, ie this affects both websites and if in doubt | |||
18 | should be tested against both as soon as both websites have | 18 | should be tested against both as soon as both websites have |
19 | been switched to this code. | 19 | been switched to this code. |
20 | 20 | ||
21 | Note | ||
22 | ---- | ||
23 | YAML files are used for both translations and the Site Builder as | ||
24 | content properties. The collection hierarchy is as follows | ||
25 | * First, the file "properties.yml" in the top directory is read. | ||
26 | * Second, all YAML files are collected in a directory called "properties.d". | ||
27 | * Third, the collected files are fed into the build system as a dictionary. | ||
28 | Note that the name is controlled by a variable PROPERTIES_YML. The name of | ||
29 | the file is linked to the name of the directory. | ||
30 | |||
21 | Dependencies | 31 | Dependencies |
22 | ------------ | 32 | ------------ |
23 | 33 | ||
diff --git a/list-languages b/list-languages index 9df48ac..c1379d2 100755 --- a/list-languages +++ b/list-languages | |||
@@ -1,8 +1,18 @@ | |||
1 | #!/usr/bin/env python3 | 1 | #!/usr/bin/env python3 |
2 | 2 | ||
3 | from ruamel.yaml import YAML | ||
4 | from pathlib import Path | 3 | from pathlib import Path |
4 | from ruamel.yaml import YAML | ||
5 | |||
6 | PROPERTIES_YML = "properties.yml" | ||
7 | |||
8 | # NOTE : read-in properties.yml and afterwards all *.yml in properties.d | ||
9 | properties = {} | ||
5 | yaml = YAML(typ="safe") | 10 | yaml = YAML(typ="safe") |
6 | config = yaml.load(Path("www.yml")) | 11 | yaml.preserve_quotes = True |
7 | langs = config["langs_full"] | 12 | properties.update(yaml.load(Path(PROPERTIES_YML))) |
13 | properties_dir = PROPERTIES_YML.removesuffix('.yml') + '.d' | ||
14 | for filepath in Path(properties_dir).glob('*.yml'): | ||
15 | properties.update(yaml.load(filepath)) | ||
16 | |||
17 | langs = properties["langs_full"] | ||
8 | print(" ".join(langs)) | 18 | print(" ".join(langs)) |
diff --git a/sitegen/site.py b/sitegen/site.py index e2b20c0..6139d1c 100644 --- a/sitegen/site.py +++ b/sitegen/site.py | |||
@@ -34,6 +34,8 @@ import sitegen.i18nfix as i18nfix | |||
34 | from sitegen.timeutil import time_rfc822, time_now, conv_date_rfc822 | 34 | from sitegen.timeutil import time_rfc822, time_now, conv_date_rfc822 |
35 | 35 | ||
36 | 36 | ||
37 | PROPERTIES_YML = "properties.yml" | ||
38 | |||
37 | def html2text(html_str): | 39 | def html2text(html_str): |
38 | class extractText(html.parser.HTMLParser): | 40 | class extractText(html.parser.HTMLParser): |
39 | def __init__(self): | 41 | def __init__(self): |
@@ -195,9 +197,16 @@ class SiteGenerator: | |||
195 | env.filters["extract_body"] = extract_body | 197 | env.filters["extract_body"] = extract_body |
196 | env.newstyle_gettext = True | 198 | env.newstyle_gettext = True |
197 | self.env = env | 199 | self.env = env |
200 | # NOTE : read-in properties.yml and afterwards all *.yml in properties.d | ||
201 | properties = {} | ||
198 | yaml = YAML(typ="safe") | 202 | yaml = YAML(typ="safe") |
199 | site_configfile = self.root / "www.yml" | 203 | yaml.preserve_quotes = True |
200 | self.config = yaml.load(site_configfile) | 204 | properties.update(yaml.load(self.root/PROPERTIES_YML)) |
205 | properties_dir = PROPERTIES_YML.removesuffix('.yml') + '.d' | ||
206 | for filepath in Path(self.root/properties_dir).glob('*.yml'): | ||
207 | properties.update(yaml.load(filepath)) | ||
208 | self.config = properties | ||
209 | |||
201 | self.baseurl = os.environ.get("BASEURL") | 210 | self.baseurl = os.environ.get("BASEURL") |
202 | if not self.baseurl: | 211 | if not self.baseurl: |
203 | self.baseurl = self.config["siteconf"].get("baseurl") | 212 | self.baseurl = self.config["siteconf"].get("baseurl") |