Index

joshstock.in / 00c3573

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
711 Nov 2019 18:2700c3573opengraph meta tags, privacy pageJosh Stockin170N

Blob @ joshstock.in / compile.py

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