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"language": "en-US", 103"favicon": "https://steve0greatness.github.io/favicon.ico", 104"description": "A blog by a human being.", 105"authors": [ 106{ 107"name": "Steve0Greatness", 108"url": "https://steve0greatness.github.io" 109} 110], 111"items": [] 112} 113for post in PostList: 114CreatedJSON["items"].append({ 115"id": "https://steve0greatness.github.io/blog/" + post["pathname"], 116"title": "JSON Feed version 1.1", 117"content_html": post["content"], 118"date_published": post["date"], 119"url": "https://steve0greatness.github.io/blog/" + post["pathname"] 120}) 121with open(BUILD_DIRECTORY + "/blog/feed.json", "w") as JSONFeedFile: 122DumpJSON(CreatedJSON, JSONFeedFile) 123 124if __name__ == "__main__": 125print("Wiping directory") 126WipeFinalDir() 127print("Creating blog holder") 128CreateDirectory(BUILD_DIRECTORY + "/blog") 129print("Rendering posts") 130RenderPosts() 131CreateJSONFeed() 132print("Copying static directory") 133CopyDirectory("static", BUILD_DIRECTORY) 134 135print("Building pages") 136for file, path in PAGES.items(): 137if file in DISALLOWED_SITEMAP: 138RenderPage(file, path, False, PostList=PostList) 139continue 140RenderPage(file, path, PostList=PostList) 141 142with open(BUILD_DIRECTORY + "/sitemap.txt", "w") as SitemapFile: 143SitemapFile.write("\n".join(sitemap)) 144 145pass