1 | #!/usr/bin/env python3 |
2 |
|
3 | import sys |
4 | import os |
5 | import json |
6 |
|
7 | def 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 |
|
20 | def 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 |
|
29 | routemaps = {} |
30 | def routemap(route, file): |
31 | routemaps[route] = file |
32 |
|
33 | def 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 | # /blog* |
44 | print("creating blog articles") |
45 | listings = "" |
46 | article_listing_template = readfile(config["templates"]["blog-archive-listing"]) |
47 | article_template = readfile(config["templates"]["blog-article"]) |
48 | blog_css = readfile(config["templates"]["blog-css"]) |
49 |
|
50 | for article in config["articles"]: |
51 | # Create article |
52 | print("creating article \"" + article["title"] + "\"") |
53 | articlehtml = "" + article_template |
54 | articlehtml = articlehtml.replace("$css", blog_css) |
55 | articlehtml = articlehtml.replace("$title", article["title"]) |
56 | articlehtml = articlehtml.replace("$date", article["date"]) |
57 | articlehtml = articlehtml.replace("$banner", article["banner"]) |
58 | articlehtml = articlehtml.replace("$content", readfile(article["content"])) |
59 | articlehtml = articlehtml.replace("$copyright", config["copyright"]) |
60 | file = os.path.join(out_path, "blog-"+article["title"].lower().replace(" ", "-")+".html") |
61 | path = "/blog/"+article["title"].lower().replace(" ", "-") |
62 | articlehtml = articlehtml.replace("$permalink", path) |
63 | writefile(file, articlehtml) |
64 | routemap(path, file) |
65 |
|
66 | # Update archive listings |
67 | listinghtml = "" + article_listing_template |
68 | listinghtml = listinghtml.replace("$title", article["title"]) |
69 | listinghtml = listinghtml.replace("$date", article["date"]) |
70 | listinghtml = listinghtml.replace("$banner", article["banner"]) |
71 | listinghtml = listinghtml.replace("$summary", article["summary"]) |
72 | listinghtml = listinghtml.replace("$permalink", path) |
73 | listings = listinghtml + listings |
74 |
|
75 | # Blog archive |
76 | print("creating blog archive") |
77 | archive_template = readfile(config["templates"]["blog-archive"]) |
78 | archive_template = archive_template.replace("$css", blog_css) |
79 | archive_template = archive_template.replace("$articles", listings) |
80 | archive_template = archive_template.replace("$copyright", config["copyright"]) |
81 | file = os.path.join(out_path, "blog.html") |
82 | writefile(file, archive_template) |
83 | routemap("/blog", file) |
84 |
|
85 | # Error 404 |
86 | print("creating 404 error page") |
87 | e404 = readfile(config["templates"]["404"]) |
88 | file = os.path.join(out_path, "error-404.html") |
89 | writefile(file, e404) |
90 |
|
91 | # Routemap config |
92 | print("Routemap:") |
93 | for map in routemaps: |
94 | print(map) |
95 |
|
96 | if __name__ == "__main__": |
97 | if len(sys.argv) < 2: |
98 | print("usage: compile.py <folder>") |
99 | exit(1) |
100 | folder_out = sys.argv[1] |
101 | print("compile.py starting") |
102 | if not os.path.isdir(folder_out): |
103 | print(folder_out + " is not a valid folder location. exiting") |
104 | exit(1) |
105 | out_path = os.path.abspath(folder_out) |
106 | print("output set to " + out_path) |
107 | print("beginning main routine") |
108 | main() |
109 | print("finished") |
110 |
|