Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/ng2web/ng2web.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
##############################################################################
# Jinja2 imports.
from jinja2 import (
ChoiceLoader,
Environment,
FileSystemLoader,
PackageLoader,
select_autoescape,
)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Loading