-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
141 lines (110 loc) · 4.86 KB
/
main.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""Main module"""
import datetime
import locale
import os
import re
import shutil
import textile
import metadata
import template
inFolder = os.path.abspath("content")
staticFolder = os.path.abspath("static")
outFolder = os.path.abspath("docs")
entries_path = "entry"
entries_out_folder = os.path.join(outFolder, entries_path)
tags_path = "tag"
tags_out_folder = os.path.join(outFolder, tags_path)
OUT_FILE_EXT = ".html"
INDEX_FILE_NAME = "index"
def init():
"""Initialize output location"""
shutil.rmtree(outFolder, ignore_errors=True)
print(f"Creating output target {outFolder}")
os.makedirs(outFolder, exist_ok=True)
os.makedirs(entries_out_folder, exist_ok=True)
os.makedirs(tags_out_folder, exist_ok=True)
def place_static():
"""Move static files into place"""
shutil.copytree(staticFolder, outFolder, dirs_exist_ok=True)
def render_files(state: metadata.SiteState):
"""Find all of the content files and turn them into pages"""
for root, _, files in os.walk(inFolder):
rel_root = os.path.relpath(root, inFolder)
out_root = os.path.join(entries_out_folder, rel_root)
print(f"Creating output directory {out_root}")
os.makedirs(out_root, exist_ok=True)
for file_name in files:
in_path = os.path.join(root, file_name)
out_file_name = os.path.splitext(file_name)[0] + OUT_FILE_EXT
file_path = os.path.join(rel_root, out_file_name)
file_path = file_path.removeprefix("./")
out_path = os.path.join(out_root, out_file_name)
meta = metadata.Entry(file_path)
render_file(in_path, out_path, meta, state)
state.entries.append(meta)
state.entries.sort(key=metadata.Entry.get_creation_date, reverse=True)
def render_file(in_file, out_file, meta, state: metadata.SiteState):
"""Turn a given file into an HTML page"""
print(f"Writing contents of {in_file} to {out_file}...")
with open(in_file, "r", encoding=locale.getpreferredencoding(do_setlocale=False)) as in_stream:
in_contents = in_stream.read()
parse_file_contents(in_contents, meta, state)
if meta.date is None:
if os.name == "nt":
# Windows keeps creating in ctime, POSIX uses it as metadata mod time
meta.date = datetime.date.fromtimestamp(os.stat(in_file).st_ctime)
else:
meta.date = datetime.date.fromtimestamp(os.stat(in_file).st_mtime)
with open(out_file, "w",
encoding=locale.getpreferredencoding(do_setlocale=False)) as out_stream:
out_contents = textile.textile(in_contents)
out_stream.write(template.apply_entry(meta, out_contents))
def parse_file_contents(contents, meta, state: metadata.SiteState):
"""Parse the contents of a file and extract metadata"""
pattern = re.compile(r"\s*###\.\s+(?P<key>bi\.\S+)\s+(?P<value>.*)\s*",
flags=re.IGNORECASE)
for line in contents.splitlines():
match = pattern.fullmatch(line)
if match:
key = match.group('key')
value = match.group('value').strip()
if key == "bi.tag":
meta.tags.append(value)
state.tagged_entries[value].add(meta)
elif key == "bi.title":
meta.title = value
elif key == "bi.date":
meta.date = datetime.date.fromisoformat(value)
elif key == "bi.mod":
meta.modified = datetime.date.fromisoformat(value)
else:
raise Exception("Unknown meta comment " + key)
def generate_index(state: metadata.SiteState):
"""Generate the index page"""
index_file_path = os.path.join(outFolder, INDEX_FILE_NAME) + OUT_FILE_EXT
print("generating index at ", index_file_path)
with open(index_file_path, "w",
encoding=locale.getpreferredencoding(do_setlocale=False)) as index_file:
index_file.write(template.apply_index(state))
def generate_tags(state: metadata.SiteState):
for tag, entries in state.tagged_entries.items():
with open(os.path.join(tags_out_folder, tag) + OUT_FILE_EXT, "w") as outfile:
content = '<ul class="entries_list">\n'
for entry in entries:
title = entry.title
url = template.gen_link("entry", entry.file_path)
dateline = entry.get_dateline()
content += f'<li><a href="{url}">{title}</a> <span class="dateline">{dateline}</span></li>\n'
content += '</ul>'
meta = metadata.File(f"Tag: {tag}")
outfile.write(template.apply_file(meta, content))
def generate():
"""Run all of the steps to create the blog"""
state = metadata.SiteState()
init()
place_static()
render_files(state)
generate_tags(state)
generate_index(state)
if __name__ == "__main__":
generate()