build.py
Python script, ASCII text executable
1
from Renderers import RenderTemplate, RenderMarkdown
2
from sys import argv
3
from shutil import rmtree as DeleteDirectory
4
from os import mkdir as CreateDirectory, listdir as ListDirectory, unlink as DeleteFile
5
from os.path import isfile as IsFile, exists as PathExists
6
from distutils.dir_util import copy_tree as CopyDirectory
7
from datetime import datetime
8
from json import dump as DumpJSON
9
from yaml import safe_load as LoadYML
10
11
GITHUB_BUILD_DIR = "docs" # Separate because this site is built with an action that won't work if they aren't
12
LOCAL_BUILD_DIR = "build"
13
14
BUILD_DIRECTORY = GITHUB_BUILD_DIR if len(argv) > 1 and argv[1] == "gh-pages-deploy" else LOCAL_BUILD_DIR
15
16
PAGES = {
17
"index.html": "index.html",
18
"blog-list.html": "blog/index.html",
19
"blog-feed.rss": "blog/feed.rss",
20
"blog-feed.atom": "blog/feed.atom",
21
"404.html": "404.html"
22
}
23
24
DISALLOWED_SITEMAP = [
25
"404.html",
26
"blog-feed.rss"
27
]
28
29
REDIRECTS = [
30
("link-tree.html", "list/link-tree.html") # Old location -> new location
31
]
32
33
SITEMAP_HREF = "https://steve0greatness.github.io/"
34
sitemap = []
35
36
def WipeFinalDir():
37
if not PathExists(BUILD_DIRECTORY):
38
print("Directory didn't existing, creating it...")
39
CreateDirectory(BUILD_DIRECTORY)
40
return
41
print("Directory exists, wiping it...")
42
for item in ListDirectory(BUILD_DIRECTORY):
43
path = BUILD_DIRECTORY + "/" + item
44
if IsFile(path):
45
DeleteFile(path)
46
continue
47
DeleteDirectory(path)
48
49
def PostDateToDateObj(Date):
50
return datetime.strptime(Date, "%Y %b %d")
51
52
def PostSortHelper(Post):
53
return PostDateToDateObj(Post["date"])
54
55
def GetBlogList():
56
print("Grabbing post list")
57
PostSlugs = ListDirectory("blog-posts")
58
Posts = []
59
for slug in PostSlugs:
60
print("Grabbing post list blog-posts/%s" % (slug))
61
with open("blog-posts/" + slug, encoding="utf-8") as MDFile:
62
PostHTML = RenderMarkdown(MDFile.read())
63
Item = PostHTML.metadata
64
Item["content"] = PostHTML
65
Item["rss-post-time"] = PostDateToDateObj(Item["date"]).strftime("%a, %d %b %Y") + " 00:00:00 GMT"
66
Item["atom-post-time"] = PostDateToDateObj(Item["date"]).strftime("%Y-%m-%d") + "T00:00:00Z"
67
Item["atom-update-time"] = Item["atom-post-time"]
68
if "updated" in Item:
69
Item["atom-update-time"] = PostDateToDateObj(Item["updated"]).strftime("%Y-%m-%d") + "T00:00:00Z"
70
Item["pathname"] = slug.replace(".md", ".html")
71
Posts.append(Item)
72
PostsByDate = sorted(Posts, key=PostSortHelper, reverse=True)
73
return PostsByDate
74
75
PostList = []
76
77
def ListParseCategory(Obj, depth):
78
html = "<h%d id=\"%s\">%s</h%d>" % (2+depth, Obj["id"], Obj["title"], 2+depth)
79
if "paragraph" in Obj:
80
html += "<p>%s</p>" % Obj["paragraph"]
81
listType = "ul"
82
if "list-type" in Obj and Obj["list-type"] == "ordered":
83
listType = "ol"
84
html += "<%s>" % listType
85
for item in Obj["list"]:
86
html += "<li>" + LIST_PARSER_DICT[item["type"]](item, depth + 1) + "</li>"
87
html += "</%s>" % listType
88
return html
89
90
def ListParseLink(Obj, depth):
91
html = "<a href=\"%s\">" % Obj["href"]
92
text = Obj["text"]
93
if "text-type" in Obj and Obj["text-type"] == "text/markdown":
94
text = RenderMarkdown(text).replace("<p>", "").replace("</p>", "")
95
html += text + "</a>"
96
if "comment" in Obj:
97
html += "(%s)" % Obj["comment"]
98
return html
99
100
def ListParseText(Obj, depth):
101
text = Obj["text"]
102
# if "text-type" in Obj and Obj["text-type"] == "text/markdown":
103
# print(RenderMarkdown(text))
104
# text = RenderMarkdown(text) # this doesn't work???
105
if "comment" in Obj:
106
text += "(%s)" % Obj["comment"]
107
return text
108
109
LIST_PARSER_DICT = {
110
"category": ListParseCategory,
111
"link": ListParseLink,
112
"text": ListParseText,
113
}
114
115
def GetLists():
116
ListSlugs = ListDirectory("lists")
117
Lists = []
118
for slug in ListSlugs:
119
List = {
120
"title": "",
121
"content": "",
122
"filename": slug
123
}
124
with open("lists/" + slug) as ListYML:
125
ListDict = LoadYML(ListYML.read())
126
List["title"] = ListDict["title"]
127
if "paragraph" in ListDict:
128
List["content"] += "<p>%s</p>" % ListDict["paragraph"]
129
for item in ListDict["list"]:
130
List["content"] += LIST_PARSER_DICT[item["type"]](item, 0)
131
Lists.append(List)
132
return Lists
133
134
def RenderPosts():
135
for post in ListDirectory("blog-posts"):
136
path = "blog-posts/" + post
137
RenderedHTML: str
138
PostMD: str
139
PostPath = post.replace(".md", ".html")
140
PlaintextPath = post.replace(".md", ".txt")
141
with open(path, "r", encoding="utf-8") as PostContent:
142
PostMD = PostContent.read()
143
PostHTML = RenderMarkdown(PostMD)
144
Title = PostHTML.metadata["title"]
145
PostDate = PostHTML.metadata["date"]
146
Revised = False
147
if "updated" in PostHTML.metadata:
148
Revised = PostHTML.metadata["updated"]
149
RenderedHTML = RenderTemplate("blog-post.html", Revised=Revised, Title=Title, PostDate=PostDate, Content=PostHTML, PostPath=PostPath, PlaintextPath=PlaintextPath)
150
print("Building blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PostPath))
151
with open(BUILD_DIRECTORY + "/blog/" + PostPath, "w", encoding="utf-8") as PostHTMLFile:
152
PostHTMLFile.write(RenderedHTML)
153
print("Copying blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PlaintextPath))
154
with open(BUILD_DIRECTORY + "/blog/" + PlaintextPath, "w", encoding="utf-8") as PostPlaintext:
155
PostPlaintext.write(PostMD)
156
sitemap.append(SITEMAP_HREF + "/blog/" + PostPath)
157
158
def RenderPage(PageInput: str, ContentDest: str, AllowSitemap: bool = True, **kwargs):
159
print("Building views/%s to %s/%s" % (PageInput, BUILD_DIRECTORY, ContentDest))
160
if AllowSitemap:
161
sitemap.append(SITEMAP_HREF + ContentDest)
162
with open(BUILD_DIRECTORY + "/" + ContentDest, "w", encoding="utf-8") as DestLocation:
163
DestLocation.write(RenderTemplate(PageInput, **kwargs))
164
165
def CreateJSONFeed():
166
CreatedJSON = {
167
"version": "https://jsonfeed.org/version/1",
168
"title": "Steve0Greatness' Blog",
169
"home_page_url": "https://steve0greatness.github.io",
170
"feed_url": "https://steve0greatness.github.io/blog/feed.rss",
171
"language": "en-US",
172
"favicon": "https://steve0greatness.github.io/favicon.ico",
173
"description": "A blog by a human being.",
174
"authors": [
175
{
176
"name": "Steve0Greatness",
177
"url": "https://steve0greatness.github.io"
178
}
179
],
180
"items": []
181
}
182
for post in PostList:
183
CreatedJSON["items"].append({
184
"id": "https://steve0greatness.github.io/blog/" + post["pathname"],
185
"title": "JSON Feed version 1.1",
186
"icon": "https://steve0greatness.github.io/favicon.ico",
187
"content_html": post["content"],
188
"date_published": post["atom-post-time"],
189
"date_modified": post["atom-update-time"],
190
"url": "https://steve0greatness.github.io/blog/" + post["pathname"]
191
})
192
with open(BUILD_DIRECTORY + "/blog/feed.json", "w") as JSONFeedFile:
193
DumpJSON(CreatedJSON, JSONFeedFile)
194
195
def RenderLists():
196
Lists = GetLists()
197
CreateDirectory(BUILD_DIRECTORY + "/list/")
198
ListIndex = "<ul>"
199
for List in Lists:
200
FileLocation = "/list/" + List["filename"].replace(".yml", ".html")
201
Title = List["title"]
202
print("%s -> %s" % ("lists/" + List["filename"], BUILD_DIRECTORY + FileLocation))
203
with open(BUILD_DIRECTORY + FileLocation, "w") as file:
204
file.write(RenderTemplate("list.html", Content=List["content"], Title=Title, Location=FileLocation))
205
ListIndex += "<li><a href=\"%s\">%s</a></li>" % (FileLocation, Title)
206
ListIndex += "</ul>"
207
print("Building list index")
208
with open(BUILD_DIRECTORY + "/list/index.html", "w") as file:
209
file.write(RenderTemplate("list-index.html", Content=ListIndex))
210
211
def main():
212
PostList = GetBlogList()
213
print("Wiping directory")
214
WipeFinalDir()
215
print("Creating blog holder")
216
CreateDirectory(BUILD_DIRECTORY + "/blog")
217
print("Rendering posts")
218
RenderPosts()
219
CreateJSONFeed()
220
print("Copying static directory")
221
CopyDirectory("static", BUILD_DIRECTORY)
222
print("Creating lists")
223
RenderLists()
224
225
print("Building pages")
226
for file, path in PAGES.items():
227
if file in DISALLOWED_SITEMAP:
228
RenderPage(file, path, False, PostList=PostList)
229
continue
230
RenderPage(file, path, PostList=PostList)
231
232
print("Building redirects")
233
for OldLocation, NewLocation in REDIRECTS:
234
RenderPage("redirect.html", OldLocation, False, redirect=NewLocation)
235
236
with open(BUILD_DIRECTORY + "/sitemap.txt", "w") as SitemapFile:
237
SitemapFile.write("\n".join(sitemap))
238
239
if __name__ == "__main__":
240
main()