create basic html combiner and demo scripts

This commit is contained in:
Crispy 2021-06-24 23:55:00 +02:00
parent e92e2543d8
commit 9bb895731e
6 changed files with 88 additions and 0 deletions

16
docs/test1.html Normal file
View file

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>page 1</title>
</head>
<body>
<h>My awesome header</h>
<p>todo: add links</p>
<p>amazing content</p>
<p>amazing footer</p>
</body>
</html>

54
html-combiner.py Executable file
View file

@ -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()

14
pages/test1.html Normal file
View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>page 1</title>
</head>
<body>
&include(header.html)
<p>amazing content</p>
&include(footer.html)
</body>
</html>

1
templates/footer.html Normal file
View file

@ -0,0 +1 @@
<p>amazing footer</p>

2
templates/header.html Normal file
View file

@ -0,0 +1,2 @@
<h>My awesome header</h>
&include(navbar.html)

1
templates/navbar.html Normal file
View file

@ -0,0 +1 @@
<p>todo: add links</p>