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