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