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"] 75Revised = False 76if "updated" in PostHTML.metadata: 77Revised = PostHTML.metadata["updated"] 78RenderedHTML = RenderTemplate("blog-post.html", Revised=Revised, Title=Title, PostDate=PostDate, Content=PostHTML, PostPath=PostPath, PlaintextPath=PlaintextPath) 79print("Building blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PostPath)) 80with open(BUILD_DIRECTORY + "/blog/" + PostPath, "w", encoding="utf-8") as PostHTMLFile: 81PostHTMLFile.write(RenderedHTML) 82print("Copying blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PlaintextPath)) 83with open(BUILD_DIRECTORY + "/blog/" + PlaintextPath, "w", encoding="utf-8") as PostPlaintext: 84PostPlaintext.write(PostMD) 85sitemap.append(SITEMAP_HREF + "/blog/" + PostPath) 86 87def RenderPage(PageInput: str, ContentDest: str, AllowSitemap: bool = True, **kwargs): 88print("Building views/%s to %s/%s" % (PageInput, BUILD_DIRECTORY, ContentDest)) 89if AllowSitemap: 90sitemap.append(SITEMAP_HREF + ContentDest) 91with open(BUILD_DIRECTORY + "/" + ContentDest, "w", encoding="utf-8") as DestLocation: 92DestLocation.write(RenderTemplate(PageInput, **kwargs)) 93 94if __name__ == "__main__": 95print("Wiping directory") 96WipeFinalDir() 97print("Creating blog holder") 98CreateDirectory(BUILD_DIRECTORY + "/blog") 99print("Rendering posts") 100RenderPosts() 101print("Copying static directory") 102CopyDirectory("static", BUILD_DIRECTORY) 103 104print("Building pages") 105for file, path in PAGES.items(): 106if file in DISALLOWED_SITEMAP: 107RenderPage(file, path, False, PostList=PostList) 108continue 109RenderPage(file, path, PostList=PostList) 110 111with open(BUILD_DIRECTORY + "/sitemap.txt", "w") as SitemapFile: 112SitemapFile.write("\n".join(sitemap)) 113 114pass