From 6913498941ea13c901dadc1f1d292bfd5cb74bfd Mon Sep 17 00:00:00 2001 From: jgrogan Date: Tue, 27 Aug 2024 08:51:26 +0100 Subject: [PATCH] Support headers, footers and static content --- src/dialann/site_generator.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/dialann/site_generator.py b/src/dialann/site_generator.py index d509ffa..5be4a97 100644 --- a/src/dialann/site_generator.py +++ b/src/dialann/site_generator.py @@ -1,5 +1,6 @@ from pathlib import Path import os +import shutil from markdown_it import MarkdownIt @@ -7,21 +8,44 @@ class SiteGenerator(): def __init__(self, source_dir: Path, build_dir: Path): - self.source_dir = source_dir - self.build_dir = build_dir + self.source_dir = source_dir.resolve() + self.build_dir = build_dir.resolve() self.parser = MarkdownIt() def run(self): + header_src = "" + footer_src = "" + + header_path = self.source_dir / "templates/header.html" + footer_path = self.source_dir / "templates/footer.html" + if header_path.exists(): + with open(header_path, 'r', encoding='utf-8') as f: + header_src = f.read() + + if footer_path.exists(): + with open(footer_path, 'r', encoding='utf-8') as f: + footer_src = f.read() + + static_dir = self.source_dir / "static" + build_static_dir = self.build_dir / "static" + if static_dir.exists(): + + if build_static_dir.exists(): + shutil.rmtree(build_static_dir) + + shutil.copytree(static_dir, self.build_dir / "static") + for md_file in self.source_dir.glob('**/*.md'): with open(md_file, 'r', encoding='utf-8') as f: md_src = f.read() - html_path = self.build_dir / (str(md_file.stem) + ".html") + md_dir = md_file.parent.relative_to(self.source_dir) + html_path = self.build_dir / md_dir / (str(md_file.stem) + ".html") os.makedirs(html_path.parent, exist_ok=True) html_src = self.parser.render(md_src) with open(html_path, 'w', encoding='utf-8') as f: - f.write(html_src) + f.write(header_src + html_src + footer_src)