Index

joshstock.in / 0bb9ab9

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
2016 Nov 2019 20:339385d2dStatic files, style changes, new blog articleJosh Stockin12011N

Blob @ joshstock.in / compile.py

application/x-python4868 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 # Index
67 print("creating landing")
68 landing = readfile(config["templates"]["landing"])
69 file = os.path.join(out_path, "index.html")
70 writefile(file, landing)
71 routemap("/", 1.0)
72
73 # Privacy
74 print("creating privacy policy page")
75 privacy = readfile(config["templates"]["privacy"])
76 privacy = privacy.replace("$copyright", config["copyright"])
77 file = os.path.join(out_path, "privacy.html")
78 writefile(file, privacy)
79 routemap("/privacy", 0.5)
80
81 # /blog*
82 print("creating blog articles")
83 listings = ""
84 article_listing_template = readfile(config["templates"]["blog-archive-listing"])
85 article_template = readfile(config["templates"]["blog-article"])
86 blog_css = readfile(config["templates"]["blog-css"])
87
88 for article in config["articles"]:
89 # Create article
90 print("creating article \"" + article["title"] + "\"")
91 articlehtml = "" + article_template
92 articlehtml = articlehtml.replace("$css", blog_css)
93 articlehtml = articlehtml.replace("$title", article["title"])
94 articlehtml = articlehtml.replace("$date", article["date"])
95 articlehtml = articlehtml.replace("$banner", article["banner"])
96 articlehtml = articlehtml.replace("$content", readfile(article["content"]))
97 articlehtml = articlehtml.replace("$summary", article["summary"])
98 articlehtml = articlehtml.replace("$copyright", config["copyright"])
99 file = os.path.join(out_path, "blog-"+article["title"].lower().replace(" ", "-")+".html")
100 path = "/blog/"+article["title"].lower().replace(" ", "-")
101 articlehtml = articlehtml.replace("$permalink", path)
102 writefile(file, articlehtml)
103 routemap(path, 0.7)
104
105 # Update archive listings
106 listinghtml = "" + article_listing_template
107 listinghtml = listinghtml.replace("$title", article["title"])
108 listinghtml = listinghtml.replace("$date", article["date"])
109 listinghtml = listinghtml.replace("$banner", article["banner"])
110 listinghtml = listinghtml.replace("$summary", article["summary"])
111 listinghtml = listinghtml.replace("$permalink", path)
112 listings = listinghtml + listings
113
114 # Blog archive
115 print("creating blog archive")
116 archive_template = readfile(config["templates"]["blog-archive"])
117 archive_template = archive_template.replace("$css", blog_css)
118 archive_template = archive_template.replace("$articles", listings)
119 archive_template = archive_template.replace("$copyright", config["copyright"])
120 file = os.path.join(out_path, "blog.html")
121 writefile(file, archive_template)
122 routemap("/blog", 0.9)
123
124 # Error 404
125 print("creating 404 error page")
126 e404 = readfile(config["templates"]["404"])
127 file = os.path.join(out_path, "error-404.html")
128 writefile(file, e404)
129
130 # Routemap config
131 print("writing sitemap to sitemap.xml")
132 global sitemap
133 sitemap += "</urlset>"
134 writefile(os.path.join(out_path, "sitemap.xml"), sitemap)
135
136if __name__ == "__main__":
137 if len(sys.argv) < 2:
138 print("usage: compile.py <folder>")
139 exit(1)
140 folder_out = sys.argv[1]
141 print("compile.py starting")
142 print("changing active directory to script location")
143 os.chdir(sys.path[0])
144 if not os.path.isdir(folder_out):
145 print(folder_out + " is not a valid folder location. exiting")
146 exit(1)
147 out_path = os.path.abspath(folder_out)
148 print("output set to " + out_path)
149 print("beginning main routine")
150 main()
151 print("finished")
152