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
9
GITHUB_BUILD_DIR = "docs" # Separate because this site is built with an action that won't work if they aren't
10
LOCAL_BUILD_DIR = "build"
11
12
BUILD_DIRECTORY = GITHUB_BUILD_DIR if len(argv) > 1 and argv[1] == "gh-pages-deploy" else LOCAL_BUILD_DIR
13
14
PAGES = {
15
"index.html": "index.html",
16
"blog-list.html": "blog/index.html",
17
"blog-feed.rss": "blog/feed.rss",
18
"link-tree.html": "link-tree.html",
19
"404.html": "404.html"
20
}
21
22
def WipeFinalDir():
23
if not PathExists(BUILD_DIRECTORY):
24
CreateDirectory(BUILD_DIRECTORY)
25
for item in ListDirectory(BUILD_DIRECTORY):
26
path = BUILD_DIRECTORY + "/" + item
27
if IsFile(path):
28
DeleteFile(path)
29
continue
30
DeleteDirectory(path)
31
32
def PostSortHelper(Post):
33
return datetime.strptime(Post["date"], "%Y %b %d")
34
35
def GetBlogList():
36
PostSlugs = ListDirectory("blog-posts")
37
Posts = []
38
for slug in PostSlugs:
39
with open("blog-posts/" + slug, encoding="utf-8") as MDFile:
40
PostHTML = RenderMarkdown(MDFile.read())
41
Item = PostHTML.metadata
42
Item["content"] = PostHTML
43
Item["pathname"] = slug.replace(".md", ".html")
44
Posts.append(Item)
45
PostsByDate = sorted(Posts, key=PostSortHelper, reverse=True)
46
return PostsByDate
47
48
PostList = GetBlogList()
49
50
def RenderPosts():
51
for post in ListDirectory("blog-posts"):
52
path = "blog-posts/" + post
53
RenderedHTML: str
54
PostMD: str
55
PostPath = post.replace(".md", ".html")
56
PlaintextPath = post.replace(".md", ".txt")
57
with open(path, "r", encoding="utf-8") as PostContent:
58
PostMD = PostContent.read()
59
PostHTML = RenderMarkdown(PostMD)
60
Title = PostHTML.metadata["title"]
61
PostDate = PostHTML.metadata["date"]
62
RenderedHTML = RenderTemplate("blog-post.html", Title=Title, PostDate=PostDate, Content=PostHTML, PostPath=PostPath, PlaintextPath=PlaintextPath)
63
with open(BUILD_DIRECTORY + "/blog/" + PostPath, "w", encoding="utf-8") as PostPlaintext:
64
PostPlaintext.write(RenderedHTML)
65
with open(BUILD_DIRECTORY + "/blog/" + PlaintextPath, "w", encoding="utf-8") as PostPlaintext:
66
PostPlaintext.write(PostMD)
67
68
def RenderPage(PageInput: str, ContentDest: str, **kwargs):
69
with open(BUILD_DIRECTORY + "/" + ContentDest, "w", encoding="utf-8") as DestLocation:
70
DestLocation.write(RenderTemplate(PageInput, **kwargs))
71
72
if __name__ == "__main__":
73
print("Wiping directory")
74
WipeFinalDir()
75
print("Creating blog holder")
76
CreateDirectory(BUILD_DIRECTORY + "/blog")
77
print("Rendering posts")
78
RenderPosts()
79
print("Copying static directory")
80
CopyDirectory("static", BUILD_DIRECTORY)
81
82
print("Building pages")
83
for file, path in PAGES.items():
84
RenderPage(file, path, PostList=PostList)
85
86
pass