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