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
|
#!/usr/bin/env python
"""Usage: bupa <filename>
Options:
-h --help Show this screen.
"""
from jinja2 import Environment, FileSystemLoader
import glob, yaml, docopt, datetime
from docutils import core
from pyatom import AtomFeed
def reST_to_html(s):
return core.publish_parts(s, writer_name='html4css1')['fragment']
def filename_to_id(file_name):
return int(file_name.split('/')[-1].
split('-')[0].
split('.')[0])
class Page(object):
def __init__(self, page_dict):
self.id = None
self.title = page_dict['title']
self.header = page_dict['header']
self.date = page_dict['date']
self.author = page_dict['author']
if 'author_link' in page_dict:
self.author_link = page_dict['author_link']
self.body = page_dict['body'][0]
def parse(filename):
with open(filename, 'rb') as file:
data = file.read().decode('utf-8')
frontmatter, *body = data.split('\n\n', 1)
frontmatter = frontmatter.partition('\n')[2]
frontmatter = yaml.load(frontmatter)
page_dict = frontmatter.copy()
page_dict['body'] = body
if not 'header' in page_dict:
page_dict['header'] = page_dict['title']
page = Page(page_dict)
return page
def build_journal(env):
template = env.get_template('journal.html')
entry_tmpl = env.get_template('entry.html')
files = glob.glob('src/journal/*.rst')
files.sort(key=filename_to_id, reverse=True)
feed = AtomFeed(
title="kyrias’ journal",
subtitle="The lost journal of Kyrias",
feed_url="https://theos.kyriasis.com/~kyrias/journal.xml",
url="https://theos.kyriasis.com/~kyrias/journal.html",
author={'name': 'Johannes Löthberg',
'uri': 'https://theos.kyriasis.com/~kyrias/about.html'},
updated=datetime.datetime.utcnow(),
generator=('bupa', '', '0.0.1')
)
entries = []
for file in files:
entry = parse(file)
entry.id = filename_to_id(file)
entry.page = file.split('/', 1)[1].split('.', 1)[0] + '.html'
entry.body = reST_to_html(entry.body)
entries += [entry]
with open('build/'+entry.page, 'wb') as file:
file.write(entry_tmpl.render(entry=entry, title='~/journal',
header='The lost journal').encode('utf-8'))
feed.add(
title=entry.title,
content=entry.body,
content_type="html",
author={'name': entry.author, 'uri': entry.author_link},
url="https://theos.kyriasis.com/~kyrias/"+entry.page,
updated=entry.date
)
with open('build/journal.html', 'wb') as file:
file.write(template.render(entries=entries, title='~/journal',
header='The lost journal').encode('utf-8'))
with open('build/journal.xml', 'wb') as file:
file.write(feed.to_string().encode('utf-8'))
def build_page(env, templ_name, pagename):
template = env.get_template(templ_name)
page = parse('src/'+pagename+'.rst')
page.body = reST_to_html(page.body)
page.article_id = pagename
with open('build/'+pagename+'.html', 'wb') as file:
file.write(template.render(page=page, title=page.title, header=page.header).encode('utf-8'))
def main():
arguments = docopt.docopt(__doc__)
env = Environment(loader=FileSystemLoader('src/templates'))
if arguments['<filename>'] == 'journal.html':
build_journal(env)
elif arguments['<filename>'] == 'index.html':
build_page(env, 'page.html', 'index')
elif arguments['<filename>'] == 'about.html':
build_page(env, 'page.html', 'about')
if __name__ == '__main__':
main()
|