Index

joshstock.in / fd93ec3

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
811 Nov 2019 18:50fd93ec3OpenGraph description tagsJosh Stockin110N

Blob @ joshstock.in / compile.py

application/x-python3667 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("$summary", article["summary"])
67 articlehtml = articlehtml.replace("$copyright", config["copyright"])
68 file = os.path.join(out_path, "blog-"+article["title"].lower().replace(" ", "-")+".html")
69 path = "/blog/"+article["title"].lower().replace(" ", "-")
70 articlehtml = articlehtml.replace("$permalink", path)
71 writefile(file, articlehtml)
72 routemap(path, file)
73
74 # Update archive listings
75 listinghtml = "" + article_listing_template
76 listinghtml = listinghtml.replace("$title", article["title"])
77 listinghtml = listinghtml.replace("$date", article["date"])
78 listinghtml = listinghtml.replace("$banner", article["banner"])
79 listinghtml = listinghtml.replace("$summary", article["summary"])
80 listinghtml = listinghtml.replace("$permalink", path)
81 listings = listinghtml + listings
82
83 # Blog archive
84 print("creating blog archive")
85 archive_template = readfile(config["templates"]["blog-archive"])
86 archive_template = archive_template.replace("$css", blog_css)
87 archive_template = archive_template.replace("$articles", listings)
88 archive_template = archive_template.replace("$copyright", config["copyright"])
89 file = os.path.join(out_path, "blog.html")
90 writefile(file, archive_template)
91 routemap("/blog", file)
92
93 # Error 404
94 print("creating 404 error page")
95 e404 = readfile(config["templates"]["404"])
96 file = os.path.join(out_path, "error-404.html")
97 writefile(file, e404)
98
99 # Routemap config
100 print("Routemap:")
101 for map in routemaps:
102 print(map)
103
104if __name__ == "__main__":
105 if len(sys.argv) < 2:
106 print("usage: compile.py <folder>")
107 exit(1)
108 folder_out = sys.argv[1]
109 print("compile.py starting")
110 if not os.path.isdir(folder_out):
111 print(folder_out + " is not a valid folder location. exiting")
112 exit(1)
113 out_path = os.path.abspath(folder_out)
114 print("output set to " + out_path)
115 print("beginning main routine")
116 main()
117 print("finished")
118