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 | |
parent | 1fa59360a22db580a8b2b6a201d11eb545f7aa81 (diff) | |
download | www-ab19fc81090ebbbecf471b010bc41f5a43ec858f.tar.gz www-ab19fc81090ebbbecf471b010bc41f5a43ec858f.zip |
inc: more logic to remove code from Makefile. news/index.hmtl.j2: syntax.
-rw-r--r-- | GNUmakefile | 2 | ||||
-rw-r--r-- | Makefile | 8 | ||||
-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 | ||||
-rwxr-xr-x | make_site.py | 33 | ||||
-rw-r--r-- | news/index.html.j2 | 2 |
10 files changed, 92 insertions, 29 deletions
diff --git a/GNUmakefile b/GNUmakefile index d29a6d41..4dc2b729 100644 --- a/GNUmakefile +++ b/GNUmakefile | |||
@@ -28,7 +28,7 @@ all: css locale template | |||
28 | (for lang in en de es fr it ; do \ | 28 | (for lang in en de es fr it ; do \ |
29 | $(cp) rendered/static/robots.txt rendered/$$lang/robots.txt ; \ | 29 | $(cp) rendered/static/robots.txt rendered/$$lang/robots.txt ; \ |
30 | done) | 30 | done) |
31 | ($(cp) favicon.ico rendered/favicon.ico) | 31 | #($(cp) favicon.ico rendered/favicon.ico) |
32 | ($(sh) make_sitemap.sh) | 32 | ($(sh) make_sitemap.sh) |
33 | ($(cp) rendered/sitemap.xml rendered/en/sitemap.xml) | 33 | ($(cp) rendered/sitemap.xml rendered/en/sitemap.xml) |
34 | ($(cp) static/moved.html rendered/frontpage.html) | 34 | ($(cp) static/moved.html rendered/frontpage.html) |
@@ -21,9 +21,9 @@ _DIRLIST= dist static | |||
21 | # All: build HTML pages in all languages and compile the | 21 | # All: build HTML pages in all languages and compile the |
22 | # TypeScript logic in web-common. | 22 | # TypeScript logic in web-common. |
23 | all: css locale template | 23 | all: css locale template |
24 | .for _dir in ${_DIRLIST} | 24 | #.for _dir in ${_DIRLIST} |
25 | $(cp) -R ${_dir} rendered/ | 25 | # $(cp) -R ${_dir} rendered/ |
26 | .endfor | 26 | #.endfor |
27 | .for _lang in ${_LOCALELIST} | 27 | .for _lang in ${_LOCALELIST} |
28 | ($(cp) rendered/static/robots.txt rendered/${_lang}) | 28 | ($(cp) rendered/static/robots.txt rendered/${_lang}) |
29 | ($(cp) rendered/static/stage.robots.txt rendered/${_lang}) | 29 | ($(cp) rendered/static/stage.robots.txt rendered/${_lang}) |
@@ -74,7 +74,7 @@ locale: locale-update locale-compile | |||
74 | # Run the jinja2 templating engine to expand templates to HTML | 74 | # Run the jinja2 templating engine to expand templates to HTML |
75 | # incorporating translations. | 75 | # incorporating translations. |
76 | template: locale-compile | 76 | template: locale-compile |
77 | $(python) ./make_site.py | 77 | $(python) ./make_site.py -vvvv |
78 | 78 | ||
79 | css: | 79 | css: |
80 | $(sassc) static/styles.sass static/styles.css | 80 | $(sassc) static/styles.sass static/styles.css |
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): |
diff --git a/make_site.py b/make_site.py index c4a17e21..d335f979 100755 --- a/make_site.py +++ b/make_site.py | |||
@@ -18,29 +18,50 @@ | |||
18 | # is thus to be invoked via the Makefile. | 18 | # is thus to be invoked via the Makefile. |
19 | 19 | ||
20 | import jinja2 | 20 | import jinja2 |
21 | import os | 21 | import sys |
22 | from pathlib import Path, PurePath | ||
22 | from inc.site import gen_site | 23 | from inc.site import gen_site |
23 | from inc.fileproc import copy_files | 24 | from inc.fileproc import copy_files |
24 | 25 | ||
25 | env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__),"inc"), | 26 | env = jinja2.Environment(loader=jinja2.FileSystemLoader(str(PurePath(__file__).parent)), |
26 | extensions=["jinja2.ext.i18n"], | 27 | extensions=["jinja2.ext.i18n"], |
27 | lstrip_blocks=True, | 28 | lstrip_blocks=True, |
28 | trim_blocks=True, | 29 | trim_blocks=True, |
29 | undefined=jinja2.StrictUndefined, | 30 | undefined=jinja2.StrictUndefined, |
30 | autoescape=False) | 31 | autoescape=False) |
31 | 32 | ||
33 | if len(sys.argv) >= 2 and sys.argv[1] == "-vv": | ||
34 | DEBUG=1 | ||
35 | elif len(sys.argv) >= 2 and sys.argv[1] == "-vvv": | ||
36 | DEBUG=2 | ||
37 | elif len(sys.argv) >= 2 and sys.argv[1] == "-vvvv": | ||
38 | DEBUG=3 | ||
39 | else: | ||
40 | DEBUG=0 | ||
41 | |||
32 | def main(): | 42 | def main(): |
33 | # rm_rf("rendered") | 43 | # rm_rf("rendered") |
34 | x = gen_site() | 44 | x = gen_site(DEBUG) |
35 | conf = x.load_config("www.yml") | 45 | conf = x.load_config("www.yml") |
36 | print("generating news abstracts...") | ||
37 | x.gen_abstract(conf, "newsposts", "abstract", "page", 1000) | 46 | x.gen_abstract(conf, "newsposts", "abstract", "page", 1000) |
38 | print("generating html from jinja2 templates...") | 47 | if DEBUG: |
48 | print("generating html from jinja2 templates...") | ||
39 | x.run("template", conf, env) | 49 | x.run("template", conf, env) |
40 | print("generating html from jinja2 news templates...") | 50 | if DEBUG: |
51 | print(Path.cwd()) | ||
52 | _ = Path("rendered") | ||
53 | for child in _.iterdir(): | ||
54 | print(child) | ||
55 | if DEBUG: | ||
56 | print(Path.cwd()) | ||
57 | print("generating html from jinja2 news templates...") | ||
41 | x.run("news", conf, env) | 58 | x.run("news", conf, env) |
42 | #for lang in conf["langs_full"]: | 59 | #for lang in conf["langs_full"]: |
43 | # copy_files("static", conf, lang, "staticfiles", "rendered") | 60 | # copy_files("static", conf, lang, "staticfiles", "rendered") |
61 | if DEBUG: | ||
62 | print("copying directories...") | ||
63 | x.copy_trees("static") | ||
64 | x.copy_trees("dist") | ||
44 | # print("generating rss...") | 65 | # print("generating rss...") |
45 | # x.generate_rss() | 66 | # x.generate_rss() |
46 | # print("generating sitemap...") | 67 | # print("generating sitemap...") |
diff --git a/news/index.html.j2 b/news/index.html.j2 index 8c58d9f1..bdb716d5 100644 --- a/news/index.html.j2 +++ b/news/index.html.j2 | |||
@@ -10,7 +10,7 @@ | |||
10 | <section> | 10 | <section> |
11 | <p> | 11 | <p> |
12 | News posts published by GNUnet about changes related to | 12 | News posts published by GNUnet about changes related to |
13 | GNUnet, releases, and events. | 13 | GNUnet, releases, and events – <a href="{{ url_localized('rss.xml')}}">subscribe to our RSS feed</a> |
14 | </p> | 14 | </p> |
15 | </section> | 15 | </section> |
16 | </div> | 16 | </div> |