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