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 7from datetime import datetime 8 9GITHUB_BUILD_DIR = "docs" # Separate because this site is built with an action that won't work if they aren't 10LOCAL_BUILD_DIR = "build" 11 12BUILD_DIRECTORY = GITHUB_BUILD_DIR if len(argv) > 1 and argv[1] == "gh-pages-deploy" else LOCAL_BUILD_DIR 13 14PAGES = { 15"index.html": "index.html", 16"blog-list.html": "blog/index.html", 17"blog-feed.rss": "blog/feed.rss", 18"link-tree.html": "link-tree.html", 19"404.html": "404.html" 20} 21 22def WipeFinalDir(): 23if not PathExists(BUILD_DIRECTORY): 24print("Directory didn't existing, creating it...") 25CreateDirectory(BUILD_DIRECTORY) 26return 27print("Directory exists, wiping it...") 28for item in ListDirectory(BUILD_DIRECTORY): 29path = BUILD_DIRECTORY + "/" + item 30if IsFile(path): 31DeleteFile(path) 32continue 33DeleteDirectory(path) 34 35def PostSortHelper(Post): 36return datetime.strptime(Post["date"], "%Y %b %d") 37 38def GetBlogList(): 39print("Grabbing post list") 40PostSlugs = ListDirectory("blog-posts") 41Posts = [] 42for slug in PostSlugs: 43print("Grabbing post list blog-posts/%s" % (slug)) 44with open("blog-posts/" + slug, encoding="utf-8") as MDFile: 45PostHTML = RenderMarkdown(MDFile.read()) 46Item = PostHTML.metadata 47Item["content"] = PostHTML 48Item["pathname"] = slug.replace(".md", ".html") 49Posts.append(Item) 50PostsByDate = sorted(Posts, key=PostSortHelper, reverse=True) 51return PostsByDate 52 53PostList = GetBlogList() 54 55def RenderPosts(): 56for post in ListDirectory("blog-posts"): 57path = "blog-posts/" + post 58RenderedHTML: str 59PostMD: str 60PostPath = post.replace(".md", ".html") 61PlaintextPath = post.replace(".md", ".txt") 62with open(path, "r", encoding="utf-8") as PostContent: 63PostMD = PostContent.read() 64PostHTML = RenderMarkdown(PostMD) 65Title = PostHTML.metadata["title"] 66PostDate = PostHTML.metadata["date"] 67RenderedHTML = RenderTemplate("blog-post.html", Title=Title, PostDate=PostDate, Content=PostHTML, PostPath=PostPath, PlaintextPath=PlaintextPath) 68print("Building blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PostPath)) 69with open(BUILD_DIRECTORY + "/blog/" + PostPath, "w", encoding="utf-8") as PostHTMLFile: 70PostHTMLFile.write(RenderedHTML) 71print("Copying blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PlaintextPath)) 72with open(BUILD_DIRECTORY + "/blog/" + PlaintextPath, "w", encoding="utf-8") as PostPlaintext: 73PostPlaintext.write(PostMD) 74 75def RenderPage(PageInput: str, ContentDest: str, **kwargs): 76print("Building views/%s to %s/%s" % (PageInput, BUILD_DIRECTORY, ContentDest)) 77with open(BUILD_DIRECTORY + "/" + ContentDest, "w", encoding="utf-8") as DestLocation: 78DestLocation.write(RenderTemplate(PageInput, **kwargs)) 79 80if __name__ == "__main__": 81print("Wiping directory") 82WipeFinalDir() 83print("Creating blog holder") 84CreateDirectory(BUILD_DIRECTORY + "/blog") 85print("Rendering posts") 86RenderPosts() 87print("Copying static directory") 88CopyDirectory("static", BUILD_DIRECTORY) 89 90print("Building pages") 91for file, path in PAGES.items(): 92RenderPage(file, path, PostList=PostList) 93 94pass