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 22DISALLOWED_SITEMAP = [ 23"404.html", 24"blog-feed.rss" 25] 26 27SITEMAP_HREF = "https://steve0greatness.github.io/" 28sitemap = [] 29 30def WipeFinalDir(): 31if not PathExists(BUILD_DIRECTORY): 32print("Directory didn't existing, creating it...") 33CreateDirectory(BUILD_DIRECTORY) 34return 35print("Directory exists, wiping it...") 36for item in ListDirectory(BUILD_DIRECTORY): 37path = BUILD_DIRECTORY + "/" + item 38if IsFile(path): 39DeleteFile(path) 40continue 41DeleteDirectory(path) 42 43def PostSortHelper(Post): 44return datetime.strptime(Post["date"], "%Y %b %d") 45 46def GetBlogList(): 47print("Grabbing post list") 48PostSlugs = ListDirectory("blog-posts") 49Posts = [] 50for slug in PostSlugs: 51print("Grabbing post list blog-posts/%s" % (slug)) 52with open("blog-posts/" + slug, encoding="utf-8") as MDFile: 53PostHTML = RenderMarkdown(MDFile.read()) 54Item = PostHTML.metadata 55Item["content"] = PostHTML 56Item["pathname"] = slug.replace(".md", ".html") 57Posts.append(Item) 58PostsByDate = sorted(Posts, key=PostSortHelper, reverse=True) 59return PostsByDate 60 61PostList = GetBlogList() 62 63def RenderPosts(): 64for post in ListDirectory("blog-posts"): 65path = "blog-posts/" + post 66RenderedHTML: str 67PostMD: str 68PostPath = post.replace(".md", ".html") 69PlaintextPath = post.replace(".md", ".txt") 70with open(path, "r", encoding="utf-8") as PostContent: 71PostMD = PostContent.read() 72PostHTML = RenderMarkdown(PostMD) 73Title = PostHTML.metadata["title"] 74PostDate = PostHTML.metadata["date"] 75RenderedHTML = RenderTemplate("blog-post.html", Title=Title, PostDate=PostDate, Content=PostHTML, PostPath=PostPath, PlaintextPath=PlaintextPath) 76print("Building blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PostPath)) 77with open(BUILD_DIRECTORY + "/blog/" + PostPath, "w", encoding="utf-8") as PostHTMLFile: 78PostHTMLFile.write(RenderedHTML) 79print("Copying blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PlaintextPath)) 80with open(BUILD_DIRECTORY + "/blog/" + PlaintextPath, "w", encoding="utf-8") as PostPlaintext: 81PostPlaintext.write(PostMD) 82sitemap.append(SITEMAP_HREF + "/blog/" + PostPath) 83 84def RenderPage(PageInput: str, ContentDest: str, AllowSitemap: bool = True, **kwargs): 85print("Building views/%s to %s/%s" % (PageInput, BUILD_DIRECTORY, ContentDest)) 86if AllowSitemap: 87sitemap.append(SITEMAP_HREF + ContentDest) 88with open(BUILD_DIRECTORY + "/" + ContentDest, "w", encoding="utf-8") as DestLocation: 89DestLocation.write(RenderTemplate(PageInput, **kwargs)) 90 91if __name__ == "__main__": 92print("Wiping directory") 93WipeFinalDir() 94print("Creating blog holder") 95CreateDirectory(BUILD_DIRECTORY + "/blog") 96print("Rendering posts") 97RenderPosts() 98print("Copying static directory") 99CopyDirectory("static", BUILD_DIRECTORY) 100 101print("Building pages") 102for file, path in PAGES.items(): 103if file in DISALLOWED_SITEMAP: 104RenderPage(file, path, False, PostList=PostList) 105continue 106RenderPage(file, path, PostList=PostList) 107 108with open(BUILD_DIRECTORY + "/sitemap.txt", "w") as SitemapFile: 109SitemapFile.write("\n".join(sitemap)) 110 111pass