-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
160 lines (125 loc) · 4.21 KB
/
build.py
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
from datetime import datetime
from feedgen.feed import FeedGenerator
from functools import cache
from jinja2 import Environment, FileSystemLoader, select_autoescape
from markdown import markdown
from os import mkdir, listdir
from os.path import abspath, dirname, isdir, join, realpath
from re import match, search
from shutil import copy, copytree, rmtree
from urllib.parse import urljoin
URL = "http://iankinsey.com/"
FEED_NAME = "rss.xml"
SITEMAP_NAME = "sitemap.xml"
SLUG_REGEX = r"\d+-\d+-\d+-[a-zA-Z\-]+"
SLUG_DATE_REGEX = r"\d+-\d+-\d+"
PROJECT_ROOT = abspath(dirname(realpath(__file__)))
INDEX_TEMPLATE = "index.jinja"
ARTICLE_TEMPLATE = "article.jinja"
SITEMAP_TEMPLATE = "sitemap.jinja"
STATIC_DIR = join(PROJECT_ROOT, 'static')
TEMPLATES_DIR = join(PROJECT_ROOT, 'templates')
ARTICLES_DIR = join(PROJECT_ROOT, 'articles')
DIST_DIR = join(PROJECT_ROOT, 'dist')
ARTICLE_DIST_DIR = join(DIST_DIR, 'articles')
PAGES = [
'about',
'projects'
]
ARTICLES = [
i for i in listdir(ARTICLES_DIR)
if match(SLUG_REGEX, i)
]
ARTICLES.sort(reverse=True)
ENV = Environment(
loader=FileSystemLoader(TEMPLATES_DIR),
autoescape=select_autoescape(),
trim_blocks=True,
lstrip_blocks=True
)
ENV.globals['current_year'] = datetime.now().year
def render_pages():
for page in PAGES:
template = ENV.get_template(f"{page}.jinja")
contents = template.render()
open(join(DIST_DIR, f"{page}.html"), 'w').write(contents)
def render_index():
articles = [get_article(a)['html'] for a in ARTICLES]
template = ENV.get_template(INDEX_TEMPLATE)
contents = template.render(articles=articles)
open(join(DIST_DIR, "index.html"), 'w').write(contents)
def render_articles():
for article in ARTICLES:
render_article(article)
def render_article(name):
contents = get_article_contents(name)
open(join(ARTICLE_DIST_DIR, f'{name}.html'), 'w').write(contents)
def get_article_contents(name):
article = get_article(name)
template = ENV.get_template(ARTICLE_TEMPLATE)
return template.render(title=article['title'], article=article['html'])
@cache
def get_article(article):
md = open(join(ARTICLES_DIR, article)).read()
short_url = f"/articles/{article}.html"
url = urljoin(URL, short_url)
title = md.split("\n")[0].replace("#", "").strip()
datestr = search(SLUG_DATE_REGEX, article)[0]
date = datetime.strptime(datestr, '%Y-%m-%d')
published = f'####<div class="pubdate">{date.strftime("%B %d, %Y")}</div>'
md_content = md.replace(title, f"[{title}]({short_url})\n{published}", 1)
return {
"html": markdown(md_content, extensions=['codehilite']),
"title": title,
"name": article,
"url": url,
"short_url": url,
"date": f"{date}T00:00:00+00:00",
"parsed_date": date
}
def render_rss():
feed_url = urljoin(URL, FEED_NAME)
fg = FeedGenerator()
fg.title("Ian Kinsey")
fg.subtitle("Thoughts in software development")
fg.author({"name": "Ian Kinsey", "email": "ikrss@nym.hush.com"})
fg.link(href=URL, rel="alternate")
fg.link(href=feed_url, rel="self")
fg.language("en")
for name in ARTICLES:
article = get_article(name)
entry = fg.add_entry()
entry.title(article['title'])
entry.published(article['date'])
entry.link(href=article['url'])
fg.rss_file(join(DIST_DIR, FEED_NAME))
def render_sitemap():
template = ENV.get_template(SITEMAP_TEMPLATE)
articles = [get_article(a) for a in ARTICLES]
urls = [urljoin(URL, f"{p}.html") for p in PAGES] + [URL]
contents = template.render(urls=urls, articles=articles)
open(join(DIST_DIR, SITEMAP_NAME), 'w').write(contents)
def copy_static_assets():
for file_name in listdir(STATIC_DIR):
path = join(STATIC_DIR, file_name)
if isdir(path):
copytree(path, join(DIST_DIR, file_name))
else:
copy(path, DIST_DIR)
def setup():
try:
rmtree(DIST_DIR)
except FileNotFoundError:
pass
mkdir(DIST_DIR)
mkdir(ARTICLE_DIST_DIR)
def build():
setup()
render_pages()
render_articles()
render_index()
render_rss()
render_sitemap()
copy_static_assets()
if __name__ == "__main__":
build()