-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.py
58 lines (48 loc) · 1.66 KB
/
compile.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
from jinja2 import Environment, FileSystemLoader, select_autoescape
import os
from os.path import join
from utils import empty_dir, copy_file
def compile_dir(
templates_dir: str,
pages_dir: str,
output_dir: str,
types: list[str],
no_copy: bool=False,
_clear_out: bool=True,
_base: str="",
_env: Environment=None,
):
if not _env:
_env = Environment(
loader=FileSystemLoader(templates_dir),
autoescape=select_autoescape
)
if _clear_out: empty_dir(output_dir)
for file in os.listdir(join(templates_dir, pages_dir)):
full_path = join(pages_dir, file)
real_path = join(templates_dir, pages_dir, file)
if os.path.isdir(real_path):
os.mkdir(join(output_dir, _base, file))
compile_dir(
templates_dir,
full_path,
output_dir,
types,
no_copy=no_copy,
_clear_out=False,
_base=join(_base, file)
)
continue
if not os.path.isfile(real_path):
# print(f"Skipping non-file '{full_path}'")
continue
if not file.split(".")[-1] in types:
if not no_copy:
# print(f"Copying non-template file '{file}'")
copy_file(real_path, join(output_dir, _base, file))
continue
out_path = join(output_dir, _base, f"{file.split(".")[0]}.html")
with open(out_path, "w") as out:
template = _env.get_template(full_path)
out.write(template.render())
# print(f"Template '{file}' rendered")