Simple site generation

This commit is contained in:
jgrogan 2024-08-26 17:12:57 +01:00
parent ed18ceb6a5
commit 89ea1f6062
3 changed files with 39 additions and 4 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
.venv .venv
build/ build/
*.egg-info *.egg-info
__pycache__/

View file

@ -3,11 +3,14 @@ import argparse
import logging import logging
from pathlib import Path from pathlib import Path
from .site_generator import SiteGenerator
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def generate(build_dir: Path): def generate(source_dir:Path, build_dir: Path):
pass generator = SiteGenerator(source_dir, build_dir)
generator.run()
def main_cli(): def main_cli():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -16,8 +19,12 @@ def main_cli():
type=Path, type=Path,
help="Path to the build output", help="Path to the build output",
) )
parser.add_argument(
"--source_dir", type=Path, default=Path(),
help="Site source directory")
args = parser.parse_args() args = parser.parse_args()
generate(args.build_dir) generate(args.source_dir, args.build_dir)
if __name__ == "__main__": if __name__ == "__main__":
main_cli() main_cli()

View file

@ -0,0 +1,27 @@
from pathlib import Path
import os
from markdown_it import MarkdownIt
class SiteGenerator():
def __init__(self, source_dir: Path,
build_dir: Path):
self.source_dir = source_dir
self.build_dir = build_dir
self.parser = MarkdownIt()
def run(self):
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")
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)