-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
80 lines (62 loc) · 2.48 KB
/
Copy pathbuild.py
File metadata and controls
80 lines (62 loc) · 2.48 KB
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
import json
import os
import re
from datetime import datetime
from collections import OrderedDict
def read_json(fname):
with open(fname) as data_file:
data = json.load(data_file)
return data
def read_file(fname):
with open(fname) as f:
content = f.readlines()
return "".join([x for x in content])
def write_file(fname, content):
target = open(fname, 'w')
target.write(content)
target.close()
def delete_file(fname):
try:
os.remove(fname)
except OSError:
pass
def rw_replace(fname, old, new):
c = read_file(fname)
delete_file(fname)
c = re.sub(old, new, c)
write_file(fname, c)
def render_post(template, title, date, content, fname):
delete_file(fname)
post = '<div><h2 id="post_title">'+title+'</h2>'
post += '<p id="post_date">'+date+'</p>'
post += '<div id="post_body" class="page_body">'+content+'</div></div>'
post = template.replace('<div id="content"></div>',
'<div id="content">'+post+'</div>')
write_file(fname, post)
def render_archive(template, fname, posts):
content = '<div><dl class="archive" id="arch">'
for f in posts:
date = posts[f]['date']
title = posts[f]['title']
content += '<dt>'+date+'</dt><dd><a href="./' + f + '">'+title+'</a></dd>'
content += '</dl></div>'
render_post(template, 'Archive', '', content, fname)
def render_about(template, source, target):
render_post(template, 'About', '', read_file(source), target)
CONTENT_PREF = 'content/'
RESULT_PREF = 'posts/'
MAINTEMPLATE_FILE = 'assets/template.html'
INDEXJSON_FILE = CONTENT_PREF + 'index.json'
# set most recent post as the home link in main template and default redirect in index.html
posts = {k:v for k,v in read_json(INDEXJSON_FILE).items() if 'date' in v.keys()}
posts = dict(sorted(posts.items(), key=lambda i: datetime.strptime(i[1]['date'], '%B %d, %Y'), reverse=True))
rw_replace(MAINTEMPLATE_FILE, '<a href="(.*).html"><img src=', '<a href="' + list(posts.keys())[0] + '"><img src=')
rw_replace('index.html', '/(.*).html', '/' + list(posts.keys())[0]) # used for redirection
TEMPLATE = read_file(MAINTEMPLATE_FILE)
render_archive(TEMPLATE, RESULT_PREF + 'archive.html', posts)
render_about(TEMPLATE, CONTENT_PREF + 'about.html', RESULT_PREF + 'about.html')
for f in posts:
render_post(TEMPLATE,
posts[f]['title'],
posts[f]['date'],
read_file(CONTENT_PREF + f), RESULT_PREF + f)