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

import os.path
import json
from collections import OrderedDict

from jinja2 import Environment, FileSystemLoader

from utils.reST import reST_to_html


def build_entry(template, fname, entry, older=None, newer=None):
    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(template, filename, title, entries):
    rendered = template.render(title=title, 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 = []
    tags = {}

    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)

        for tag in entry.get('tags', []):
            if not tag in tags:
                tags[tag] = []

            tags[tag].append(entry)

    return (published, unpublished, tags)


if __name__ == '__main__':
    jenv = Environment(loader=FileSystemLoader('templates'),
                       trim_blocks=True,
                       lstrip_blocks=True)
    entry_tmpl = jenv.get_template('entry.html')
    archive_tmpl = jenv.get_template('archive.html')
    tags_tmpl = jenv.get_template('tags.html')

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

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

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

    build_entry(entry_tmpl, 'index', published[-1], older=published[-2:-1])
    build_archive(archive_tmpl, 'archive', 'Archive', published)

    tag_list = OrderedDict(sorted(map(lambda k: (k, 0), tags.keys())))
    for tag in tags:
        build_archive(archive_tmpl, 'tag-{}'.format(tag), tag, tags[tag])
        tag_list[tag] = len(tags[tag])

    build_archive(tags_tmpl, 'tags', 'Tags', tag_list)