diff --git a/pyproject.toml b/pyproject.toml index d00c0a3..96e6f10 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,8 @@ [project] name = "stsg" dependencies = [ - "watchdog~=6.0.0" + "watchdog~=6.0.0", + "markdown~=3.3.6" ] dynamic = [] authors = [] diff --git a/src/example_page_1/de-DE.md b/src/example_page_1/de-DE.md index 0519ecb..dae1eee 100644 --- a/src/example_page_1/de-DE.md +++ b/src/example_page_1/de-DE.md @@ -1 +1,6 @@ - \ No newline at end of file +# foo + +Lore ipsum dolor sit amend + +- bar +- foobar \ No newline at end of file diff --git a/src/index.html b/src/index.html index aecb12f..b0d4ed1 100644 --- a/src/index.html +++ b/src/index.html @@ -4,7 +4,7 @@ STSG - + @@ -22,7 +22,7 @@
-
+
diff --git a/stsg/build.py b/stsg/build.py index 90fd0fa..9df0cc8 100644 --- a/stsg/build.py +++ b/stsg/build.py @@ -2,6 +2,8 @@ import logging import shutil from pathlib import Path import os +import markdown +from typing import Optional from .config import SOURCE_DIRECTORY, DIST_DIRECTORY, STATIC_DIRECTORY @@ -39,7 +41,7 @@ def copy_static(src, dst): class Context: def __init__(self, root: str = SOURCE_DIRECTORY): - self.file = Path(root, "index.html") + self.file = None current_root = Path(root) while current_root.parts and self.file is None: @@ -54,7 +56,7 @@ class Context: exit(1) logger.info("%s found context %r", root, str(self.file)) - def get_text(self, placeholder_values: dict): + def get_text(self, **placeholder_values: dict): text = self.file.read_text() for key, value in placeholder_values.items(): @@ -64,21 +66,38 @@ class Context: return text +def convert_md(src: Path, dst: Path, context: Optional[Context] = None): + html_content = markdown.markdown(src.read_text()) + context = Context(str(src.parent)) + full_page = context.get_text(content=html_content) + + folder_dst = dst.parent / dst.name.replace(".md", "") + folder_dst.mkdir(parents=True, exist_ok=True) + + + with Path(folder_dst, "index.html").open("w") as f: + f.write(full_page) + def walk_directory(root): src_path = Path(SOURCE_DIRECTORY, root) dst_path = Path(DIST_DIRECTORY, root) + language_codes_found = [] + for current_full_path in src_path.iterdir(): current_name = Path(current_full_path).name current_dst = Path(dst_path, current_name) current_src = Path(src_path, current_name) if current_name == "static": - print(current_src, current_dst) copy_static(current_src, current_dst) continue + + if current_name.endswith(".md"): + print(current_dst) + convert_md(current_src, current_dst) - if current_dst.is_dir(): + if current_src.is_dir(): walk_directory(Path(root, current_full_path.name))