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
print("Directory didn't existing, creating it...")
25
CreateDirectory(BUILD_DIRECTORY)
26
return
27
print("Directory exists, wiping it...")
28
for item in ListDirectory(BUILD_DIRECTORY):
29
path = BUILD_DIRECTORY + "/" + item
30
if IsFile(path):
31
DeleteFile(path)
32
continue
33
DeleteDirectory(path)
34
35
def PostSortHelper(Post):
36
return datetime.strptime(Post["date"], "%Y %b %d")
37
38
def GetBlogList():
39
print("Grabbing post list")
40
PostSlugs = ListDirectory("blog-posts")
41
Posts = []
42
for slug in PostSlugs:
43
print("Grabbing post list blog-posts/%s" % (slug))
44
with open("blog-posts/" + slug, encoding="utf-8") as MDFile:
45
PostHTML = RenderMarkdown(MDFile.read())
46
Item = PostHTML.metadata
47
Item["content"] = PostHTML
48
Item["pathname"] = slug.replace(".md", ".html")
49
Posts.append(Item)
50
PostsByDate = sorted(Posts, key=PostSortHelper, reverse=True)
51
return PostsByDate
52
53
PostList = GetBlogList()
54
55
def RenderPosts():
56
for post in ListDirectory("blog-posts"):
57
path = "blog-posts/" + post
58
RenderedHTML: str
59
PostMD: str
60
PostPath = post.replace(".md", ".html")
61
PlaintextPath = post.replace(".md", ".txt")
62
with open(path, "r", encoding="utf-8") as PostContent:
63
PostMD = PostContent.read()
64
PostHTML = RenderMarkdown(PostMD)
65
Title = PostHTML.metadata["title"]
66
PostDate = PostHTML.metadata["date"]
67
RenderedHTML = RenderTemplate("blog-post.html", Title=Title, PostDate=PostDate, Content=PostHTML, PostPath=PostPath, PlaintextPath=PlaintextPath)
68
print("Building blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PostPath))
69
with open(BUILD_DIRECTORY + "/blog/" + PostPath, "w", encoding="utf-8") as PostHTMLFile:
70
PostHTMLFile.write(RenderedHTML)
71
print("Copying blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PlaintextPath))
72
with open(BUILD_DIRECTORY + "/blog/" + PlaintextPath, "w", encoding="utf-8") as PostPlaintext:
73
PostPlaintext.write(PostMD)
74
75
def RenderPage(PageInput: str, ContentDest: str, **kwargs):
76
print("Building views/%s to %s/%s" % (PageInput, BUILD_DIRECTORY, ContentDest))
77
with open(BUILD_DIRECTORY + "/" + ContentDest, "w", encoding="utf-8") as DestLocation:
78
DestLocation.write(RenderTemplate(PageInput, **kwargs))
79
80
if __name__ == "__main__":
81
print("Wiping directory")
82
WipeFinalDir()
83
print("Creating blog holder")
84
CreateDirectory(BUILD_DIRECTORY + "/blog")
85
print("Rendering posts")
86
RenderPosts()
87
print("Copying static directory")
88
CopyDirectory("static", BUILD_DIRECTORY)
89
90
print("Building pages")
91
for file, path in PAGES.items():
92
RenderPage(file, path, PostList=PostList)
93
94
pass