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