build.py
Python script, ASCII text executable
1
from Renderers import RenderTemplate, RenderMarkdown
2
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
8
BUILD_DIRECTORY = "docs"
9
10
def WipeFinalDir():
11
if not PathExists(BUILD_DIRECTORY):
12
CreateDirectory(BUILD_DIRECTORY)
13
for item in ListDirectory(BUILD_DIRECTORY):
14
path = BUILD_DIRECTORY + "/" + item
15
if IsFile(path):
16
DeleteFile(path)
17
continue
18
DeleteDirectory(path)
19
20
def GetBlogList():
21
PostSlugs = ListDirectory("blog-posts")
22
Posts = []
23
for slug in PostSlugs:
24
with open("blog-posts/" + slug) as MDFile:
25
PostHTML = RenderMarkdown(MDFile.read())
26
Item = PostHTML.metadata
27
Item["content"] = PostHTML
28
Item["pathname"] = slug.replace(".md", ".html")
29
Posts.append(Item)
30
return Posts
31
32
PostList = GetBlogList()
33
34
def RenderPosts():
35
for post in ListDirectory("blog-posts"):
36
path = "blog-posts/" + post
37
RenderedHTML: str
38
with open(path, "r") as PostContent:
39
PostHTML = RenderMarkdown(PostContent.read())
40
Title = PostHTML.metadata["title"]
41
PostDate = PostHTML.metadata["date"]
42
RenderedHTML = RenderTemplate("blog-post.html", Title=Title, PostDate=PostDate, Content=PostHTML)
43
with open(BUILD_DIRECTORY + "/blog/" + post.replace(".md", ".html"), "w") as PostLocation:
44
PostLocation.write(RenderedHTML)
45
46
def RenderPage(PageInput: str, ContentDest: str, **kwargs):
47
with open(BUILD_DIRECTORY + "/" + ContentDest, "w") as DestLocation:
48
DestLocation.write(RenderTemplate(PageInput, **kwargs))
49
50
if __name__ == "__main__":
51
print("Wiping directory")
52
WipeFinalDir()
53
print("Creating blog holder")
54
CreateDirectory(BUILD_DIRECTORY + "/blog")
55
print("Rendering posts...")
56
RenderPosts()
57
print("Copying static directory")
58
CopyDirectory("static", BUILD_DIRECTORY + "/static")
59
60
print("Building static files")
61
RenderPage("index.html", "index.html", PostList=PostList)
62
RenderPage("blog-list.html", "/blog/index.html", PostList=PostList)
63
RenderPage("blog-feed.rss", "/blog/feed.rss", PostList=PostList)
64
RenderPage("404.html", "/404.html")
65
66
pass