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