A mirror of my website's source code.

By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 build.py

View raw Download
text/x-script.python • 7.9 kiB
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 PostSortHelper(Post):
50
return datetime.strptime(Post["date"], "%Y %b %d")
51
52
def GetBlogList():
53
print("Grabbing post list")
54
PostSlugs = ListDirectory("blog-posts")
55
Posts = []
56
for slug in PostSlugs:
57
print("Grabbing post list blog-posts/%s" % (slug))
58
with open("blog-posts/" + slug, encoding="utf-8") as MDFile:
59
PostHTML = RenderMarkdown(MDFile.read())
60
Item = PostHTML.metadata
61
Item["content"] = PostHTML
62
Item["pathname"] = slug.replace(".md", ".html")
63
Posts.append(Item)
64
PostsByDate = sorted(Posts, key=PostSortHelper, reverse=True)
65
return PostsByDate
66
67
PostList = GetBlogList()
68
69
def ListParseCategory(Obj, depth):
70
html = "<h%d id=\"%s\">%s</h%d>" % (2+depth, Obj["id"], Obj["title"], 2+depth)
71
if "paragraph" in Obj:
72
html += "<p>%s</p>" % Obj["paragraph"]
73
listType = "ul"
74
if "list-type" in Obj and Obj["list-type"] == "ordered":
75
listType = "ol"
76
html += "<%s>" % listType
77
for item in Obj["list"]:
78
html += "<li>" + LIST_PARSER_DICT[item["type"]](item, depth + 1) + "</li>"
79
html += "</%s>" % listType
80
return html
81
82
def ListParseLink(Obj, depth):
83
html = "<a href=\"%s\">" % Obj["href"]
84
text = Obj["text"]
85
if "text-type" in Obj and Obj["text-type"] == "text/markdown":
86
text = RenderMarkdown(text).replace("<p>", "").replace("</p>", "")
87
html += text + "</a>"
88
if "comment" in Obj:
89
html += "(%s)" % Obj["comment"]
90
return html
91
92
def ListParseText(Obj, depth):
93
text = Obj["text"]
94
# if "text-type" in Obj and Obj["text-type"] == "text/markdown":
95
# print(RenderMarkdown(text))
96
# text = RenderMarkdown(text) # this doesn't work???
97
if "comment" in Obj:
98
text += "(%s)" % Obj["comment"]
99
return text
100
101
LIST_PARSER_DICT = {
102
"category": ListParseCategory,
103
"link": ListParseLink,
104
"text": ListParseText,
105
}
106
107
def GetLists():
108
ListSlugs = ListDirectory("lists")
109
Lists = []
110
for slug in ListSlugs:
111
List = {
112
"title": "",
113
"content": "",
114
"filename": slug
115
}
116
with open("lists/" + slug) as ListYML:
117
ListDict = LoadYML(ListYML.read())
118
List["title"] = ListDict["title"]
119
if "paragraph" in ListDict:
120
List["content"] += "<p>%s</p>" % ListDict["paragraph"]
121
for item in ListDict["list"]:
122
List["content"] += LIST_PARSER_DICT[item["type"]](item, 0)
123
Lists.append(List)
124
return Lists
125
126
def RenderPosts():
127
for post in ListDirectory("blog-posts"):
128
path = "blog-posts/" + post
129
RenderedHTML: str
130
PostMD: str
131
PostPath = post.replace(".md", ".html")
132
PlaintextPath = post.replace(".md", ".txt")
133
with open(path, "r", encoding="utf-8") as PostContent:
134
PostMD = PostContent.read()
135
PostHTML = RenderMarkdown(PostMD)
136
Title = PostHTML.metadata["title"]
137
PostDate = PostHTML.metadata["date"]
138
Revised = False
139
if "updated" in PostHTML.metadata:
140
Revised = PostHTML.metadata["updated"]
141
RenderedHTML = RenderTemplate("blog-post.html", Revised=Revised, Title=Title, PostDate=PostDate, Content=PostHTML, PostPath=PostPath, PlaintextPath=PlaintextPath)
142
print("Building blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PostPath))
143
with open(BUILD_DIRECTORY + "/blog/" + PostPath, "w", encoding="utf-8") as PostHTMLFile:
144
PostHTMLFile.write(RenderedHTML)
145
print("Copying blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PlaintextPath))
146
with open(BUILD_DIRECTORY + "/blog/" + PlaintextPath, "w", encoding="utf-8") as PostPlaintext:
147
PostPlaintext.write(PostMD)
148
sitemap.append(SITEMAP_HREF + "/blog/" + PostPath)
149
150
def RenderPage(PageInput: str, ContentDest: str, AllowSitemap: bool = True, **kwargs):
151
print("Building views/%s to %s/%s" % (PageInput, BUILD_DIRECTORY, ContentDest))
152
if AllowSitemap:
153
sitemap.append(SITEMAP_HREF + ContentDest)
154
with open(BUILD_DIRECTORY + "/" + ContentDest, "w", encoding="utf-8") as DestLocation:
155
DestLocation.write(RenderTemplate(PageInput, **kwargs))
156
157
def CreateJSONFeed():
158
CreatedJSON = {
159
"version": "https://jsonfeed.org/version/1",
160
"title": "Steve0Greatness' Blog",
161
"home_page_url": "https://steve0greatness.github.io",
162
"feed_url": "https://steve0greatness.github.io/blog/feed.rss",
163
"language": "en-US",
164
"favicon": "https://steve0greatness.github.io/favicon.ico",
165
"description": "A blog by a human being.",
166
"authors": [
167
{
168
"name": "Steve0Greatness",
169
"url": "https://steve0greatness.github.io"
170
}
171
],
172
"items": []
173
}
174
for post in PostList:
175
CreatedJSON["items"].append({
176
"id": "https://steve0greatness.github.io/blog/" + post["pathname"],
177
"title": "JSON Feed version 1.1",
178
"content_html": post["content"],
179
"date_published": post["date"],
180
"url": "https://steve0greatness.github.io/blog/" + post["pathname"]
181
})
182
with open(BUILD_DIRECTORY + "/blog/feed.json", "w") as JSONFeedFile:
183
DumpJSON(CreatedJSON, JSONFeedFile)
184
185
def RenderLists():
186
Lists = GetLists()
187
CreateDirectory(BUILD_DIRECTORY + "/list/")
188
for List in Lists:
189
FileLocation = BUILD_DIRECTORY + "/list/" + List["filename"].replace(".yml", ".html")
190
print("%s -> %s" % ("lists/" + List["filename"], FileLocation))
191
with open(FileLocation, "w") as file:
192
file.write(RenderTemplate("list.html", Content=List["content"], Title=List["title"]))
193
194
if __name__ == "__main__":
195
print("Wiping directory")
196
WipeFinalDir()
197
print("Creating blog holder")
198
CreateDirectory(BUILD_DIRECTORY + "/blog")
199
print("Rendering posts")
200
RenderPosts()
201
CreateJSONFeed()
202
print("Copying static directory")
203
CopyDirectory("static", BUILD_DIRECTORY)
204
print("Creating lists")
205
RenderLists()
206
207
print("Building pages")
208
for file, path in PAGES.items():
209
if file in DISALLOWED_SITEMAP:
210
RenderPage(file, path, False, PostList=PostList)
211
continue
212
RenderPage(file, path, PostList=PostList)
213
214
for OldLocation, NewLocation in REDIRECTS:
215
RenderPage("redirect.html", OldLocation, False, redirect=NewLocation)
216
217
with open(BUILD_DIRECTORY + "/sitemap.txt", "w") as SitemapFile:
218
SitemapFile.write("\n".join(sitemap))
219
220
pass