Index

joshstock.in / 6adfcc4

Source for serving and static templating/compiling of https://joshstock.in.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
911 Nov 2019 18:534cf1cb9Fix relative file pathsJosh Stockin120N

Blob @ joshstock.in / compile.py

application/x-python3690 bytesdownload raw
1#!/usr/bin/env python3
2
3import sys
4import os
5import json
6
7os.chdir(sys.path[0])
8
9def readfile(filename):
10 try:
11 with open(filename, "r") as file:
12 s = file.read()
13 file.close()
14 return s
15 except FileNotFoundError:
16 print(filename + " not found. exiting")
17 exit(1)
18 except:
19 print("can't open " + filename + " for reading. exiting")
20 exit(1)
21
22def writefile(filename, text):
23 try:
24 with open(filename, "w") as file:
25 file.write(text)
26 file.close()
27 except:
28 print("can't open " + filename + " for writing. exiting")
29 exit(1)
30
31routemaps = {}
32def routemap(route, file):
33 routemaps[route] = file
34
35def main():
36 config = json.loads(readfile("config.json"))
37
38 # Index
39 print("creating landing")
40 landing = readfile(config["templates"]["landing"])
41 file = os.path.join(out_path, "index.html")
42 writefile(file, landing)
43 routemap("/", file)
44
45 # Privacy
46 print("creating privacy policy page")
47 privacy = readfile(config["templates"]["privacy"])
48 file = os.path.join(out_path, "privacy.html")
49 writefile(file, privacy)
50 routemap("/privacy", file)
51
52 # /blog*
53 print("creating blog articles")
54 listings = ""
55 article_listing_template = readfile(config["templates"]["blog-archive-listing"])
56 article_template = readfile(config["templates"]["blog-article"])
57 blog_css = readfile(config["templates"]["blog-css"])
58
59 for article in config["articles"]:
60 # Create article
61 print("creating article \"" + article["title"] + "\"")
62 articlehtml = "" + article_template
63 articlehtml = articlehtml.replace("$css", blog_css)
64 articlehtml = articlehtml.replace("$title", article["title"])
65 articlehtml = articlehtml.replace("$date", article["date"])
66 articlehtml = articlehtml.replace("$banner", article["banner"])
67 articlehtml = articlehtml.replace("$content", readfile(article["content"]))
68 articlehtml = articlehtml.replace("$summary", article["summary"])
69 articlehtml = articlehtml.replace("$copyright", config["copyright"])
70 file = os.path.join(out_path, "blog-"+article["title"].lower().replace(" ", "-")+".html")
71 path = "/blog/"+article["title"].lower().replace(" ", "-")
72 articlehtml = articlehtml.replace("$permalink", path)
73 writefile(file, articlehtml)
74 routemap(path, file)
75
76 # Update archive listings
77 listinghtml = "" + article_listing_template
78 listinghtml = listinghtml.replace("$title", article["title"])
79 listinghtml = listinghtml.replace("$date", article["date"])
80 listinghtml = listinghtml.replace("$banner", article["banner"])
81 listinghtml = listinghtml.replace("$summary", article["summary"])
82 listinghtml = listinghtml.replace("$permalink", path)
83 listings = listinghtml + listings
84
85 # Blog archive
86 print("creating blog archive")
87 archive_template = readfile(config["templates"]["blog-archive"])
88 archive_template = archive_template.replace("$css", blog_css)
89 archive_template = archive_template.replace("$articles", listings)
90 archive_template = archive_template.replace("$copyright", config["copyright"])
91 file = os.path.join(out_path, "blog.html")
92 writefile(file, archive_template)
93 routemap("/blog", file)
94
95 # Error 404
96 print("creating 404 error page")
97 e404 = readfile(config["templates"]["404"])
98 file = os.path.join(out_path, "error-404.html")
99 writefile(file, e404)
100
101 # Routemap config
102 print("Routemap:")
103 for map in routemaps:
104 print(map)
105
106if __name__ == "__main__":
107 if len(sys.argv) < 2:
108 print("usage: compile.py <folder>")
109 exit(1)
110 folder_out = sys.argv[1]
111 print("compile.py starting")
112 if not os.path.isdir(folder_out):
113 print(folder_out + " is not a valid folder location. exiting")
114 exit(1)
115 out_path = os.path.abspath(folder_out)
116 print("output set to " + out_path)
117 print("beginning main routine")
118 main()
119 print("finished")
120