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.82 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
"404.html": "404.html"
19
}
20
21
def WipeFinalDir():
22
if not PathExists(BUILD_DIRECTORY):
23
CreateDirectory(BUILD_DIRECTORY)
24
for item in ListDirectory(BUILD_DIRECTORY):
25
path = BUILD_DIRECTORY + "/" + item
26
if IsFile(path):
27
DeleteFile(path)
28
continue
29
DeleteDirectory(path)
30
31
def PostSortHelper(Post):
32
return datetime.strptime(Post["date"], "%Y %b %d")
33
34
def GetBlogList():
35
PostSlugs = ListDirectory("blog-posts")
36
Posts = []
37
for slug in PostSlugs:
38
with open("blog-posts/" + slug, encoding="utf-8") as MDFile:
39
PostHTML = RenderMarkdown(MDFile.read())
40
Item = PostHTML.metadata
41
Item["content"] = PostHTML
42
Item["pathname"] = slug.replace(".md", ".html")
43
Posts.append(Item)
44
PostsByDate = sorted(Posts, key=PostSortHelper, reverse=True)
45
return PostsByDate
46
47
PostList = GetBlogList()
48
49
def RenderPosts():
50
for post in ListDirectory("blog-posts"):
51
path = "blog-posts/" + post
52
RenderedHTML: str
53
with open(path, "r", encoding="utf-8") as PostContent:
54
PostHTML = RenderMarkdown(PostContent.read())
55
Title = PostHTML.metadata["title"]
56
PostDate = PostHTML.metadata["date"]
57
RenderedHTML = RenderTemplate("blog-post.html", Title=Title, PostDate=PostDate, Content=PostHTML)
58
with open(BUILD_DIRECTORY + "/blog/" + post.replace(".md", ".html"), "w", encoding="utf-8") as PostLocation:
59
PostLocation.write(RenderedHTML)
60
61
def RenderPage(PageInput: str, ContentDest: str, **kwargs):
62
with open(BUILD_DIRECTORY + "/" + ContentDest, "w", encoding="utf-8") as DestLocation:
63
DestLocation.write(RenderTemplate(PageInput, **kwargs))
64
65
if __name__ == "__main__":
66
print("Wiping directory")
67
WipeFinalDir()
68
print("Creating blog holder")
69
CreateDirectory(BUILD_DIRECTORY + "/blog")
70
print("Rendering posts")
71
RenderPosts()
72
print("Copying static directory")
73
CopyDirectory("static", BUILD_DIRECTORY)
74
75
print("Building pages")
76
for file, path in PAGES.items():
77
RenderPage(file, path, PostList=PostList)
78
79
pass