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