build.py
Python script, ASCII text executable
1from Renderers import RenderTemplate, RenderMarkdown 2 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 7 8BUILD_DIRECTORY = "docs" 9 10def WipeFinalDir(): 11if not PathExists(BUILD_DIRECTORY): 12CreateDirectory(BUILD_DIRECTORY) 13for item in ListDirectory(BUILD_DIRECTORY): 14path = BUILD_DIRECTORY + "/" + item 15if IsFile(path): 16DeleteFile(path) 17continue 18DeleteDirectory(path) 19 20def GetBlogList(): 21PostSlugs = ListDirectory("blog-posts") 22Posts = [] 23for slug in PostSlugs: 24with open("blog-posts/" + slug) as MDFile: 25PostHTML = RenderMarkdown(MDFile.read()) 26Item = PostHTML.metadata 27Item["content"] = PostHTML 28Item["pathname"] = slug.replace(".md", ".html") 29Posts.append(Item) 30return Posts 31 32PostList = GetBlogList() 33 34def RenderPosts(): 35for post in ListDirectory("blog-posts"): 36path = "blog-posts/" + post 37RenderedHTML: str 38with open(path, "r") as PostContent: 39PostHTML = RenderMarkdown(PostContent.read()) 40Title = PostHTML.metadata["title"] 41PostDate = PostHTML.metadata["date"] 42RenderedHTML = RenderTemplate("blog-post.html", Title=Title, PostDate=PostDate, Content=PostHTML) 43with open(BUILD_DIRECTORY + "/blog/" + post.replace(".md", ".html"), "w") as PostLocation: 44PostLocation.write(RenderedHTML) 45 46def RenderPage(PageInput: str, ContentDest: str, **kwargs): 47with open(BUILD_DIRECTORY + "/" + ContentDest, "w") as DestLocation: 48DestLocation.write(RenderTemplate(PageInput, **kwargs)) 49 50if __name__ == "__main__": 51print("Wiping directory") 52WipeFinalDir() 53print("Creating blog holder") 54CreateDirectory(BUILD_DIRECTORY + "/blog") 55print("Rendering posts...") 56RenderPosts() 57print("Copying static directory") 58CopyDirectory("static", BUILD_DIRECTORY + "/static") 59 60print("Building static files") 61RenderPage("index.html", "index.html", PostList=PostList) 62RenderPage("blog-list.html", "/blog/index.html", PostList=PostList) 63RenderPage("blog-feed.rss", "/blog/feed.rss", PostList=PostList) 64RenderPage("404.html", "/404.html") 65 66pass