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 8from json import dump as DumpJSON 9 10GITHUB_BUILD_DIR = "docs" # Separate because this site is built with an action that won't work if they aren't 11LOCAL_BUILD_DIR = "build" 12 13BUILD_DIRECTORY = GITHUB_BUILD_DIR if len(argv) > 1 and argv[1] == "gh-pages-deploy" else LOCAL_BUILD_DIR 14 15PAGES = { 16"index.html": "index.html", 17"blog-list.html": "blog/index.html", 18"blog-feed.rss": "blog/feed.rss", 19"blog-feed.atom": "blog/feed.atom", 20"link-tree.html": "link-tree.html", 21"404.html": "404.html" 22} 23 24DISALLOWED_SITEMAP = [ 25"404.html", 26"blog-feed.rss" 27] 28 29SITEMAP_HREF = "https://steve0greatness.github.io/" 30sitemap = [] 31 32def WipeFinalDir(): 33if not PathExists(BUILD_DIRECTORY): 34print("Directory didn't existing, creating it...") 35CreateDirectory(BUILD_DIRECTORY) 36return 37print("Directory exists, wiping it...") 38for item in ListDirectory(BUILD_DIRECTORY): 39path = BUILD_DIRECTORY + "/" + item 40if IsFile(path): 41DeleteFile(path) 42continue 43DeleteDirectory(path) 44 45def PostSortHelper(Post): 46return datetime.strptime(Post["date"], "%Y %b %d") 47 48def GetBlogList(): 49print("Grabbing post list") 50PostSlugs = ListDirectory("blog-posts") 51Posts = [] 52for slug in PostSlugs: 53print("Grabbing post list blog-posts/%s" % (slug)) 54with open("blog-posts/" + slug, encoding="utf-8") as MDFile: 55PostHTML = RenderMarkdown(MDFile.read()) 56Item = PostHTML.metadata 57Item["content"] = PostHTML 58Item["pathname"] = slug.replace(".md", ".html") 59Posts.append(Item) 60PostsByDate = sorted(Posts, key=PostSortHelper, reverse=True) 61return PostsByDate 62 63PostList = GetBlogList() 64 65def RenderPosts(): 66for post in ListDirectory("blog-posts"): 67path = "blog-posts/" + post 68RenderedHTML: str 69PostMD: str 70PostPath = post.replace(".md", ".html") 71PlaintextPath = post.replace(".md", ".txt") 72with open(path, "r", encoding="utf-8") as PostContent: 73PostMD = PostContent.read() 74PostHTML = RenderMarkdown(PostMD) 75Title = PostHTML.metadata["title"] 76PostDate = PostHTML.metadata["date"] 77Revised = False 78if "updated" in PostHTML.metadata: 79Revised = PostHTML.metadata["updated"] 80RenderedHTML = RenderTemplate("blog-post.html", Revised=Revised, Title=Title, PostDate=PostDate, Content=PostHTML, PostPath=PostPath, PlaintextPath=PlaintextPath) 81print("Building blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PostPath)) 82with open(BUILD_DIRECTORY + "/blog/" + PostPath, "w", encoding="utf-8") as PostHTMLFile: 83PostHTMLFile.write(RenderedHTML) 84print("Copying blog/%s to %s/blog/%s" % (post, BUILD_DIRECTORY, PlaintextPath)) 85with open(BUILD_DIRECTORY + "/blog/" + PlaintextPath, "w", encoding="utf-8") as PostPlaintext: 86PostPlaintext.write(PostMD) 87sitemap.append(SITEMAP_HREF + "/blog/" + PostPath) 88 89def RenderPage(PageInput: str, ContentDest: str, AllowSitemap: bool = True, **kwargs): 90print("Building views/%s to %s/%s" % (PageInput, BUILD_DIRECTORY, ContentDest)) 91if AllowSitemap: 92sitemap.append(SITEMAP_HREF + ContentDest) 93with open(BUILD_DIRECTORY + "/" + ContentDest, "w", encoding="utf-8") as DestLocation: 94DestLocation.write(RenderTemplate(PageInput, **kwargs)) 95 96def CreateJSONFeed(): 97CreatedJSON = { 98"version": "https://jsonfeed.org/version/1", 99"title": "Steve0Greatness' Blog", 100"home_page_url": "https://steve0greatness.github.io", 101"feed_url": "https://steve0greatness.github.io/blog/feed.rss", 102"items": [] 103} 104for post in PostList: 105CreatedJSON["items"].append({ 106"id": "https://steve0greatness.github.io/blog/" + post["pathname"], 107"title": "JSON Feed version 1.1", 108"content_html": post["content"], 109"date_published": post["date"], 110"url": "https://steve0greatness.github.io/blog/" + post["pathname"] 111}) 112with open("build/blog/feed.json", "w") as JSONFeedFile: 113DumpJSON(CreatedJSON, JSONFeedFile) 114 115if __name__ == "__main__": 116print("Wiping directory") 117WipeFinalDir() 118print("Creating blog holder") 119CreateDirectory(BUILD_DIRECTORY + "/blog") 120print("Rendering posts") 121RenderPosts() 122CreateJSONFeed() 123print("Copying static directory") 124CopyDirectory("static", BUILD_DIRECTORY) 125 126print("Building pages") 127for file, path in PAGES.items(): 128if file in DISALLOWED_SITEMAP: 129RenderPage(file, path, False, PostList=PostList) 130continue 131RenderPage(file, path, PostList=PostList) 132 133with open(BUILD_DIRECTORY + "/sitemap.txt", "w") as SitemapFile: 134SitemapFile.write("\n".join(sitemap)) 135 136pass