1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#!/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 "<section class='wide'>" + markdown(line) + "</section>"
def normal_section(lines):
if len(lines):
return "<section>" + markdown("\n\n".join(lines)) + "</section>"
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()
|