summaryrefslogtreecommitdiffstats
path: root/scripts/build.py
blob: e0290523a329488928ceb25efca81a043c87ec99 (plain)
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
#!/usr/bin/env python3

import os.path
import json

from jinja2 import Environment, FileSystemLoader

from utils.reST import reST_to_html


def build_entry(jenv, fname, entry, older=None, newer=None):
    template = jenv.get_template('entry.html')
    source_file = 'entries/{}.rst'.format(entry['file'])
    with open(source_file, 'r') as f:
        source = f.read()

    rendered_source = reST_to_html(source)

    rendered = template.render(entry=entry,
                               older=older,
                               newer=newer,
                               content=rendered_source)
    with open('out/{}.html'.format(fname), 'w+') as f:
        f.write(rendered)


def build_archive(jenv, filename, entries):
    archive_template = jenv.get_template('archive.html')
    rendered = archive_template.render(entries=entries)
    with open('out/{}.html'.format(filename), 'w+') as f:
        f.write(rendered)


def load_metadata(name):
    with open(name) as f:
        entries = json.load(f)

    published = []
    unpublished = []

    for entry in entries:
        path = 'entries/{}.rst'.format(entry['file'])
        if not os.path.isfile(path):
            print('''Source file '{}' for entry '{}' does not exist. Skipping.'''
                  .format(path, entry['title']))
            continue

        if entry.get('published', "True") == "True":
            published.append(entry)
        else:
            unpublished.append(entry)

    return (published, unpublished)


if __name__ == '__main__':
    jenv = Environment(loader=FileSystemLoader('templates'),
                       trim_blocks=True,
                       lstrip_blocks=True)

    published, unpublished = load_metadata('metadata.json')

    for entry in unpublished:
        build_entry(jenv, entry['file'], entry)

    for index, entry in enumerate(published):
        older = published[index-1:index]
        newer = published[index+1:index+2]
        build_entry(jenv, entry['file'], entry, older, newer)

    build_entry(jenv, 'index', published[-1], older=published[-2:-1])

    build_archive(jenv, 'archive', published)