aboutsummaryrefslogtreecommitdiff
path: root/template.py
blob: d2dc5a9a5a4ba4eb207181817396daf04befd5f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
# coding: utf-8
#
# Copyright (C) 2017, 2018, 2019 GNUnet e.V.
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.
#
# ----
#
# This script runs the jinja2 templating engine on an input template-file
# using the specified locale for gettext translations, and outputs
# the resulting (HTML) ouptut-file.
#
# Note that the gettext files need to be prepared first. This script
# is thus to be invoked via the Makefile.
#
# We import unicode_literals until people have understood how unicode
# with bytes and strings changed in python2->python3.
# from __future__ import unicode_literals
import os
import os.path
import sys
import re
import gettext
import glob
import codecs
import jinja2
import i18nfix
from pathlib import Path
import hashlib
from bs4 import BeautifulSoup
from ruamel.yaml import YAML

# TODO: Turn repetition into a class.

env = jinja2.Environment(loader=jinja2.FileSystemLoader(
    os.path.dirname(__file__)),
                         extensions=["jinja2.ext.i18n"],
                         lstrip_blocks=True,
                         trim_blocks=True,
                         undefined=jinja2.StrictUndefined,
                         autoescape=False)


def localized(filename, locale, *args):
    if len(args) == 0:
        return "../" + locale + "/" + filename
    ext = kwargs.get('ext', None)
    if ext is not None:
        lf = filename + "." + locale + "." + ext
        lp = Path(lf)
        if locale == "en" or not lp.is_file():
            return "../" + filename + "." + ext
        else:
            return "../" + lf


def fileop(infile, outfile, action):
    """
    infile: inputfile, Path object
    outfile: outputfile, Path object
    action: action if any, String
    """
    i = Path(infile)
    o = Path(outfile)
    outdir = Path("rendered")
    if i.is_file() is not False:
        if action == "copy":
            # Write content of i to o.
            o.write_text(i.read_text())
        if action == "link":
            o.symlink_to(i)


def write_name(filename, infile, locale, replacer):
    return "./rendered/" + locale + "/" + infile.replace(replacer,
                                                         '').rstrip(".j2")


def sha256sum(_):
    sha256 = hashlib.sha256()
    with io.open(_, mode="rb") as fd:
        content = fd.read()
        sha256.update(content)
    return sha256.hexdigest()


def walksum(_):
    sha256 = hashlib.sha256()
    x = Path(_)
    if not x.exists():
        return -1
    try:
        for root, directories, files in os.walk(_):
            for names in sorted(files):
                filepath = os.path.join(root, names)
                try:
                    fl = open(filepath, 'rb')
                except:
                    fl.close()
                    continue
                while 1:
                    buf = fl.read(4096)
                    if not buf:
                        break
                    sha256.update(hashlib.sha256(buf).hexdigest())
                fl.close()
    except:
        import traceback
        traceback.print_exc()
        return -2
    return sha256.hexdigest()


def rm_rf(directory):
    directory = Path(directory)
    for child in directory.glob('*'):
        if child.is_file():
            child.unlink()
        else:
            rm_rf(child)
    directory.rmdir()


# This generates and switches sites generations, preventing
# in-place modification of the website.
# * save old generation directory name
# * jinja2 creates content in "rendered" (happened before calling this function)
# * calculate sum of "rendered"
# * move "rendered" to out/$sum
# * remove symlink "html_dir"
# * symlink out/$sum to "html_dir"
# * delete old generation directory
def generation_dir(htmldir):
    oldgen = Path(htmldir).resolve()
    # precondition: jinja2 has created the files in "rendered".
    newgen = Path("rendered")
    newgen_sum = walksum(newgen)
    outdir = Path("out")
    outdir.mkdir(parents=True, exist_ok=True)
    newgen_target = Path("out") / newgen_sum
    newgen.rename(newgen_target)
    html = Path(htmldir)
    html.unlink()
    fileop(newgen, html, "link")
    rm_rf(oldgen)


