diff options
author | Florian Dold <florian@dold.me> | 2021-05-06 19:05:18 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-05-06 19:05:18 +0200 |
commit | 4e83d962573f95f1bc82440c983638aeeda69e98 (patch) | |
tree | 21516e4b45ddc5b0e080abc318bc134b6397a1a1 | |
parent | 1cffe9eadcf164f66e633071ea48300371156356 (diff) | |
download | www_shared-4e83d962573f95f1bc82440c983638aeeda69e98.tar.gz www_shared-4e83d962573f95f1bc82440c983638aeeda69e98.zip |
cleanup
-rw-r--r-- | fileproc.py | 93 | ||||
-rw-r--r-- | make_sitemap.py | 51 | ||||
-rw-r--r-- | site.py | 42 |
3 files changed, 41 insertions, 145 deletions
diff --git a/fileproc.py b/fileproc.py deleted file mode 100644 index 3a1d1ba..0000000 --- a/fileproc.py +++ /dev/null | |||
@@ -1,93 +0,0 @@ | |||
1 | # Copyright (C) 2019 GNUnet e.V. | ||
2 | # | ||
3 | # This code is derived from code contributed to GNUnet e.V. | ||
4 | # by nikita <nikita@n0.is> | ||
5 | # | ||
6 | # Permission to use, copy, modify, and/or distribute this software for | ||
7 | # any purpose with or without fee is hereby granted. | ||
8 | # | ||
9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL | ||
10 | # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED | ||
11 | # WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE | ||
12 | # AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
13 | # DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA | ||
14 | # OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | ||
15 | # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
16 | # PERFORMANCE OF THIS SOFTWARE. | ||
17 | # | ||
18 | # SPDX-License-Identifier: 0BSD | ||
19 | |||
20 | from pathlib import Path | ||
21 | import os | ||
22 | import shutil | ||
23 | |||
24 | def copy_tree(source, destination): | ||
25 | destination.mkdir(parents=True, exist_ok=True) | ||
26 | for _ in os.listdir(source): | ||
27 | i = source / _ | ||
28 | o = destination / _ | ||
29 | if i.is_dir(): | ||
30 | copy_tree(i, o) | ||
31 | else: | ||
32 | shutil.copy2(str(i), str(o)) | ||
33 | |||
34 | |||
35 | def copy_files(kind, conf, locale, inlist, ptarget): | ||
36 | o = Path(ptarget) | ||
37 | for item in conf[inlist]: | ||
38 | i = Path(kind + "/" + item["file"]) | ||
39 | # print(i) | ||
40 | for t in item["targets"]: | ||
41 | d_loc = o / locale / t | ||
42 | d = o / t | ||
43 | # print(d) | ||
44 | if i.is_file() is not False: | ||
45 | d_loc.write_text(i.read_text()) | ||
46 | print("copied " + str(i) + " to " + str(d_loc) + "...") | ||
47 | d.write_text(i.read_text()) | ||
48 | print("copied " + str(i) + " to " + str(d) + "...") | ||
49 | |||
50 | |||
51 | def rm_rf(directory): | ||
52 | directory = Path(directory) | ||
53 | for child in directory.glob('*'): | ||
54 | if child.is_file(): | ||
55 | child.unlink() | ||
56 | else: | ||
57 | rm_rf(child) | ||
58 | # directory.rmdir() | ||
59 | |||
60 | |||
61 | def fileop(infile, outfile, action): | ||
62 | """ | ||
63 | infile: inputfile, Path object | ||
64 | outfile: outputfile, Path object | ||
65 | action: action if any, String | ||
66 | """ | ||
67 | i = Path(infile) | ||
68 | o = Path(outfile) | ||
69 | outdir = Path("rendered") | ||
70 | if i.is_file() is not False: | ||
71 | if action == "copy": | ||
72 | # Write content of i to o. | ||
73 | o.write_text(i.read_text()) | ||
74 | if action == "link": | ||
75 | o.symlink_to(i) | ||
76 | |||
77 | |||
78 | def write_name(filename, infile, locale, replacer): | ||
79 | return "./rendered/" + locale + "/" + infile.replace(replacer, | ||
80 | '').rstrip(".j2") | ||
81 | |||
82 | |||
83 | def localized(filename, locale, *args): | ||
84 | if len(args) == 0: | ||
85 | return "../" + locale + "/" + filename | ||
86 | ext = kwargs.get('ext', None) | ||
87 | if ext is not None: | ||
88 | lf = filename + "." + locale + "." + ext | ||
89 | lp = Path(lf) | ||
90 | if locale == "en" or not lp.is_file(): | ||
91 | return "../" + filename + "." + ext | ||
92 | else: | ||
93 | return "../" + lf | ||
diff --git a/make_sitemap.py b/make_sitemap.py deleted file mode 100644 index 3bccc32..0000000 --- a/make_sitemap.py +++ /dev/null | |||
@@ -1,51 +0,0 @@ | |||
1 | # Copyright (C) 2019 GNUnet e.V. | ||
2 | # | ||
3 | # This code is derived from code contributed to GNUnet e.V. | ||
4 | # by nikita <nikita@n0.is> | ||
5 | # | ||
6 | # Permission to use, copy, modify, and/or distribute this software for any | ||
7 | # purpose with or without fee is hereby granted. | ||
8 | # | ||
9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE | ||
12 | # LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES | ||
13 | # OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | ||
14 | # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, | ||
15 | # ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF | ||
16 | # THIS SOFTWARE. | ||
17 | # | ||
18 | # SPDX-License-Identifier: 0BSD | ||
19 | # | ||
20 | # A sitemap generator. | ||
21 | |||
22 | import sys | ||
23 | from pathlib import Path | ||
24 | from datetime import datetime | ||
25 | |||
26 | def main(): | ||
27 | if len(sys.argv) >= 2 and sys.argv[1] == "-i": | ||
28 | i = sys.argv[2] | ||
29 | else: | ||
30 | i = "rendered" | ||
31 | |||
32 | p = Path(i) | ||
33 | links = sorted(p.rglob("*.html")) | ||
34 | t0 = datetime.now() | ||
35 | timestamp = t0.strftime("%Y-%m-%d") | ||
36 | |||
37 | o = Path("sitemap.xml") | ||
38 | with o.open("w") as f: | ||
39 | f.write('<?xml version="1.0" encoding="UTF-8"?>\n') | ||
40 | f.write('<urlset\n') | ||
41 | f.write('xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n') | ||
42 | f.write('xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 ') | ||
43 | f.write('http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"\n') | ||
44 | f.write('xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n') | ||
45 | for link in links: | ||
46 | f.write('<url><loc>' + str(link).lstrip("rendered") + '</loc><lastmod>' + timestamp + '</lastmod><priority>1.0</priority></url>\n') | ||
47 | f.write('</urlset>\n') | ||
48 | |||
49 | |||
50 | if __name__ == "__main__": | ||
51 | main() | ||
@@ -17,6 +17,7 @@ | |||
17 | # | 17 | # |
18 | # SPDX-License-Identifier: 0BSD | 18 | # SPDX-License-Identifier: 0BSD |
19 | import os | 19 | import os |
20 | import shutil | ||
20 | import os.path | 21 | import os.path |
21 | import sys | 22 | import sys |
22 | import re | 23 | import re |
@@ -26,6 +27,7 @@ import codecs | |||
26 | import jinja2 | 27 | import jinja2 |
27 | from pathlib import Path, PurePosixPath, PurePath | 28 | from pathlib import Path, PurePosixPath, PurePath |
28 | from ruamel.yaml import YAML | 29 | from ruamel.yaml import YAML |
30 | from datetime import datetime | ||
29 | 31 | ||
30 | # Make sure the current directory is in the search path when trying | 32 | # Make sure the current directory is in the search path when trying |
31 | # to import i18nfix. | 33 | # to import i18nfix. |
@@ -33,7 +35,6 @@ sys.path.insert(0, ".") | |||
33 | 35 | ||
34 | import inc.i18nfix as i18nfix | 36 | import inc.i18nfix as i18nfix |
35 | from inc.textproc import cut_news_text, cut_article | 37 | from inc.textproc import cut_news_text, cut_article |
36 | from inc.fileproc import copy_files, copy_tree | ||
37 | from inc.time import time_rfc822, time_now, conv_date_rfc822 | 38 | from inc.time import time_rfc822, time_now, conv_date_rfc822 |
38 | 39 | ||
39 | 40 | ||
@@ -95,6 +96,16 @@ def make_helpers(root, in_file, locale): | |||
95 | ) | 96 | ) |
96 | 97 | ||
97 | 98 | ||
99 | def copytree(src, dst, symlinks=False, ignore=None): | ||
100 | for item in os.listdir(src): | ||
101 | s = os.path.join(src, item) | ||
102 | d = os.path.join(dst, item) | ||
103 | if os.path.isdir(s): | ||
104 | shutil.copytree(s, d, symlinks, ignore, dirs_exist_ok=True) | ||
105 | else: | ||
106 | shutil.copy2(s, d) | ||
107 | |||
108 | |||
98 | class SiteGenerator: | 109 | class SiteGenerator: |
99 | def __init__(self, debug=0, root="."): | 110 | def __init__(self, debug=0, root="."): |
100 | self.root = Path(root).resolve() | 111 | self.root = Path(root).resolve() |
@@ -169,9 +180,34 @@ class SiteGenerator: | |||
169 | except: | 180 | except: |
170 | print(e) | 181 | print(e) |
171 | 182 | ||
183 | def emit_sitemap(self): | ||
184 | p = self.root / "rendered" | ||
185 | links = sorted(p.rglob("*.html")) | ||
186 | t0 = datetime.now() | ||
187 | timestamp = t0.strftime("%Y-%m-%d") | ||
188 | |||
189 | o = p / "sitemap.xml" | ||
190 | with o.open("w") as f: | ||
191 | f.write('<?xml version="1.0" encoding="UTF-8"?>\n') | ||
192 | f.write("<urlset\n") | ||
193 | f.write('xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n') | ||
194 | f.write('xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 ') | ||
195 | f.write('http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"\n') | ||
196 | f.write('xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n') | ||
197 | for link in links: | ||
198 | f.write( | ||
199 | "<url><loc>" | ||
200 | + str(link).lstrip("rendered") | ||
201 | + "</loc><lastmod>" | ||
202 | + timestamp | ||
203 | + "</lastmod><priority>1.0</priority></url>\n" | ||
204 | ) | ||
205 | f.write("</urlset>\n") | ||
206 | |||
172 | def run(self): | 207 | def run(self): |
173 | conf = self.config | 208 | conf = self.config |
174 | root = self.root | 209 | root = self.root |
210 | |||
175 | for l in root.glob("locale/*/"): | 211 | for l in root.glob("locale/*/"): |
176 | 212 | ||
177 | if not l.is_dir(): | 213 | if not l.is_dir(): |
@@ -204,3 +240,7 @@ class SiteGenerator: | |||
204 | continue | 240 | continue |
205 | 241 | ||
206 | self.run_localized(locale, tr) | 242 | self.run_localized(locale, tr) |
243 | |||
244 | self.emit_sitemap() | ||
245 | |||
246 | copytree(root / "static", root / "rendered") | ||