-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_site.py
executable file
·153 lines (119 loc) · 4.73 KB
/
generate_site.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
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
import importlib.util
import json
import logging
import marko
import os
import shutil
import sys
from jinja2 import FileSystemLoader, Environment
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
LOG = logging.getLogger()
DEFAULT_CONFIG_PATH = "./yass_config.json"
DEFAULT_CONFIGS = {
"data_dir": "./data",
"global_template_parameters": {},
"permanent_paths": ["CNAME", "static"],
"site_dir": "./docs",
"sitemap_file": "./sitemap.json",
"static_url": "",
"templates_dir": "./templates",
"template_functions_file": None,
}
def read_json_file(filename):
with open(filename) as f:
return json.loads(f.read())
def render_and_write(yass_config, template_file, params, output_file):
template_env = yass_config["template_env"]
output = template_env.get_template(template_file).render(**params)
with open(os.path.join(yass_config["site_dir"], output_file), "w") as f:
f.write(output)
def generate_page_params(yass_config, level_path, level_data):
params = {}
if level_data.get("data_type"):
with open(os.path.join(yass_config["data_dir"], level_path)) as f:
params["data"] = f.read()
if level_data.get("data_type") == "json":
params["data"] = json.loads(params["data"])
elif level_data.get("data_type") == "markdown":
params["data"] = marko.convert(params["data"])
return params
def generate_level(yass_config, level_map, parent_level_path):
print(level_map)
level_name = level_map["route"]
level_path = os.path.join(parent_level_path, level_name)
output_file = os.path.join(level_path, "index.html")
link = os.path.join("/", level_path)
template = level_map.get("template", "template_contents.html")
LOG.info(f"Generating: {parent_level_path} | {level_path} | {output_file} | {link} | {template}")
if "children" in level_map:
# generate children and get table of contents
additional_params = {
"contents": [
generate_level(yass_config, child, level_path) for child in level_map["children"]
],
}
else:
# single page
additional_params = generate_page_params(yass_config, level_path, level_map)
params = {
"title": level_map["title"],
"tab_title": level_map.get("tab_title", level_map["title"]),
"parent_page": os.path.join("/", parent_level_path),
"static_url": yass_config["static_url"],
**yass_config["global_template_parameters"],
**additional_params,
}
os.makedirs(os.path.join(yass_config["site_dir"], level_path), exist_ok=True)
render_and_write(yass_config, template, params, output_file)
return {
"title": level_map["title"],
"link": link,
}
def generate_site(yass_config):
return generate_level(yass_config, yass_config["sitemap"], parent_level_path="")
def generate_statics(yass_config):
pass
def clear_directory(yass_config):
site_dir = yass_config["site_dir"]
permanent_paths = yass_config["permanent_paths"]
paths = os.listdir(site_dir)
for path in paths:
if path in permanent_paths:
continue
path = os.path.join(site_dir, path)
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
def create_template_env(yass_config):
template_env = Environment(
loader=FileSystemLoader(searchpath=yass_config["templates_dir"])
)
if yass_config["template_functions_file"]:
spec = importlib.util.spec_from_file_location(
"yass_custom",
yass_config["template_functions_file"]
)
template_functions_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(template_functions_module)
for (name, component) in template_functions_module.__dict__.items():
if callable(component) and name.startswith('yass_'):
template_env.globals[name[len('yass_'):]] = component
return template_env
def main():
yass_config_path = DEFAULT_CONFIG_PATH if len(sys.argv) == 1 else sys.argv[1]
LOG.info(f"Using configuration from {yass_config_path}")
yass_config = dict(DEFAULT_CONFIGS, **read_json_file(sys.argv[1]))
LOG.info(f"Using configuration: {yass_config}")
yass_config["sitemap"] = read_json_file(yass_config["sitemap_file"])
yass_config["template_env"] = create_template_env(yass_config)
LOG.info(f"Clearing target directory")
clear_directory(yass_config)
LOG.info("Starting generation ...")
generate_site(yass_config)
LOG.info("Generating statics if needed ...")
generate_statics(yass_config)
LOG.info("Generation complete")
if __name__ == "__main__":
main()