build.py
Python script, ASCII text executable
1from Renderers import RenderTemplate, RenderMarkdown 2from sys import argv 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, exists as PathExists 6from distutils.dir_util import copy_tree as CopyDirectory 7 8GITHUB_BUILD_DIR = "docs" # Separate because this site is built with an action that won't work if they aren't 9LOCAL_BUILD_DIR = "build" 10 11BUILD_DIRECTORY = GITHUB_BUILD_DIR if len(argv) > 1 and argv[1] == "gh-pages-deploy" else LOCAL_BUILD_DIR 12 13PAGES = { 14"index.html": "index.html", 15"blog-list.html": "blog/index.html", 16"blog-feed.rss": "blog/feed.rss", 17"404.html": "404.html" 18} 19 20def WipeFinalDir(): 21if not PathExists(BUILD_DIRECTORY): 22CreateDirectory(BUILD_DIRECTORY) 23for item in ListDirectory(BUILD_DIRECTORY): 24path = BUILD_DIRECTORY + "/" + item 25if IsFile(path): 26DeleteFile(path) 27continue 28DeleteDirectory(path) 29 30def GetBlogList(): 31PostSlugs = ListDirectory("blog-posts") 32Posts = [] 33for slug in PostSlugs: 34with open("blog-posts/" + slug) as MDFile: 35PostHTML = RenderMarkdown(MDFile.read()) 36Item = PostHTML.metadata 37Item["content"] = PostHTML 38Item["pathname"] = slug.replace(".md", ".html") 39Posts.append(Item) 40return Posts 41 42PostList = GetBlogList() 43 44def RenderPosts(): 45for post in ListDirectory("blog-posts"): 46path = "blog-posts/" + post 47RenderedHTML: str 48with open(path, "r") as PostContent: 49PostHTML = RenderMarkdown(PostContent.read()) 50Title = PostHTML.metadata["title"] 51PostDate = PostHTML.metadata["date"] 52RenderedHTML = RenderTemplate("blog-post.html", Title=Title, PostDate=PostDate, Content=PostHTML) 53with open(BUILD_DIRECTORY + "/blog/" + post.replace(".md", ".html"), "w") as PostLocation: 54PostLocation.write(RenderedHTML) 55 56def RenderPage(PageInput: str, ContentDest: str, **kwargs): 57with open(BUILD_DIRECTORY + "/" + ContentDest, "w") as DestLocation: 58DestLocation.write(RenderTemplate(PageInput, **kwargs)) 59 60if __name__ == "__main__": 61print("Wiping directory") 62WipeFinalDir() 63print("Creating blog holder") 64CreateDirectory(BUILD_DIRECTORY + "/blog") 65print("Rendering posts") 66RenderPosts() 67print("Copying static directory") 68CopyDirectory("static", BUILD_DIRECTORY) 69 70print("Building pages") 71for file, path in PAGES.items(): 72RenderPage(file, path, PostList=PostList) 73 74pass