feat: implemented context class

This commit is contained in:
2025-04-10 14:43:37 +02:00
parent 0b7bfddd64
commit fab58de71d
6 changed files with 56 additions and 69 deletions

View File

@@ -39,9 +39,42 @@ def copy_static():
dest_file = os.path.join(dest_dir, file)
shutil.copy2(src_file, dest_file)
print(src, dst)
class Context:
def __init__(self, root: str = SOURCE_DIRECTORY):
self.file = Path(root, "index.html")
current_root = Path(root)
while current_root.parts and self.file is None:
current_file = Path(current_root, "index.html")
if current_file.exists() and current_file.is_file:
self.file = current_file
current_root = current_root.parent
if self.file is None:
logger.error("couldn't find context for %s", root)
exit(1)
logger.info("%s found context %r", root, str(self.file))
def get_text(self, placeholder_values: dict):
text = self.file.read_text()
for key, value in placeholder_values.items():
text = text.replace(f"<{key}/>", value)
text = text.replace(f"<{key} />", value)
return text
def build():
logger.info("building static page")
copy_static()
context = Context()
print(context.get_text({
"content": """
<h1 class="title">Hello World</h1>
<p class="subtitle">My first website with <strong>Bulma</strong>!</p>
"""
}))