build.py
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
from json import dump as DumpJSON
9
10
GITHUB_BUILD_DIR = "docs" # Separate because this site is built with an action that won't work if they aren't
11
LOCAL_BUILD_DIR = "build"
12
13
BUILD_DIRECTORY = GITHUB_BUILD_DIR if len(argv) > 1 and argv[1] == "gh-pages-deploy" else LOCAL_BUILD_DIR
14
15
PAGES = {
16
"index.html": "index.html",
17
"blog-list.html": "blog/index.html",
18
"blog-feed.rss": "blog/feed.rss",
19
"blog-feed.atom": "blog/feed.atom",
20
"link-tree.html": "link-tree.html",
21
"404.html": "404.html"
22
}
23
24
DISALLOWED_SITEMAP = [
25
"404.html",
26
"blog-feed.rss"
27
]
28
29
SITEMAP_HREF = "https://steve0greatness.github.io/"
30
sitemap = []
31
32
def WipeFinalDir():
33
if not PathExists(BUILD_DIRECTORY):
34
print("Directory didn't existing, creating it...")
35
CreateDirectory(BUILD_DIRECTORY)
36
return
37
print("Directory exists, wiping it...")
38
for item in ListDirectory(BUILD_DIRECTORY):
39
path = BUILD_DIRECTORY + "/" + item
40
if IsFile(path):
41
DeleteFile(path)
42
continue
43
DeleteDirectory(path)
44
45
def PostSortHelper(Post):
46
return datetime.strptime(Post["date"], "%Y %b %d")
47
48
def GetBlogList():
49
print("Grabbing post list")
50
PostSlugs = ListDirectory("blog-posts")
51
Posts = []
52
for slug in PostSlugs:
53
print("Grabbing post list blog-posts/%s" % (slug))
54
with open("blog-posts/" + slug, encoding="utf-8") as MDFile:
55
PostHTML = RenderMarkdown(MDFile.read())
56
Item = PostHTML.metadata
57
Item["content"] = PostHTML
58
Item["pathname"] = slug.replace(".md", ".html")
59
Posts.append(Item)
60
PostsByDate = sorted(Posts, key=PostSortHelper, reverse=True)
61
return PostsByDate
62
63
PostList = GetBlogList()
64
65
def RenderPosts():
66
for post in ListDirectory("blog-posts"):
67
path = "blog-posts/" + post
68
RenderedHTML: str
69
PostMD: str
70
PostPath = post.replace(".md", ".html")
71
PlaintextPath = post.replace(".md", ".txt")
72
with open(path, "r", encoding="utf-8") as PostContent:
73
PostMD = PostContent.read()
74
PostHTML = RenderMarkdown(PostMD)
75
Title = PostHTML.metadata["title"]
76
PostDate = PostHTML.metadata["date"]
77
Revised = False
78
if "updated" in PostHTML.metadata:
79
Revised = PostHTML.metadata["updated"]
80
RenderedHTML = RenderTemplate("blog-post.html", Revised=Revised, Title=Title, PostDate=PostDate, Content=PostHTML, PostPath=PostPath, PlaintextPath=PlaintextPath)
81
print("Building blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PostPath))
82
with open(BUILD_DIRECTORY + "/blog/" + PostPath, "w", encoding="utf-8") as PostHTMLFile:
83
PostHTMLFile.write(RenderedHTML)
84
print("Copying blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PlaintextPath))
85
with open(BUILD_DIRECTORY + "/blog/" + PlaintextPath, "w", encoding="utf-8") as PostPlaintext:
86
PostPlaintext.write(PostMD)
87
sitemap.append(SITEMAP_HREF + "/blog/" + PostPath)
88
89
def RenderPage(PageInput: str, ContentDest: str, AllowSitemap: bool = True, **kwargs):
90
print("Building views/%s to %s/%s" % (PageInput, BUILD_DIRECTORY, ContentDest))
91
if AllowSitemap:
92
sitemap.append(SITEMAP_HREF + ContentDest)
93
with open(BUILD_DIRECTORY + "/" + ContentDest, "w", encoding="utf-8") as DestLocation:
94
DestLocation.write(RenderTemplate(PageInput, **kwargs))
95
96
def CreateJSONFeed():
97
CreatedJSON = {
98
"version": "https://jsonfeed.org/version/1",
99
"title": "Steve0Greatness' Blog",
100
"home_page_url": "https://steve0greatness.github.io",
101
"feed_url": "https://steve0greatness.github.io/blog/feed.rss",
102
"language": "en-US",
103
"favicon": "https://steve0greatness.github.io/favicon.ico",
104
"description": "A blog by a human being.",
105
"authors": [
106
{
107
"name": "Steve0Greatness",
108
"url": "https://steve0greatness.github.io"
109
}
110
],
111
"items": []
112
}
113
for post in PostList:
114
CreatedJSON["items"].append({
115
"id": "https://steve0greatness.github.io/blog/" + post["pathname"],
116
"title": "JSON Feed version 1.1",
117
"content_html": post["content"],
118
"date_published": post["date"],
119
"url": "https://steve0greatness.github.io/blog/" + post["pathname"]
120
})
121
with open(BUILD_DIRECTORY + "/blog/feed.json", "w") as JSONFeedFile:
122
DumpJSON(CreatedJSON, JSONFeedFile)
123
124
if __name__ == "__main__":
125
print("Wiping directory")
126
WipeFinalDir()
127
print("Creating blog holder")
128
CreateDirectory(BUILD_DIRECTORY + "/blog")
129
print("Rendering posts")
130
RenderPosts()
131
CreateJSONFeed()
132
print("Copying static directory")
133
CopyDirectory("static", BUILD_DIRECTORY)
134
135
print("Building pages")
136
for file, path in PAGES.items():
137
if file in DISALLOWED_SITEMAP:
138
RenderPage(file, path, False, PostList=PostList)
139
continue
140
RenderPage(file, path, PostList=PostList)
141
142
with open(BUILD_DIRECTORY + "/sitemap.txt", "w") as SitemapFile:
143
SitemapFile.write("\n".join(sitemap))
144
145
pass