commit cdc43cac0bfedce6ae786bef22c958cf73c6f938
parent 3bc09aa35a83514042ec6a349dc9618a7018b270
Author: Charlie Stanton <charlie@shtanton.com>
Date: Wed, 5 Aug 2020 16:33:26 +0100
Adds gopher file generation
Diffstat:
2 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/.gitignore b/.gitignore
@@ -1,2 +1,3 @@
target
-html
+/html/
+/gopher/
diff --git a/build_post.py b/build_post.py
@@ -38,18 +38,47 @@ def html_template(header, content, nav):
template = re.sub(r"\{nav\}", nav, template)
return template
+def line_to_gopher(line):
+ line = re.sub(r"`([^`]+)`", r"\1", line)
+ line = re.sub(r"\[([^]]+)\]\(([^)]+)\)", r"\1 (\2)", line)
+ return line
+
+def para_to_gopher(paragraph):
+ lines = paragraph.split("\n")
+ if lines[0][0] == "#":
+ hashes = 1
+ while lines[0][hashes] == "#":
+ hashes = hashes + 1
+ for i in range(len(lines)):
+ lines[i] = line_to_gopher(lines[i][hashes+1:])
+ return "i{}\tnone\tshtanton.com\t70\r\n".format(" ".join(lines)) + "i\tnone\tshtanton.com\t70\r\n"
+ elif lines[0][:2] == "- " or lines[0][:2] == "> ":
+ for i in range(len(lines)):
+ lines[i] = "i{}\tnone\tshtanton.com\t70\r\n".format(line_to_gopher(lines[i]))
+ return "".join(lines) + "i\tnone\tshtanton.com\t70\r\n"
+ else:
+ for i in range(len(lines)):
+ lines[i] = line_to_gopher(lines[i])
+ return "i{}\tnone\tshtanton.com\t70\r\n".format(" ".join(lines))+"i\tnone\tshtanton.com\t70\r\n"
+
def generate_files(post, nav):
input_file = os.path.join("./posts", post["file"]+".md")
html_output = os.path.join("./html", post["file"]+".html")
+ gopher_output = os.path.join("./gopher", post["file"])
html = ""
+ gopher = "1Return to index\t\tshtanton.com\t70\r\n"
with open(input_file, encoding="utf-8") as input_markdown:
paragraphs = input_markdown.read().split("\n\n")
for i in range(len(paragraphs)):
html = html+para_to_html(paragraphs[i])
+ gopher = gopher+para_to_gopher(paragraphs[i])
header = "<h2>{title}</h2><em>{date}</em>".format(title=post["title"], date=post["date"])
html = html_template(header, html, nav)
+ gopher = gopher+".\r\n"
with open(html_output, mode="w", encoding="utf-8") as html_file:
html_file.write(html)
+ with open(gopher_output, mode="w", encoding="utf-8") as gopher_file:
+ gopher_file.write(gopher)
def generate_html_nav(posts):
nav = "<ul>"
@@ -66,6 +95,14 @@ def generate_html_root(posts):
with open("./html/index.html", mode="w", encoding="utf-8") as index_file:
index_file.write(page)
+def generate_gopher_root(posts):
+ gopher = "iCharlie's phlog\tnone\tshtanton.com\t70\r\n"
+ for post in posts:
+ gopher = gopher + "1{} - {}\t{}\tshtanton.com\t70\r\n".format(post["title"], post["date"], post["file"])
+ gopher = gopher + ".\r\n"
+ with open("./gopher/index", mode="w", encoding="utf-8") as index_file:
+ index_file.write(gopher)
+
parser = argparse.ArgumentParser(description="Generate the site")
with open("config.json", encoding="utf-8") as config_file:
@@ -75,3 +112,4 @@ with open("config.json", encoding="utf-8") as config_file:
for i in range(len(posts)):
generate_files(posts[i], html_nav)
generate_html_root(posts)
+ generate_gopher_root(posts)