#!/usr/bin/python
import os
import glob
import mistune
from jinja2 import Environment, FileSystemLoader, select_autoescape
public_path = "../site/public"
content_path = "../site/content"
template_path = "../site/templates"
env = Environment(
loader=FileSystemLoader(template_path),
autoescape=select_autoescape([])
)
renderer = mistune.Renderer(escape=False)
markdown = mistune.Markdown(renderer=renderer)
def wide_section(line):
return ""
def normal_section(lines):
if len(lines):
return "" + markdown("\n\n".join(lines)) + ""
return ""
def build_file(fn):
print(fn)
output_path = os.path.dirname(fn).replace(content_path, public_path)
output_fn = os.path.join(output_path, "index.html")
with open(fn, "r") as file:
sections = file.read().split("\n\n")
metadata = {}
for line in sections[0].split("\n"):
print(line)
key, value = line.split(': ', 1)
metadata[key.lower()] = value
groups = []
current_group = []
for section in sections[1:]:
if '![wide]' in section:
groups.append(normal_section(current_group))
groups.append(wide_section(section))
current_group = []
else:
current_group.append(section)
groups.append(normal_section(current_group))
content = "".join(groups)
if 'blog/' in fn:
template = env.get_template("blog.html")
else:
template = env.get_template("page.html")
html = template.render(metadata=metadata, content=content)
os.makedirs(output_path, exist_ok=True)
with open(output_fn, "w") as file:
file.write(html)
def build_site():
print("Building...")
for fn in glob.iglob(os.path.join(content_path, "**/index.txt"), recursive=True):
print(fn)
build_file(fn)
if __name__ == '__main__':
build_site()