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

from collections import namedtuple, Counter
from datetime import date
from glob import glob
from itertools import chain
from jinja2 import Environment, FileSystemLoader
from markdown import markdown
from markupsafe import Markup
from operator import attrgetter
from subprocess import call as run
from pygments.formatters import HtmlFormatter
import meta
import os

CONTENT = './content'
OUTPUT = './output'
KWORDS = { 'Tomasz Kramkowski', 'the-tk', 'blog', 'projects', 'TK', 'Arch-TK' }
ERRORS = {
    403: "Forbidden",
    404: "File Not Found",
    500: "Internal Server Error",
    502: "Bad Gateway",
    503: "Service Unavailable",
    504: "Gateway Timeout",
}

md = lambda s: Markup(markdown(s, extensions=['fenced_code', 'codehilite', 'toc']))

File = namedtuple('File', 'content date slug ext')
def parse(path, has_date=False):
    with open(path) as f:
        content = meta.parse(f.read())
    slug, ext = os.path.splitext(os.path.split(path)[1])
    if has_date:
        y, m, d, slug = slug.split('-', 3)
        dat = date(int(y), int(m), int(d))
    else:
        dat = None
    return File(content, dat, slug, ext)

Post = namedtuple('Post', 'title date tags pre content location')
def readpost(path):
    pf = parse(path, has_date=True)
    location = os.path.join('post',
            '{:04}'.format(pf.date.year),
            '{:02}'.format(pf.date.month),
            '{:02}'.format(pf.date.day),
            pf.slug + '.html')
    tags = set(pf.content.meta.get('tags', []))
    return Post(' '.join(pf.content.meta['title']), pf.date, tags,
            pf.content.sects['pre'],
            '\n'.join(pf.content.sects.values()), location)

Project = namedtuple('Project', 'name description source ml aur content location')
def readproject(path):
    pf = parse(path)
    source = pf.content.meta.get('source', [None])[0]
    ml = pf.content.meta.get('ml', [None])[0]
    aur = pf.content.meta.get('aur', [None])[0]
    content = '\n'.join(pf.content.sects.values())
    location = os.path.join('project', pf.slug + '.html')
    return Project(' '.join(pf.content.meta['name']),
            pf.content.sects.get('desc', content), source, ml, aur, content,
            location);

def output(dest, cont):
    dest = os.path.join(OUTPUT, dest)
    destdir = os.path.dirname(dest)
    if not os.path.exists(destdir):
        os.makedirs(destdir)
    with open(dest, 'w') as f:
        f.write(cont)

def outputpost(env, post, tags):
    page = {
        'title': post.title,
        'description': md(post.pre),
        'keywords': ', '.join(KWORDS | post.tags),
        'date': post.date,
        'content': md(post.content),
        'tags': sorted(post.tags & set(tags)),
    }
    output(post.location, env.get_template('post.html').render(page=page))

def outputtag(env, posts, tag):
    page = {
        'title': 'Posts tagged - {}'.format(tag),
        'description': "List of posts tagged '{}'.".format(tag),
        'keywords': ', '.join(KWORDS | {tag}),
        'tag': tag,
        'posts': [ p for p in posts if tag in p.tags ],
    }
    output('tag/{}.html'.format(tag), env.get_template('tag.html').render(page=page))

def outputproject(env, project):
    page = {
        'title': project.name,
        'description': project.description.replace('\n', ' '),
        'keywords': ', '.join(KWORDS | {project.name}),
        'content': md(project.content),
        'source': project.source,
        'ml': project.ml,
        'aur': project.aur,
    }
    output(project.location, env.get_template('project.html').render(page=page))

def main(argv=['generate.py']):
    from getopt import getopt
    global OUTPUT

    opts = dict(getopt(argv[1:], "o:b:")[0])
    OUTPUT = opts.get('-o', OUTPUT)
    baseurl = opts.get('-b', 'https://the-tk.com')

    run(['rm', '-rf', os.path.join(OUTPUT, '*')])

    env = Environment(loader=FileSystemLoader('./templates'), autoescape=True)
    env.globals['site'] = {'baseurl': baseurl}
    env.filters['datefmt'] = lambda d, f='%Y-%m-%d': d.strftime(f)
    env.filters['md'] = md

    posts = glob(os.path.join(CONTENT, 'posts', '*'))
    posts = [p for p in posts if not os.path.split(p)[1].startswith('_')]
    posts = sorted(map(readpost, posts), key=attrgetter('date'), reverse=True)
    tags = Counter(chain(*(p.tags for p in posts)))
    tags = sorted(k for k, v in tags.items() if v > 1)

    page = {
        'title': 'Blog',
        'description': 'New posts will appear chronologically on this page.',
        'keywords': ', '.join(KWORDS | set(tags)),
        'tags': tags,
        'posts': posts,
    }
    output('blog.html', env.get_template('blog.html').render(page=page))
    output('blog.atom.xml', env.get_template('blog.atom.xml').render(page=page))

    projects = glob(os.path.join(CONTENT, 'projects', '*'))
    projects = [p for p in projects if not os.path.split(p)[1].startswith('_')]
    projects = sorted(map(readproject, projects), key=lambda e: e.name.lower())

    page = {
        'title': 'Projects',
        'description': 'This is a listing of all of my notable projects.',
        'keywords': ', '.join(KWORDS),
        'projects': projects,
    }
    output('projects.html', env.get_template('projects.html').render(page=page))

    page = {
        'title': 'the-tk.com',
        'description': 'This is my website. Here you can find some blog posts and descriptions for notable projects.',
        'keywords': ', '.join(KWORDS),
        'posts': posts[:3],
    }
    output('index.html', env.get_template('index.html').render(page=page))

    for post in posts:
        outputpost(env, post, tags)
    for tag in tags:
        outputtag(env, posts, tag)
    for project in projects:
        outputproject(env, project)

    for code, error in ERRORS.items():
        page = {
            'title': 'Error: {} - {}'.format(code, error),
            'code': code,
            'error': error,
        }
        output('error/{}.html'.format(code), env.get_template('error.html').render(page=page))

    with open(os.path.join(OUTPUT, 'style.css'), 'w') as f:
        with open(os.path.join(CONTENT, 'style.css')) as css:
            f.write(css.read())
        f.write(HtmlFormatter().get_style_defs('.codehilite'))
    run(['cp', '-r', os.path.join(CONTENT, 'images'), OUTPUT])

if __name__ == '__main__':
    from sys import argv
    main(argv)