commit 89548dbf788652d0b6701bebbd22ed5eca64577f
parent f324944d6bd08bd1193bc0c92e130c0c90ce0891
Author: Charlie Stanton <charlie@shtanton.com>
Date: Wed, 5 Aug 2020 13:01:38 +0100
Finishes the html generation
Diffstat:
2 files changed, 75 insertions(+), 15 deletions(-)
diff --git a/build_post.py b/build_post.py
@@ -1,5 +1,5 @@
#!/usr/bin/python3.8
-import argparse, os, re
+import argparse, os, re, json
# TODO: RSS, HTML, Gopher, PDF
@@ -30,28 +30,48 @@ def para_to_html(paragraph):
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")
+def html_template(header, content, nav):
+ with open("./html_template.html", encoding="utf-8") as template_file:
+ template = template_file.read()
+ template = re.sub(r"\{content\}", content, template)
+ template = re.sub(r"\{header\}", header, template)
+ template = re.sub(r"\{nav\}", nav, template)
+ return template
+def generate_files(post, nav):
+ input_file = os.path.join("./posts", post["file"]+".md")
+ html_output = os.path.join("./html", post["file"]+".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)
+ header = "<h2>{title}</h2><em>{date}</em>".format(title=post["title"], date=post["date"])
+ html = html_template(header, html, nav)
with open(html_output, mode="w", encoding="utf-8") as html_file:
html_file.write(html)
+def generate_html_nav(posts):
+ nav = "<ul>"
+ for post in posts:
+ nav = nav + '<li><a href="/{href}.html">{title}</a> - <em>{date}</em></li>'.format(href=post["file"], title=post["title"], date=post["date"])
+ nav = nav + "</ul>"
+ return nav
+
+def generate_html_root(posts):
+ page = "<!DOCTYPE html><html><head><title>Charlie's blog</title></head><body><h2>Charlie's blog</h2><p>RSS and phlog on their way</p><ul>"
+ for post in posts:
+ page = page + '<li><a href="/{file}.html">{title}</a> - <em>{date}</em></li>'.format(file=post["file"], title=post["title"], date=post["date"])
+ page = page + "</body></html>"
+ with open("./html/index.html", mode="w", encoding="utf-8") as index_file:
+ index_file.write(page)
+
parser = argparse.ArgumentParser(description="Generate the site")
-generate_files("ex")
+with open("config.json", encoding="utf-8") as config_file:
+ config = json.load(config_file)
+ posts = config["posts"]
+ html_nav = generate_html_nav(posts)
+ for i in range(len(posts)):
+ generate_files(posts[i], html_nav)
+ generate_html_root(posts)
diff --git a/html_template.html b/html_template.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Charlie's blog</title>
+ <style>
+ @media only screen and (min-width: 640px) {
+ body {
+ display: grid;
+ grid-template-columns: 3fr 1fr;
+ grid-column-gap: 50px;
+ }
+ header {
+ }
+ article {
+ max-width: 600px;
+ }
+ nav {
+ grid-column-start: 2;
+ grid-row-start: 1;
+ grid-row-end: 3;
+ }
+ }
+ body {
+ padding: 20px;
+ }
+ </style>
+ </head>
+ <body>
+ <nav>
+ <h2>Charlie's Blog Posts</h2>
+ {nav}
+ </nav>
+ <header>
+ {header}
+ </header>
+ <article>
+ {content}
+ </article>
+ </body>
+</html>