This commit is contained in:
Crispy 2021-06-25 00:21:46 +02:00
parent 9bb895731e
commit e3bd8369db
2 changed files with 30 additions and 22 deletions

View file

@ -10,7 +10,7 @@
<h>My awesome header</h> <h>My awesome header</h>
<p>todo: add links</p> <p>todo: add links</p>
<p>amazing content</p> <p>amazing content</p>
<p>amazing footer</p> <p>amazing footer</p>
</body> </body>
</html> </html>

View file

@ -5,50 +5,58 @@ PAGE_DIR = "./pages/"
TEMPLATE_DIR = "./templates/" TEMPLATE_DIR = "./templates/"
TARGET_DIR = "./docs/" TARGET_DIR = "./docs/"
INCLUDE_DECORATOR = "&include(" INCLUDE_MARKER = "&include("
INCLUDE_DECORATOR_END = ")" INCLUDE_MARKER_END = ")"
def process_dir(path: str = "") -> None: def process_dir(path: str = "") -> None:
items = os.listdir(PAGE_DIR + path) items = os.listdir(PAGE_DIR + path)
for i in items: for name in items:
i_path = path + i if os.path.isdir(path + name):
if os.path.isdir(i_path): process_dir(path + name + "/")
process_dir(i_path + "/")
else: else:
process_file(i_path) process_file(path + name)
def process_file(filepath: str) -> str: def process_file(filepath: str) -> str:
contents = read_file(PAGE_DIR, filepath) contents = read_file(PAGE_DIR + filepath)
while INCLUDE_DECORATOR in contents: while INCLUDE_MARKER in contents:
contents = replace_next_include(contents) contents = apply_include(contents)
with open(TARGET_DIR + filepath, "w") as f: with open(TARGET_DIR + filepath, "w") as f:
f.write(contents) f.write(contents)
def replace_next_include(contents: str) -> str: def apply_include(contents: str) -> str:
index_start, index_end = get_include_indices(contents) included_file = get_included_name(contents)
include_name = contents[index_start + len(INCLUDE_DECORATOR):index_end]
new_contents = read_file(TEMPLATE_DIR, include_name) 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)
return contents[:index_start] + new_contents + contents[index_end+1:] return contents[:index_start] + new_contents + contents[index_end+1:]
def get_include_indices(contents: str) -> tuple: def get_included_name(contents):
index_start = contents.find(INCLUDE_DECORATOR) marker_start, marker_end = get_marker_indices(contents)
index_end = contents.find(INCLUDE_DECORATOR_END, index_start) 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)
return (index_start, index_end) return (index_start, index_end)
def read_file(folder: str, filepath: str): def read_file(filepath: str):
contents = "" contents = ""
with open(folder + filepath, "r") as f: with open(filepath, "r") as f:
contents = f.read() contents = f.read()
return contents return contents
if __name__ == "__main__": if __name__ == "__main__":
process_dir() process_dir()