aboutsummaryrefslogtreecommitdiff
path: root/inc
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2019-11-12 21:30:39 +0000
committerng0 <ng0@n0.is>2019-11-12 21:30:39 +0000
commitab19fc81090ebbbecf471b010bc41f5a43ec858f (patch)
tree32e94439fbec80a06326be18ed40b9256851014b /inc
parent1fa59360a22db580a8b2b6a201d11eb545f7aa81 (diff)
downloadwww-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.py13
-rw-r--r--inc/i18nfix.py2
-rw-r--r--inc/site.py50
-rw-r--r--inc/sitemap.py1
-rw-r--r--inc/sum.py2
-rw-r--r--inc/textproc.py8
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 @@
1from pathlib import Path 1from pathlib import Path
2import os
3import shutil
4
5def 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
3def copy_files(kind, conf, locale, inlist, ptarget): 16def 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"""
11Extract translations from a Jinja2 template, stripping leading newlines. 10Extract 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
6import glob 6import glob
7import codecs 7import codecs
8import jinja2 8import jinja2
9import hashlib 9from pathlib import Path, PurePosixPath, PurePath
10from pathlib import Path, PurePosixPath
11from ruamel.yaml import YAML 10from ruamel.yaml import YAML
12import inc.i18nfix 11import inc.i18nfix as i18nfix
13from inc.textproc import cut_news_text 12from inc.textproc import cut_news_text
14from inc.fileproc import copy_files 13from inc.fileproc import copy_files, copy_tree
15 14
16 15
17class gen_site: 16class 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 @@
1import os 1import os
2from pathlib import Path, PurePosixPath 2from pathlib import Path, PurePosixPath
3 3
4
4def sitemap_tree(path): 5def sitemap_tree(path):
5 tree = dict(name=PurePosixPath(path).name, children=[]) 6 tree = dict(name=PurePosixPath(path).name, children=[])
6 try: 7 try:
diff --git a/inc/sum.py b/inc/sum.py
index 9addf78f..fff7a814 100644
--- a/inc/sum.py
+++ b/inc/sum.py
@@ -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 @@
1import html.parser 1import html.parser
2from bs4 import BeautifulSoup 2from bs4 import BeautifulSoup
3 3
4
4class extractText(html.parser.HTMLParser): 5class 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
34def cut_news_text(filename, count): 38def cut_news_text(filename, count):