From e48dec20a381753002b6dc723d8f81225733c4cd Mon Sep 17 00:00:00 2001 From: jgrogan Date: Mon, 18 Nov 2024 20:34:33 +0000 Subject: [PATCH] Small code cleanup --- src/dialann/cv.py | 41 +++++++++++------- src/dialann/main_cli.py | 22 ++++++---- src/dialann/site_generator.py | 78 ++++++++++++++++++++--------------- 3 files changed, 85 insertions(+), 56 deletions(-) diff --git a/src/dialann/cv.py b/src/dialann/cv.py index ef766bd..83a98d1 100644 --- a/src/dialann/cv.py +++ b/src/dialann/cv.py @@ -1,30 +1,35 @@ import json -class DataRange(): + +class DataRange: def __init__(self): self.start = "" self.end = "" -class Address(): + +class Address: def __init__(self): self.region = "" self.country = "" -class Organisation(): + +class Organisation: def __init__(self): self.name = "" self.address = Address() -class CvPosition(): + +class CvPosition: def __init__(self): self.role = "" self.organisation = Organisation() self.date_range = DateRange() + class AcademicEnrolement(CvPosition): def __init__(self): @@ -32,26 +37,30 @@ class AcademicEnrolement(CvPosition): self.program = "" self.grade = "" -class Award(): + +class Award: def __init__(self): self.year = "" self.role = "" self.title = "" self.event = "" - -class CvSection(): + + +class CvSection: def __init__(): self.title = "" + class CvRoles(CvSection): def __init__(self): super().__init__() self.roles = [] -class Author(): + +class Author: def __init__(self): self.first_name = "" @@ -60,28 +69,32 @@ class Author(): self.orcid = "" self.website = "" -class Event(): + +class Event: def __init__(self): self.title = "" self.address = Address() self.date = "" - -class BibliographicItem(): + + +class BibliographicItem: def __init__(self): self.authors = [] self.title = [] -class TeachingRole(): + +class TeachingRole: def __init__(self): self.title = "" self.course = "" self.institute = Organisation() -class Cv(): - + +class Cv: + def __init__(self): self.sections: list[CvSection] = [] diff --git a/src/dialann/main_cli.py b/src/dialann/main_cli.py index 1210337..f3048f1 100644 --- a/src/dialann/main_cli.py +++ b/src/dialann/main_cli.py @@ -3,15 +3,18 @@ import argparse import logging from pathlib import Path -from .site_generator import SiteGenerator +from .site_generator import generate, Config logger = logging.getLogger(__name__) -def generate(source_dir:Path, build_dir: Path): - generator = SiteGenerator(source_dir, build_dir) - generator.run() - +def cli_generate(source_dir: Path, build_dir: Path): + + config = Config(source_dir.resolve(), build_dir.resolve()) + + generate(config) + + def main_cli(): parser = argparse.ArgumentParser() parser.add_argument( @@ -20,11 +23,12 @@ def main_cli(): help="Path to the build output", ) parser.add_argument( - "--source_dir", type=Path, default=Path(), - help="Site source directory") - + "--source_dir", type=Path, default=Path(), help="Site source directory" + ) + args = parser.parse_args() - generate(args.source_dir, args.build_dir) + cli_generate(args.source_dir, args.build_dir) + if __name__ == "__main__": main_cli() diff --git a/src/dialann/site_generator.py b/src/dialann/site_generator.py index 5be4a97..6290c42 100644 --- a/src/dialann/site_generator.py +++ b/src/dialann/site_generator.py @@ -1,51 +1,63 @@ from pathlib import Path import os import shutil +from dataclasses import dataclass from markdown_it import MarkdownIt -class SiteGenerator(): - def __init__(self, source_dir: Path, - build_dir: Path): - self.source_dir = source_dir.resolve() - self.build_dir = build_dir.resolve() - self.parser = MarkdownIt() +@dataclass(frozen=True) +class Config: + source_dir: Path + build_dir: Path - def run(self): - header_src = "" - footer_src = "" +def _read_if_exists(path: Path) -> str: + if path.exists(): + with open(path, "r", encoding="utf-8") as f: + return f.read() + return "" - 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() +def _replace_content(src: Path, dst: Path): + if src.exists(): + if dst.exists(): + shutil.rmtree(dst) + shutil.copytree(src, dst) - 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) +def _write_file(path: Path, content: str): + os.makedirs(output_path.parent, exist_ok=True) - shutil.copytree(static_dir, self.build_dir / "static") + with open(path, "w", encoding="utf-8") as f: + f.write(content) - for md_file in self.source_dir.glob('**/*.md'): - with open(md_file, 'r', encoding='utf-8') as f: - md_src = f.read() - 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) +def _read_file(path: Path) -> str: + with open(path, "r", encoding="utf-8") as f: + return f.read() - html_src = self.parser.render(md_src) - with open(html_path, 'w', encoding='utf-8') as f: - f.write(header_src + html_src + footer_src) - +def generate(config: Config): + + static_dir = config.source_dir / "static" + build_static_dir = config.build_dir / "static" + _replace_content(static_dir, build_static_dir) + + template_dir = config.source_dir / "templates" + header = _read_if_exists(template_dir / "header.html") + footer = _read_if_exists(template_dir / "footer.html") + + parser = MarkdownIt() + sources = config.source_dir.glob("**/*.md") + + for source in sources: + source_dir = source.parent.relative_to(config.source_dir) + output_path = config.build_dir / source_dir / (str(source.stem) + ".html") + + with open(md_file, "r", encoding="utf-8") as f: + md_src = f.read() + + content = _read_file(source) + output = parser.render(content) + _write_file(output_path, header + output + footer)