summaryrefslogtreecommitdiffstats
path: root/scripts/build.py
blob: 52f277546d53d944be471078bbff37bb397040e0 (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
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
#!/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, entry, older=None, newer=None):
    entry_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 = entry_template.render(entry=entry,
                                     older=older,
                                     newer=newer,
                                     content=rendered_source)
    with open('out/{}.html'.format(entry['file']), 'w+') as f:
        f.write(rendered)


def build_index(jenv, entry, older=None):
    index_template = jenv.get_template('index.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 = index_template.render(entry=entry,
                                     older=older,
                                     content=rendered_source)
    with open('out/index.html', 'w+') as f:
        f.write(rendered)


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


def split_entries(entries):
    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)

    with open('entries.json') as f:
        entries = json.load(f)

    published, unpublished = split_entries(entries)

    for entry in unpublished:
        build_entry(jenv, entry)

    for index, entry in enumerate(published):
        older = None
        newer = None
        if index > 0:
            older = published[index-1]
        if index < len(published) - 1:
            newer = published[index+1]

        build_entry(jenv, entry, older, newer)

    if len(published) > 1:
        build_index(jenv, published[-1], published[-2])
    else:
        build_index(jenv, published[-1], None)

    build_archive(jenv, published)