Index

joshstock.in / 171aa88

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
2522 Dec 2019 19:24ee7e31fUpdate blog formatting; move CSS substitution to new stylesheetJosh Stockin1103N

Blob @ joshstock.in / compile.py

application/x-python4980 bytesdownload raw
1#!/usr/bin/env python3
2
3import sys
4import os
5import shutil
6import json
7
8def readfile(filename):
9 try:
10 with open(filename, "r") as file:
11 s = file.read()
12 file.close()
13 return s
14 except FileNotFoundError:
15 print(filename + " not found. exiting")
16 exit(1)
17 except:
18 print("can't open " + filename + " for reading. exiting")
19 exit(1)
20
21def writefile(filename, text):
22 try:
23 with open(filename, "w") as file:
24 file.write(text)
25 file.close()
26 except:
27 print("can't open " + filename + " for writing. exiting")
28 exit(1)
29
30def empty_dir(path):
31 for file in os.listdir(path):
32 filepath = os.path.join(path, file)
33 try:
34 if os.path.isfile(filepath):
35 os.unlink(filepath)
36 elif os.path.isdir(filepath):
37 shutil.rmtree(filepath)
38 except Exception as e:
39 print("error while trying to empty working directory")
40 print(e)
41 exit(1)
42
43routemaps = {}
44sitemap = """<?xml version="1.0" encoding="UTF-8"?>
45<urlset
46 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
47 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
48 xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
49 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">"""
50def routemap(route, priority=0.5):
51 routemaps[route] = True
52 global sitemap
53 sitemap += "<url><loc>https://joshstock.in" + route + "</loc><priority>" + str(priority) + "</priority></url>"
54
55def main():
56 print("emptying working directory")
57 empty_dir(out_path)
58
59 print("reading config")
60 config = json.loads(readfile("config.json"))
61
62 # Static
63 print("copying static")
64 shutil.copytree(config["static"], os.path.join(out_path, "static"))
65
66 # Favicon
67 print("copying favicon")
68 shutil.copy(config["templates"]["favicon"], os.path.join(out_path, "favicon.ico"))
69
70 # Index
71 print("creating landing")
72 landing = readfile(config["templates"]["landing"])
73 file = os.path.join(out_path, "index.html")
74 writefile(file, landing)
75 routemap("/", 1.0)
76
77 # CSS
78 print("copying CSS source")
79 blog_css = readfile(config["templates"]["blog-css"])
80 file = os.path.join(out_path, "blog.css")
81 writefile(file, blog_css)
82
83 # Privacy
84 print("creating privacy policy page")
85 privacy = readfile(config["templates"]["privacy"])
86 privacy = privacy.replace("$copyright", config["copyright"])
87 file = os.path.join(out_path, "privacy.html")
88 writefile(file, privacy)
89 routemap("/privacy", 0.5)
90
91 # /blog*
92 print("creating blog articles")
93 listings = ""
94 article_listing_template = readfile(config["templates"]["blog-archive-listing"])
95 article_template = readfile(config["templates"]["blog-article"])
96
97 for article in config["articles"]:
98 # Create article
99 print("creating article \"" + article["title"] + "\"")
100 articlehtml = "" + article_template
101 articlehtml = articlehtml.replace("$title", article["title"])
102 articlehtml = articlehtml.replace("$date", article["date"])
103 articlehtml = articlehtml.replace("$banner", article["banner"])
104 articlehtml = articlehtml.replace("$content", readfile(article["content"]))
105 articlehtml = articlehtml.replace("$summary", article["summary"])
106 articlehtml = articlehtml.replace("$copyright", config["copyright"])
107 file = os.path.join(out_path, "blog-"+article["title"].lower().replace(" ", "-")+".html")
108 path = "/blog/"+article["title"].lower().replace(" ", "-")
109 articlehtml = articlehtml.replace("$permalink", path)
110 writefile(file, articlehtml)
111 routemap(path, 0.7)
112
113 # Update archive listings
114 listinghtml = "" + article_listing_template
115 listinghtml = listinghtml.replace("$title", article["title"])
116 listinghtml = listinghtml.replace("$date", article["date"])
117 listinghtml = listinghtml.replace("$banner", article["banner"])
118 listinghtml = listinghtml.replace("$summary", article["summary"])
119 listinghtml = listinghtml.replace("$permalink", path)
120 listings = listinghtml + listings
121
122 # Blog archive
123 print("creating blog archive")
124 archive_template = readfile(config["templates"]["blog-archive"])
125 archive_template = archive_template.replace("$articles", listings)
126 archive_template = archive_template.replace("$copyright", config["copyright"])
127 file = os.path.join(out_path, "blog.html")
128 writefile(file, archive_template)
129 routemap("/blog", 0.9)
130
131 # Error 404
132 print("creating 404 error page")
133 e404 = readfile(config["templates"]["404"])
134 file = os.path.join(out_path, "error-404.html")
135 writefile(file, e404)
136
137 # Routemap config
138 print("writing sitemap to sitemap.xml")
139 global sitemap
140 sitemap += "</urlset>"
141 writefile(os.path.join(out_path, "sitemap.xml"), sitemap)
142
143if __name__ == "__main__":
144 if len(sys.argv) < 2:
145 print("usage: compile.py <folder>")
146 exit(1)
147 folder_out = sys.argv[1]
148 print("compile.py starting")
149 print("changing active directory to script location")
150 os.chdir(sys.path[0])
151 if not os.path.isdir(folder_out):
152 print(folder_out + " is not a valid folder location. exiting")
153 exit(1)
154 out_path = os.path.abspath(folder_out)
155 print("output set to " + out_path)
156 print("beginning main routine")
157 main()
158 print("finished")
159