diff options
author | ng0 <ng0@n0.is> | 2019-11-12 21:30:39 +0000 |
---|---|---|
committer | ng0 <ng0@n0.is> | 2019-11-12 21:30:39 +0000 |
commit | ab19fc81090ebbbecf471b010bc41f5a43ec858f (patch) | |
tree | 32e94439fbec80a06326be18ed40b9256851014b /inc | |
parent | 1fa59360a22db580a8b2b6a201d11eb545f7aa81 (diff) | |
download | www-ab19fc81090ebbbecf471b010bc41f5a43ec858f.tar.gz www-ab19fc81090ebbbecf471b010bc41f5a43ec858f.zip |
inc: more logic to remove code from Makefile. news/index.hmtl.j2: syntax.
Diffstat (limited to 'inc')
-rw-r--r-- | inc/fileproc.py | 13 | ||||
-rw-r--r-- | inc/i18nfix.py | 2 | ||||
-rw-r--r-- | inc/site.py | 50 | ||||
-rw-r--r-- | inc/sitemap.py | 1 | ||||
-rw-r--r-- | inc/sum.py | 2 | ||||
-rw-r--r-- | inc/textproc.py | 8 |
6 files changed, 59 insertions, 17 deletions
diff --git a/inc/fileproc.py b/inc/fileproc.py index 435078bc..bf6cc8df 100644 --- a/inc/fileproc.py +++ b/inc/fileproc.py | |||
@@ -1,4 +1,17 @@ | |||
1 | from pathlib import Path | 1 | from pathlib import Path |
2 | import os | ||
3 | import shutil | ||
4 | |||
5 | def copy_tree(source, destination): | ||
6 | destination.mkdir(parents=True, exist_ok=True) | ||
7 | for _ in os.listdir(source): | ||
8 | i = source / _ | ||
9 | o = destination / _ | ||
10 | if i.is_dir(): | ||
11 | copy_tree(i, o) | ||
12 | else: | ||
13 | shutil.copy2(str(i), str(o)) | ||
14 | |||
2 | 15 | ||
3 | def copy_files(kind, conf, locale, inlist, ptarget): | 16 | def copy_files(kind, conf, locale, inlist, ptarget): |
4 | o = Path(ptarget) | 17 | o = Path(ptarget) |
diff --git a/inc/i18nfix.py b/inc/i18nfix.py index 7f326ba6..69fe177d 100644 --- a/inc/i18nfix.py +++ b/inc/i18nfix.py | |||
@@ -6,7 +6,6 @@ | |||
6 | # are permitted in any medium without royalty provided the copyright | 6 | # are permitted in any medium without royalty provided the copyright |
7 | # notice and this notice are preserved. This file is offered as-is, | 7 | # notice and this notice are preserved. This file is offered as-is, |
8 | # without any warranty. | 8 | # without any warranty. |
9 | |||
10 | """ | 9 | """ |
11 | Extract translations from a Jinja2 template, stripping leading newlines. | 10 | Extract translations from a Jinja2 template, stripping leading newlines. |
12 | 11 | ||
@@ -38,4 +37,5 @@ def wrap_gettext(f): | |||
38 | def wrapper(message): | 37 | def wrapper(message): |
39 | message = normalize(message) | 38 | message = normalize(message) |
40 | return f(message) | 39 | return f(message) |
40 | |||
41 | return wrapper | 41 | return wrapper |
diff --git a/inc/site.py b/inc/site.py index 67e5afa8..9fd9ac87 100644 --- a/inc/site.py +++ b/inc/site.py | |||
@@ -6,29 +6,51 @@ import gettext | |||
6 | import glob | 6 | import glob |
7 | import codecs | 7 | import codecs |
8 | import jinja2 | 8 | import jinja2 |
9 | import hashlib | 9 | from pathlib import Path, PurePosixPath, PurePath |
10 | from pathlib import Path, PurePosixPath | ||
11 | from ruamel.yaml import YAML | 10 | from ruamel.yaml import YAML |
12 | import inc.i18nfix | 11 | import inc.i18nfix as i18nfix |
13 | from inc.textproc import cut_news_text | 12 | from inc.textproc import cut_news_text |
14 | from inc.fileproc import copy_files | 13 | from inc.fileproc import copy_files, copy_tree |
15 | 14 | ||
16 | 15 | ||
17 | class gen_site: | 16 | class gen_site: |
17 | def __init__(self, debug): | ||
18 | self.debug = debug | ||
19 | |||
18 | def load_config(self, name="www.yml"): | 20 | def load_config(self, name="www.yml"): |
19 | yaml = YAML(typ='safe') | 21 | yaml = YAML(typ='safe') |
20 | site_configfile = Path(name) | 22 | site_configfile = Path(name) |
21 | return yaml.load(site_configfile) | 23 | return yaml.load(site_configfile) |
22 | 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 | |||
23 | def gen_abstract(self, conf, name, member, pages, length): | 31 | def gen_abstract(self, conf, name, member, pages, length): |
32 | if self.debug: | ||
33 | print("generating abstracts...") | ||
24 | for item in conf[name]: | 34 | for item in conf[name]: |
25 | item[member] = cut_news_text(item[pages], length) | 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") | ||
26 | 42 | ||
27 | def run(self, root, conf, env): | 43 | def run(self, root, conf, env): |
28 | # os.chdir("..") | 44 | # root = "../" + root |
29 | print(os.getcwd()) | 45 | if self.debug: |
30 | root = "../" + root | 46 | _ = Path(".") |
31 | for in_file in glob.glob(root + "/*.j2"): | 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) | ||
32 | name, ext = re.match(r"(.*)\.([^.]+)$", | 54 | name, ext = re.match(r"(.*)\.([^.]+)$", |
33 | in_file.rstrip(".j2")).groups() | 55 | in_file.rstrip(".j2")).groups() |
34 | tmpl = env.get_template(in_file) | 56 | tmpl = env.get_template(in_file) |
@@ -74,8 +96,14 @@ class gen_site: | |||
74 | # return "../" + x | 96 | # return "../" + x |
75 | return "../" + x | 97 | return "../" + x |
76 | 98 | ||
77 | for l in glob.glob("locale/*/"): | 99 | # for l in glob.glob("locale/*/"): |
78 | locale = os.path.basename(l[:-1]) | 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 | ||
79 | 107 | ||
80 | tr = gettext.translation("messages", | 108 | tr = gettext.translation("messages", |
81 | localedir="locale", | 109 | localedir="locale", |
@@ -106,8 +134,6 @@ class gen_site: | |||
106 | root + '/', '').rstrip(".j2") | 134 | root + '/', '').rstrip(".j2") |
107 | 135 | ||
108 | outdir = Path("rendered") | 136 | outdir = Path("rendered") |
109 | if outdir.exists() is False: | ||
110 | sys.exit(1) | ||
111 | 137 | ||
112 | if root == "news": | 138 | if root == "news": |
113 | langdir = outdir / locale / root | 139 | langdir = outdir / locale / root |
diff --git a/inc/sitemap.py b/inc/sitemap.py index e050c77d..5ccf7447 100644 --- a/inc/sitemap.py +++ b/inc/sitemap.py | |||
@@ -1,6 +1,7 @@ | |||
1 | import os | 1 | import os |
2 | from pathlib import Path, PurePosixPath | 2 | from pathlib import Path, PurePosixPath |
3 | 3 | ||
4 | |||
4 | def sitemap_tree(path): | 5 | def sitemap_tree(path): |
5 | tree = dict(name=PurePosixPath(path).name, children=[]) | 6 | tree = dict(name=PurePosixPath(path).name, children=[]) |
6 | try: | 7 | try: |
@@ -31,5 +31,3 @@ def walksum(_): | |||
31 | traceback.print_exc() | 31 | traceback.print_exc() |
32 | return -2 | 32 | return -2 |
33 | return sha256.hexdigest() | 33 | return sha256.hexdigest() |
34 | |||
35 | |||
diff --git a/inc/textproc.py b/inc/textproc.py index 228518b2..f3b97d3d 100644 --- a/inc/textproc.py +++ b/inc/textproc.py | |||
@@ -1,12 +1,15 @@ | |||
1 | import html.parser | 1 | import html.parser |
2 | from bs4 import BeautifulSoup | 2 | from bs4 import BeautifulSoup |
3 | 3 | ||
4 | |||
4 | class extractText(html.parser.HTMLParser): | 5 | class extractText(html.parser.HTMLParser): |
5 | def __init__(self): | 6 | def __init__(self): |
6 | super(extractText, self).__init__() | 7 | super(extractText, self).__init__() |
7 | self.result = [] | 8 | self.result = [] |
9 | |||
8 | def handle_data(self, data): | 10 | def handle_data(self, data): |
9 | self.result.append(data) | 11 | self.result.append(data) |
12 | |||
10 | def text_in(self): | 13 | def text_in(self): |
11 | return ''.join(self.result) | 14 | return ''.join(self.result) |
12 | 15 | ||
@@ -27,8 +30,9 @@ def cut_text(filename, count): | |||
27 | k.append(i) | 30 | k.append(i) |
28 | b = ''.join(str(e) for e in k) | 31 | b = ''.join(str(e) for e in k) |
29 | text = html2text(b.replace("\n", "")) | 32 | text = html2text(b.replace("\n", "")) |
30 | textreduced = (text[:count] + '...') if len(text) > count else (text + '..') | 33 | textreduced = (text[:count] + '...') if len(text) > count else (text + |
31 | return(textreduced) | 34 | '..') |
35 | return (textreduced) | ||
32 | 36 | ||
33 | 37 | ||
34 | def cut_news_text(filename, count): | 38 | def cut_news_text(filename, count): |