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