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