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 • 2.86 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
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
with open(path, "r", encoding="utf-8") as PostContent:
55
PostHTML = RenderMarkdown(PostContent.read())
56
Title = PostHTML.metadata["title"]
57
PostDate = PostHTML.metadata["date"]
58
RenderedHTML = RenderTemplate("blog-post.html", Title=Title, PostDate=PostDate, Content=PostHTML)
59
with open(BUILD_DIRECTORY + "/blog/" + post.replace(".md", ".html"), "w", encoding="utf-8") as PostLocation:
60
PostLocation.write(RenderedHTML)
61
62
def RenderPage(PageInput: str, ContentDest: str, **kwargs):
63
with open(BUILD_DIRECTORY + "/" + ContentDest, "w", encoding="utf-8") as DestLocation:
64
DestLocation.write(RenderTemplate(PageInput, **kwargs))
65
66
if __name__ == "__main__":
67
print("Wiping directory")
68
WipeFinalDir()
69
print("Creating blog holder")
70
CreateDirectory(BUILD_DIRECTORY + "/blog")
71
print("Rendering posts")
72
RenderPosts()
73
print("Copying static directory")
74
CopyDirectory("static", BUILD_DIRECTORY)
75
76
print("Building pages")
77
for file, path in PAGES.items():
78
RenderPage(file, path, PostList=PostList)
79
80
pass