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.") 25CreateDirectory(BUILD_DIRECTORY) 26for item in ListDirectory(BUILD_DIRECTORY): 27path = BUILD_DIRECTORY + "/" + item 28if IsFile(path): 29DeleteFile(path) 30continue 31DeleteDirectory(path) 32 33def PostSortHelper(Post): 34return datetime.strptime(Post["date"], "%Y %b %d") 35 36def GetBlogList(): 37print("Grabbing post list") 38PostSlugs = ListDirectory("blog-posts") 39Posts = [] 40for slug in PostSlugs: 41print("Grabbing post list blog-posts/%s" % (slug)) 42with open("blog-posts/" + slug, encoding="utf-8") as MDFile: 43PostHTML = RenderMarkdown(MDFile.read()) 44Item = PostHTML.metadata 45Item["content"] = PostHTML 46Item["pathname"] = slug.replace(".md", ".html") 47Posts.append(Item) 48PostsByDate = sorted(Posts, key=PostSortHelper, reverse=True) 49return PostsByDate 50 51PostList = GetBlogList() 52 53def RenderPosts(): 54for post in ListDirectory("blog-posts"): 55path = "blog-posts/" + post 56RenderedHTML: str 57PostMD: str 58PostPath = post.replace(".md", ".html") 59PlaintextPath = post.replace(".md", ".txt") 60with open(path, "r", encoding="utf-8") as PostContent: 61PostMD = PostContent.read() 62PostHTML = RenderMarkdown(PostMD) 63Title = PostHTML.metadata["title"] 64PostDate = PostHTML.metadata["date"] 65RenderedHTML = RenderTemplate("blog-post.html", Title=Title, PostDate=PostDate, Content=PostHTML, PostPath=PostPath, PlaintextPath=PlaintextPath) 66print("Building blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PostPath)) 67with open(BUILD_DIRECTORY + "/blog/" + PostPath, "w", encoding="utf-8") as PostHTMLFile: 68PostHTMLFile.write(RenderedHTML) 69print("Copying blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PlaintextPath)) 70with open(BUILD_DIRECTORY + "/blog/" + PlaintextPath, "w", encoding="utf-8") as PostPlaintext: 71PostPlaintext.write(PostMD) 72 73def RenderPage(PageInput: str, ContentDest: str, **kwargs): 74print("Building views/%s to %s/%s" % (PageInput, BUILD_DIRECTORY, ContentDest)) 75with open(BUILD_DIRECTORY + "/" + ContentDest, "w", encoding="utf-8") as DestLocation: 76DestLocation.write(RenderTemplate(PageInput, **kwargs)) 77 78if __name__ == "__main__": 79print("Wiping directory") 80WipeFinalDir() 81print("Creating blog holder") 82CreateDirectory(BUILD_DIRECTORY + "/blog") 83print("Rendering posts") 84RenderPosts() 85print("Copying static directory") 86CopyDirectory("static", BUILD_DIRECTORY) 87 88print("Building pages") 89for file, path in PAGES.items(): 90RenderPage(file, path, PostList=PostList) 91 92pass