diff options
Diffstat (limited to 'inc/site.py')
-rw-r--r-- | inc/site.py | 153 |
1 files changed, 0 insertions, 153 deletions
diff --git a/inc/site.py b/inc/site.py deleted file mode 100644 index 9fd9ac87..00000000 --- a/inc/site.py +++ /dev/null | |||
@@ -1,153 +0,0 @@ | |||
1 | import os | ||
2 | import os.path | ||
3 | import sys | ||
4 | import re | ||
5 | import gettext | ||
6 | import glob | ||
7 | import codecs | ||
8 | import jinja2 | ||
9 | from pathlib import Path, PurePosixPath, PurePath | ||
10 | from ruamel.yaml import YAML | ||
11 | import inc.i18nfix as i18nfix | ||
12 | from inc.textproc import cut_news_text | ||
13 | from inc.fileproc import copy_files, copy_tree | ||
14 | |||
15 | |||
16 | class gen_site: | ||
17 | def __init__(self, debug): | ||
18 | self.debug = debug | ||
19 | |||
20 | def load_config(self, name="www.yml"): | ||
21 | yaml = YAML(typ='safe') | ||
22 | site_configfile = Path(name) | ||
23 | return yaml.load(site_configfile) | ||
24 | |||
25 | def copy_trees(self, directory): | ||
26 | """ Take a directory name (string) and pass it to copy_tree() as Path object. """ | ||
27 | i = Path(directory) | ||
28 | o = Path("rendered/" + directory) | ||
29 | copy_tree(i, o) | ||
30 | |||
31 | def gen_abstract(self, conf, name, member, pages, length): | ||
32 | if self.debug: | ||
33 | print("generating abstracts...") | ||
34 | for item in conf[name]: | ||
35 | item[member] = cut_news_text(item[pages], length) | ||
36 | if self.debug: | ||
37 | print("cwd: " + str(Path.cwd())) | ||
38 | if self.debug > 1: | ||
39 | print(conf["newsposts"]) | ||
40 | if self.debug: | ||
41 | print("[done] generating abstracts") | ||
42 | |||
43 | def run(self, root, conf, env): | ||
44 | # root = "../" + root | ||
45 | if self.debug: | ||
46 | _ = Path(".") | ||
47 | q = list(_.glob("**/*.j2")) | ||
48 | print(q) | ||
49 | # for in_file in glob.glob(root + "/*.j2"): | ||
50 | for in_file in Path(".").glob(root + "/*.j2"): | ||
51 | in_file = str(in_file) | ||
52 | if self.debug: | ||
53 | print(in_file) | ||
54 | name, ext = re.match(r"(.*)\.([^.]+)$", | ||
55 | in_file.rstrip(".j2")).groups() | ||
56 | tmpl = env.get_template(in_file) | ||
57 | |||
58 | def self_localized(other_locale): | ||
59 | """ | ||
60 | Return URL for the current page in another locale. | ||
61 | """ | ||
62 | return "../" + other_locale + "/" + in_file.replace( | ||
63 | root + '/', '').rstrip(".j2") | ||
64 | |||
65 | def url_localized(filename): | ||
66 | if root == "news": | ||
67 | return "../../" + locale + "/" + filename | ||
68 | else: | ||
69 | return "../" + locale + "/" + filename | ||
70 | |||
71 | def url_static(filename): | ||
72 | if root == "news": | ||
73 | return "../../static/" + filename | ||
74 | else: | ||
75 | return "../static/" + filename | ||
76 | |||
77 | def url_dist(filename): | ||
78 | if root == "news": | ||
79 | return "../../dist/" + filename | ||
80 | else: | ||
81 | return "../dist/" + filename | ||
82 | |||
83 | def svg_localized(filename): | ||
84 | lf = filename + "." + locale + ".svg" | ||
85 | if locale == "en" or not Path(lf).is_file(): | ||
86 | return "../" + filename + ".svg" | ||
87 | else: | ||
88 | return "../" + lf | ||
89 | |||
90 | def url(x): | ||
91 | # TODO: look at the app root environment variable | ||
92 | # TODO: check if file exists | ||
93 | #if root == "news": | ||
94 | # return "../" + "../" + x | ||
95 | #else: | ||
96 | # return "../" + x | ||
97 | return "../" + x | ||
98 | |||
99 | # for l in glob.glob("locale/*/"): | ||
100 | # https://bugs.python.org/issue22276 | ||
101 | for l in list(x for x in Path(".").glob("locale/*/") if x.is_dir()): | ||
102 | l = str(PurePath(l).name) | ||
103 | if self.debug: | ||
104 | print(l) | ||
105 | # locale = os.path.basename(l[:-1]) | ||
106 | locale = l | ||
107 | |||
108 | tr = gettext.translation("messages", | ||
109 | localedir="locale", | ||
110 | languages=[locale]) | ||
111 | |||
112 | tr.gettext = i18nfix.wrap_gettext(tr.gettext) | ||
113 | |||
114 | env.install_gettext_translations(tr, newstyle=True) | ||
115 | |||
116 | content = tmpl.render(lang=locale, | ||
117 | lang_full=conf["langs_full"][locale], | ||
118 | url=url, | ||
119 | meetingnotesdata=conf["meetingnotes"], | ||
120 | newsdata=conf["newsposts"], | ||
121 | videosdata=conf["videoslist"], | ||
122 | self_localized=self_localized, | ||
123 | url_localized=url_localized, | ||
124 | url_static=url_static, | ||
125 | url_dist=url_dist, | ||
126 | svg_localized=svg_localized, | ||
127 | filename=name + "." + ext) | ||
128 | |||
129 | if root == "news": | ||
130 | out_name = "./rendered/" + locale + "/" + root + "/" + in_file.replace( | ||
131 | root + '/', '').rstrip(".j2") | ||
132 | else: | ||
133 | out_name = "./rendered/" + locale + "/" + in_file.replace( | ||
134 | root + '/', '').rstrip(".j2") | ||
135 | |||
136 | outdir = Path("rendered") | ||
137 | |||
138 | if root == "news": | ||
139 | langdir = outdir / locale / root | ||
140 | else: | ||
141 | langdir = outdir / locale | ||
142 | |||
143 | try: | ||
144 | langdir.mkdir(parents=True, exist_ok=True) | ||
145 | except e as FileNotFoundError: | ||
146 | print(e) | ||
147 | |||
148 | with codecs.open(out_name, "w", encoding='utf-8') as f: | ||
149 | try: | ||
150 | print(Path.cwd()) | ||
151 | f.write(content) | ||
152 | except e as Error: | ||
153 | print(e) | ||