diff --git a/src/ng2web/ng2web.py b/src/ng2web/ng2web.py index 52f1fae..3bb35f2 100644 --- a/src/ng2web/ng2web.py +++ b/src/ng2web/ng2web.py @@ -12,7 +12,9 @@ ############################################################################## # Jinja2 imports. from jinja2 import ( + ChoiceLoader, Environment, + FileSystemLoader, PackageLoader, select_autoescape, ) @@ -88,6 +90,15 @@ def get_args() -> argparse.Namespace: default=".", ) + # Add an optional source of template files. + parser.add_argument( + "-t", + "--templates", + type=Path, + help="Directory of template overrides", + required=False, + ) + # Add --version parser.add_argument( "-v", @@ -336,6 +347,29 @@ def page_title(guide: NortonGuide, entry: Entry | None = None) -> str: return " ยป ".join(make_dos_like(part) for part in title) +############################################################################## +def make_loader(templates: Path | None) -> ChoiceLoader: + """Make the template loader. + + Args: + templates: Optional directory for template overrides. + + Returns: + Returns the template loader object. + """ + loaders: list[FileSystemLoader] = [] + if templates is not None: + if (templates := templates.expanduser().resolve()).is_dir(): + log(f"Adding {templates} to the template path") + loaders.append(FileSystemLoader(str(templates), followlinks=True)) + else: + log(f"Ignoring {templates} as a template location as it does not exist") + if (local_templates := Path("templates").resolve()).is_dir(): + log(f"Adding {local_templates} to the template path") + loaders.append(FileSystemLoader(str(local_templates), followlinks=True)) + return ChoiceLoader(loaders + [PackageLoader(Path(__file__).stem)]) + + ############################################################################## def to_html(args: argparse.Namespace) -> None: """Convert a Norton Guide into HTML. @@ -372,7 +406,7 @@ def to_html(args: argparse.Namespace) -> None: # Bootstrap the template stuff. env = Environment( - loader=PackageLoader(Path(__file__).stem), autoescape=select_autoescape() + loader=make_loader(args.templates), autoescape=select_autoescape() ) # Set up the global variables for template expansion.