Index

joshstock.in / d000713

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
1616 Nov 2019 12:35d000713Clear working tree before remakingJosh Stockin1140N

Blob @ joshstock.in / compile.py

application/x-python4009 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
31def empty_dir(path):
32 for file in os.listdir(path):
33 filepath = os.path.join(path, file)
34 try:
35 if os.path.isfile(filepath):
36 os.unlink(filepath)
37 except Exception as e:
38 print("error while trying to empty working directory")
39 print(e)
40 exit(1)
41
42routemaps = {}
43def routemap(route, file):
44 routemaps[route] = file
45
46def main():
47 print("emptying working directory")
48 empty_dir(out_path)
49
50 config = json.loads(readfile("config.json"))
51
52 # Index
53 print("creating landing")
54 landing = readfile(config["templates"]["landing"])
55 file = os.path.join(out_path, "index.html")
56 writefile(file, landing)
57 routemap("/", file)
58
59 # Privacy
60 print("creating privacy policy page")
61 privacy = readfile(config["templates"]["privacy"])
62 file = os.path.join(out_path, "privacy.html")
63 writefile(file, privacy)
64 routemap("/privacy", file)
65
66 # /blog*
67 print("creating blog articles")
68 listings = ""
69 article_listing_template = readfile(config["templates"]["blog-archive-listing"])
70 article_template = readfile(config["templates"]["blog-article"])
71 blog_css = readfile(config["templates"]["blog-css"])
72
73 for article in config["articles"]:
74 # Create article
75 print("creating article \"" + article["title"] + "\"")
76 articlehtml = "" + article_template
77 articlehtml = articlehtml.replace("$css", blog_css)
78 articlehtml = articlehtml.replace("$title", article["title"])
79 articlehtml = articlehtml.replace("$date", article["date"])
80 articlehtml = articlehtml.replace("$banner", article["banner"])
81 articlehtml = articlehtml.replace("$content", readfile(article["content"]))
82 articlehtml = articlehtml.replace("$summary", article["summary"])
83 articlehtml = articlehtml.replace("$copyright", config["copyright"])
84 file = os.path.join(out_path, "blog-"+article["title"].lower().replace(" ", "-")+".html")
85 path = "/blog/"+article["title"].lower().replace(" ", "-")
86 articlehtml = articlehtml.replace("$permalink", path)
87 writefile(file, articlehtml)
88 routemap(path, file)
89
90 # Update archive listings
91 listinghtml = "" + article_listing_template
92 listinghtml = listinghtml.replace("$title", article["title"])
93 listinghtml = listinghtml.replace("$date", article["date"])
94 listinghtml = listinghtml.replace("$banner", article["banner"])
95 listinghtml = listinghtml.replace("$summary", article["summary"])
96 listinghtml = listinghtml.replace("$permalink", path)
97 listings = listinghtml + listings
98
99 # Blog archive
100 print("creating blog archive")
101 archive_template = readfile(config["templates"]["blog-archive"])
102 archive_template = archive_template.replace("$css", blog_css)
103 archive_template = archive_template.replace("$articles", listings)
104 archive_template = archive_template.replace("$copyright", config["copyright"])
105 file = os.path.join(out_path, "blog.html")
106 writefile(file, archive_template)
107 routemap("/blog", file)
108
109 # Error 404
110 print("creating 404 error page")
111 e404 = readfile(config["templates"]["404"])
112 file = os.path.join(out_path, "error-404.html")
113 writefile(file, e404)
114
115 # Routemap config
116 print("Routemap:")
117 for map in routemaps:
118 print(map)
119
120if __name__ == "__main__":
121 if len(sys.argv) < 2:
122 print("usage: compile.py <folder>")
123 exit(1)
124 folder_out = sys.argv[1]
125 print("compile.py starting")
126 if not os.path.isdir(folder_out):
127 print(folder_out + " is not a valid folder location. exiting")
128 exit(1)
129 out_path = os.path.abspath(folder_out)
130 print("output set to " + out_path)
131 print("beginning main routine")
132 main()
133 print("finished")
134