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