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.28 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
Revised = False
76
if "updated" in PostHTML.metadata:
77
Revised = PostHTML.metadata["updated"]
78
RenderedHTML = RenderTemplate("blog-post.html", Revised=Revised, Title=Title, PostDate=PostDate, Content=PostHTML, PostPath=PostPath, PlaintextPath=PlaintextPath)
79
print("Building blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PostPath))
80
with open(BUILD_DIRECTORY + "/blog/" + PostPath, "w", encoding="utf-8") as PostHTMLFile:
81
PostHTMLFile.write(RenderedHTML)
82
print("Copying blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PlaintextPath))
83
with open(BUILD_DIRECTORY + "/blog/" + PlaintextPath, "w", encoding="utf-8") as PostPlaintext:
84
PostPlaintext.write(PostMD)
85
sitemap.append(SITEMAP_HREF + "/blog/" + PostPath)
86
87
def RenderPage(PageInput: str, ContentDest: str, AllowSitemap: bool = True, **kwargs):
88
print("Building views/%s to %s/%s" % (PageInput, BUILD_DIRECTORY, ContentDest))
89
if AllowSitemap:
90
sitemap.append(SITEMAP_HREF + ContentDest)
91
with open(BUILD_DIRECTORY + "/" + ContentDest, "w", encoding="utf-8") as DestLocation:
92
DestLocation.write(RenderTemplate(PageInput, **kwargs))
93
94
if __name__ == "__main__":
95
print("Wiping directory")
96
WipeFinalDir()
97
print("Creating blog holder")
98
CreateDirectory(BUILD_DIRECTORY + "/blog")
99
print("Rendering posts")
100
RenderPosts()
101
print("Copying static directory")
102
CopyDirectory("static", BUILD_DIRECTORY)
103
104
print("Building pages")
105
for file, path in PAGES.items():
106
if file in DISALLOWED_SITEMAP:
107
RenderPage(file, path, False, PostList=PostList)
108
continue
109
RenderPage(file, path, PostList=PostList)
110
111
with open(BUILD_DIRECTORY + "/sitemap.txt", "w") as SitemapFile:
112
SitemapFile.write("\n".join(sitemap))
113
114
pass