2021-06-24 23:55:00 +02:00
|
|
|
#!/bin/env python3
|
|
|
|
import os
|
|
|
|
|
|
|
|
PAGE_DIR = "./pages/"
|
|
|
|
TEMPLATE_DIR = "./templates/"
|
|
|
|
TARGET_DIR = "./docs/"
|
|
|
|
|
2021-06-25 00:21:46 +02:00
|
|
|
INCLUDE_MARKER = "&include("
|
|
|
|
INCLUDE_MARKER_END = ")"
|
2021-06-24 23:55:00 +02:00
|
|
|
|
|
|
|
def process_dir(path: str = "") -> None:
|
|
|
|
items = os.listdir(PAGE_DIR + path)
|
2021-06-25 00:21:46 +02:00
|
|
|
for name in items:
|
2021-06-25 00:42:16 +02:00
|
|
|
if os.path.isdir(PAGE_DIR + path + name):
|
2021-06-25 00:21:46 +02:00
|
|
|
process_dir(path + name + "/")
|
2021-06-24 23:55:00 +02:00
|
|
|
else:
|
2021-06-25 00:21:46 +02:00
|
|
|
process_file(path + name)
|
2021-06-24 23:55:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
def process_file(filepath: str) -> str:
|
2021-06-25 00:42:16 +02:00
|
|
|
print("processing " + filepath)
|
2021-06-25 00:21:46 +02:00
|
|
|
contents = read_file(PAGE_DIR + filepath)
|
|
|
|
while INCLUDE_MARKER in contents:
|
|
|
|
contents = apply_include(contents)
|
2021-06-24 23:55:00 +02:00
|
|
|
|
2021-06-25 00:42:16 +02:00
|
|
|
ensure_dir(TARGET_DIR + filepath)
|
|
|
|
|
2021-06-24 23:55:00 +02:00
|
|
|
with open(TARGET_DIR + filepath, "w") as f:
|
|
|
|
f.write(contents)
|
|
|
|
|
|
|
|
|
2021-06-25 00:42:16 +02:00
|
|
|
def ensure_dir(filepath: str):
|
|
|
|
i = len(filepath) - filepath[::-1].find("/")
|
|
|
|
dir = filepath[:i]
|
|
|
|
if not os.path.exists(dir):
|
|
|
|
os.mkdir(dir)
|
|
|
|
|
|
|
|
|
2021-06-25 00:21:46 +02:00
|
|
|
def apply_include(contents: str) -> str:
|
|
|
|
included_file = get_included_name(contents)
|
2021-06-24 23:55:00 +02:00
|
|
|
|
2021-06-25 00:21:46 +02:00
|
|
|
new_contents = read_file(TEMPLATE_DIR + included_file)
|
|
|
|
return insert_contents(contents, new_contents)
|
|
|
|
|
|
|
|
|
|
|
|
def insert_contents(contents, new_contents):
|
|
|
|
index_start, index_end = get_marker_indices(contents)
|
2021-06-24 23:55:00 +02:00
|
|
|
return contents[:index_start] + new_contents + contents[index_end+1:]
|
|
|
|
|
|
|
|
|
2021-06-25 00:21:46 +02:00
|
|
|
def get_included_name(contents):
|
|
|
|
marker_start, marker_end = get_marker_indices(contents)
|
|
|
|
name_start = marker_start + len(INCLUDE_MARKER)
|
|
|
|
name_end = marker_end - len(INCLUDE_MARKER_END)
|
|
|
|
return contents[name_start:name_end + 1]
|
|
|
|
|
|
|
|
|
|
|
|
def get_marker_indices(contents: str) -> tuple:
|
|
|
|
index_start = contents.find(INCLUDE_MARKER)
|
|
|
|
index_end = contents.find(INCLUDE_MARKER_END, index_start)
|
2021-06-24 23:55:00 +02:00
|
|
|
return (index_start, index_end)
|
|
|
|
|
|
|
|
|
2021-06-25 00:21:46 +02:00
|
|
|
def read_file(filepath: str):
|
2021-06-24 23:55:00 +02:00
|
|
|
contents = ""
|
2021-06-25 00:21:46 +02:00
|
|
|
with open(filepath, "r") as f:
|
2021-06-24 23:55:00 +02:00
|
|
|
contents = f.read()
|
|
|
|
return contents
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
process_dir()
|