def copy_static(locale, indict):
    for key, value in indict.items():
        print(locale + "/" + key + " ...to... " + locale + "/" + value)


def preview_text(filename):
    with open(filename) as html:
        # html = open(filename).read()
        soup = BeautifulSoup(html, features="lxml")
        for script in soup(["script", "style"]):
            script.extract()
        k = []
        # for i in soup.findAll('p')[1:3]:
        for i in soup.findAll('p')[1]:
            k.append(i)
        b = ''.join(str(e) for e in k)
        text = b.replace("\n", "")
        textreduced = (text[:1000] + '...') if len(text) > 1000 else (text + '..')
        return(textreduced)


def abstract_news(filename):
    return preview_text("news/" + filename + ".j2")


def generate_site(root, conf):
    for in_file in glob.glob(root + "/*.j2"):
        name, ext = re.match(r"(.*)\.([^.]+)$", in_file.rstrip(".j2")).groups()
        tmpl = env.get_template(in_file)

        def self_localized(other_locale):
            """
            Return URL for the current page in another locale.
            """
            return "../" + other_locale + "/" + in_file.replace(
                root + '/', '').rstrip(".j2")

        def url_localized(filename):
            if root == "news":
                return "../../" + locale + "/" + filename
            else:
                return "../" + locale + "/" + filename

        def url_static(filename):
            if root == "news":
                return "../../static/" + filename
            else:
                return "../static/" + filename

        def url_dist(filename):
            if root == "news":
                return "../../dist/" + filename
            else:
                return "../dist/" + filename

        def svg_localized(filename):
            lf = filename + "." + locale + ".svg"
            if locale == "en" or not os.path.isfile(lf):
                return "../" + filename + ".svg"
            else:
                return "../" + lf

        def url(x):
            # TODO: look at the app root environment variable
            # TODO: check if file exists
            #if root == "news":
            #    return "../" + "../" + x
            #else:
            #    return "../" + x
            return "../" + x

        for l in glob.glob("locale/*/"):
            locale = os.path.basename(l[:-1])

            tr = gettext.translation("messages",
                                     localedir="locale",
                                     languages=[locale])

            tr.gettext = i18nfix.wrap_gettext(tr.gettext)

            env.install_gettext_translations(tr, newstyle=True)

            content = tmpl.render(lang=locale,
                                  lang_full=conf["langs_full"][locale],
                                  url=url,
                                  meetingnotesdata=conf["meetingnotes"],
                                  newsdata=conf["newsposts"],
                                  videosdata=conf["videoslist"],
                                  self_localized=self_localized,
                                  url_localized=url_localized,
                                  url_static=url_static,
                                  url_dist=url_dist,
                                  svg_localized=svg_localized,
                                  filename=name + "." + ext)

            if root == "news":
                out_name = "./rendered/" + locale + "/" + root + "/" + in_file.replace(
                    root + '/', '').rstrip(".j2")
            else:
                out_name = "./rendered/" + locale + "/" + in_file.replace(
                    root + '/', '').rstrip(".j2")

            outdir = Path("rendered")

            if root == "news":
                langdir = outdir / locale / root
            else:
                langdir = outdir / locale

            langdir.mkdir(parents=True, exist_ok=True)

            with codecs.open(out_name, "w", encoding='utf-8') as f:
                f.write(content)


def main():
    # rm_rf("rendered")
    yaml=YAML(typ='safe')
    site_configfile=Path("www.yml")
    conf=yaml.load(site_configfile)

    for item in conf["newsposts"]:
        item['abstract'] = abstract_news(item['page'])
    print("generating template")
    generate_site("template", conf)
    print("generating news")
    generate_site("news", conf)

#    for l in glob.glob("locale/*/"):
#        locale = os.path.basename(l[:-1])
#        copy_static (locale, staticfiles)
# generate_rss
#print("running generation")
#generation_dir

if __name__ == "__main__":
    main()