commit 7576e672f5cd45029cf0382777fca47a3e829ac1
parent 1342387fac580a159652b7827b2d04d008062593
Author: Charlie Stanton <charlie@shtanton.com>
Date: Wed, 5 Aug 2020 08:51:20 +0100
Adds python script for compiling markdown files. So far goes to html
Diffstat:
A | build_post.py | | | 57 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 57 insertions(+), 0 deletions(-)
diff --git a/build_post.py b/build_post.py
@@ -0,0 +1,57 @@
+#!/usr/bin/python3.8
+import argparse, os, re
+
+# TODO: RSS, HTML, Gopher, PDF
+
+def line_to_html(line):
+ line = re.sub(r"`([^`]+)`", r"<code>\1</code>", line)
+ line = re.sub(r"\[([^]]+)\]\(([^)]+)\)", r'<a href="\2">\1</a>', line)
+ return line
+
+def para_to_html(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_html(lines[i][hashes+1:])
+ return "<h"+str(hashes)+">"+"\n".join(lines)+"</h"+str(hashes)+">"
+ elif lines[0][:2] == "- ":
+ for i in range(len(lines)):
+ lines[i] = line_to_html("<li>"+lines[i][2:]+"</li>")
+ return "<ul>"+"".join(lines)+"</ul>"
+ elif lines[0][:2] == "> ":
+ for i in range(len(lines)):
+ lines[i] = line_to_html(lines[i][2:])
+ return "<blockquote>"+"\n".join(lines)+"</blockquote>"
+ else:
+ for i in range(len(lines)):
+ lines[i] = line_to_html(lines[i])
+ return "<p>"+"\n".join(lines)+"</p>"
+
+def html_template(contents):
+ return """
+ <!DOCTYPE html><html><head><title>Charlie's Blog</title></head><body>
+ """+contents+"""
+ </body></html>
+ """
+
+def generate_files(name):
+ input_file = os.path.join("./posts", name+".md")
+ html_output = os.path.join("./html", name+".html")
+
+ html = ""
+
+ 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])
+
+ html = html_template(html)
+ with open(html_output, mode="w", encoding="utf-8") as html_file:
+ html_file.write(html)
+
+parser = argparse.ArgumentParser(description="Generate the site")
+
+generate_files("ex")