Index

joshstock.in / cd46007

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
310 Nov 2019 22:219e52c86basic landing page, blogJosh Stockin1780N

Blob @ joshstock.in / compile.py

application/x-python2839 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. exitting")
15 exit(1)
16 except:
17 print("can't open " + filename + " for reading. exitting")
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. exitting")
27 exit(1)
28
29def main():
30 config = json.loads(readfile("config.json"))
31 print("creating blog articles")
32 listings = ""
33 article_listing_template = readfile(config["templates"]["blog-archive-listing"])
34 article_template = readfile(config["templates"]["blog-article"])
35 blog_css = readfile(config["templates"]["blog-css"])
36 for article in config["articles"]:
37 print("creating article \"" + article["title"] + "\"")
38 articlehtml = "" + article_template
39 articlehtml = articlehtml.replace("$css", blog_css)
40 articlehtml = articlehtml.replace("$title", article["title"])
41 articlehtml = articlehtml.replace("$date", article["date"])
42 articlehtml = articlehtml.replace("$banner", article["banner"])
43 articlehtml = articlehtml.replace("$content", readfile(article["content"]))
44 articlehtml = articlehtml.replace("$copyright", config["copyright"])
45 link = article["title"].lower().replace(" ", "-")+".html"
46 articlehtml = articlehtml.replace("$permalink", link)
47 writefile(os.path.join(out_path, link), articlehtml)
48 listinghtml = "" + article_listing_template
49 listinghtml = listinghtml.replace("$title", article["title"])
50 listinghtml = listinghtml.replace("$date", article["date"])
51 listinghtml = listinghtml.replace("$banner", article["banner"])
52 listinghtml = listinghtml.replace("$summary", article["summary"])
53 listinghtml = listinghtml.replace("$permalink", link)
54 listings = listinghtml + listings
55 print("creating blog archive")
56 archive_template = readfile(config["templates"]["blog-archive"])
57 archive_template = archive_template.replace("$css", blog_css)
58 archive_template = archive_template.replace("$articles", listings)
59 archive_template = archive_template.replace("$copyright", config["copyright"])
60 writefile(os.path.join(out_path, "blog.html"), archive_template)
61 print("creating landing")
62 landing = readfile(config["templates"]["landing"])
63 writefile(os.path.join(out_path, "index.html"), landing)
64
65if __name__ == "__main__":
66 if len(sys.argv) < 2:
67 print("usage: compile.py <folder>")
68 exit(1)
69 folder_out = sys.argv[1]
70 print("compile.py starting")
71 if not os.path.isdir(folder_out):
72 print(folder_out + " is not a valid folder location. exitting")
73 exit(1)
74 out_path = os.path.abspath(folder_out)
75 print("output set to " + out_path)
76 print("beginning main routine")
77 main()
78 print("finished")
79