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