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.09 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
"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
"items": []
103
}
104
for post in PostList:
105
CreatedJSON["items"].append({
106
"id": "https://steve0greatness.github.io/blog/" + post["pathname"],
107
"title": "JSON Feed version 1.1",
108
"content_html": post["content"],
109
"date_published": post["date"],
110
"url": "https://steve0greatness.github.io/blog/" + post["pathname"]
111
})
112
with open("build/blog/feed.json", "w") as JSONFeedFile:
113
DumpJSON(CreatedJSON, JSONFeedFile)
114
115
if __name__ == "__main__":
116
print("Wiping directory")
117
WipeFinalDir()
118
print("Creating blog holder")
119
CreateDirectory(BUILD_DIRECTORY + "/blog")
120
print("Rendering posts")
121
RenderPosts()
122
CreateJSONFeed()
123
print("Copying static directory")
124
CopyDirectory("static", BUILD_DIRECTORY)
125
126
print("Building pages")
127
for file, path in PAGES.items():
128
if file in DISALLOWED_SITEMAP:
129
RenderPage(file, path, False, PostList=PostList)
130
continue
131
RenderPage(file, path, PostList=PostList)
132
133
with open(BUILD_DIRECTORY + "/sitemap.txt", "w") as SitemapFile:
134
SitemapFile.write("\n".join(sitemap))
135
136
pass