From 9bb895731eaf1333e3459ea245db97e51b08947a Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Thu, 24 Jun 2021 23:55:00 +0200 Subject: [PATCH] create basic html combiner and demo scripts --- docs/test1.html | 16 +++++++++++++ html-combiner.py | 54 +++++++++++++++++++++++++++++++++++++++++++ pages/test1.html | 14 +++++++++++ templates/footer.html | 1 + templates/header.html | 2 ++ templates/navbar.html | 1 + 6 files changed, 88 insertions(+) create mode 100644 docs/test1.html create mode 100755 html-combiner.py create mode 100644 pages/test1.html create mode 100644 templates/footer.html create mode 100644 templates/header.html create mode 100644 templates/navbar.html diff --git a/docs/test1.html b/docs/test1.html new file mode 100644 index 0000000..73861ce --- /dev/null +++ b/docs/test1.html @@ -0,0 +1,16 @@ + + + + + + + page 1 + + + My awesome header +

todo: add links

+

amazing content

+

amazing footer

+ + + \ No newline at end of file diff --git a/html-combiner.py b/html-combiner.py new file mode 100755 index 0000000..611b776 --- /dev/null +++ b/html-combiner.py @@ -0,0 +1,54 @@ +#!/bin/env python3 +import os + +PAGE_DIR = "./pages/" +TEMPLATE_DIR = "./templates/" +TARGET_DIR = "./docs/" + +INCLUDE_DECORATOR = "&include(" +INCLUDE_DECORATOR_END = ")" + +def process_dir(path: str = "") -> None: + items = os.listdir(PAGE_DIR + path) + for i in items: + i_path = path + i + if os.path.isdir(i_path): + process_dir(i_path + "/") + else: + process_file(i_path) + + +def process_file(filepath: str) -> str: + contents = read_file(PAGE_DIR, filepath) + while INCLUDE_DECORATOR in contents: + contents = replace_next_include(contents) + + with open(TARGET_DIR + filepath, "w") as f: + f.write(contents) + + +def replace_next_include(contents: str) -> str: + index_start, index_end = get_include_indices(contents) + + include_name = contents[index_start + len(INCLUDE_DECORATOR):index_end] + + new_contents = read_file(TEMPLATE_DIR, include_name) + return contents[:index_start] + new_contents + contents[index_end+1:] + + +def get_include_indices(contents: str) -> tuple: + index_start = contents.find(INCLUDE_DECORATOR) + index_end = contents.find(INCLUDE_DECORATOR_END, index_start) + return (index_start, index_end) + + +def read_file(folder: str, filepath: str): + contents = "" + with open(folder + filepath, "r") as f: + contents = f.read() + return contents + + + +if __name__ == "__main__": + process_dir() \ No newline at end of file diff --git a/pages/test1.html b/pages/test1.html new file mode 100644 index 0000000..f00aab5 --- /dev/null +++ b/pages/test1.html @@ -0,0 +1,14 @@ + + + + + + + page 1 + + + &include(header.html) +

amazing content

+ &include(footer.html) + + \ No newline at end of file diff --git a/templates/footer.html b/templates/footer.html new file mode 100644 index 0000000..8d9d67f --- /dev/null +++ b/templates/footer.html @@ -0,0 +1 @@ +

amazing footer

diff --git a/templates/header.html b/templates/header.html new file mode 100644 index 0000000..970b848 --- /dev/null +++ b/templates/header.html @@ -0,0 +1,2 @@ +My awesome header +&include(navbar.html) \ No newline at end of file diff --git a/templates/navbar.html b/templates/navbar.html new file mode 100644 index 0000000..4cbf8c1 --- /dev/null +++ b/templates/navbar.html @@ -0,0 +1 @@ +

todo: add links

\ No newline at end of file