Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for a custom template directory path #444

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/wireviz/Harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ def output(
view: bool = False,
cleanup: bool = True,
fmt: tuple = ("html", "png", "svg", "tsv"),
templatedir: (str, Path) = None,
) -> None:
# graphical output
graph = self.graph
Expand Down Expand Up @@ -697,7 +698,7 @@ def output(
print("CSV output is not yet supported")
# HTML output
if "html" in fmt:
generate_html_output(filename, bomlist, self.metadata, self.options)
generate_html_output(filename, bomlist, self.metadata, self.options, templatedir=templatedir)
# PDF output
if "pdf" in fmt:
# TODO: implement PDF output
Expand Down
3 changes: 2 additions & 1 deletion src/wireviz/wireviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def parse(
output_dir: Union[str, Path] = None,
output_name: Union[None, str] = None,
image_paths: Union[Path, str, List] = [],
template_dir: Union[str, Path] = None,
) -> Any:
"""
This function takes an input, parses it as a WireViz Harness file,
Expand Down Expand Up @@ -382,7 +383,7 @@ def alternate_type(): # flip between connector and cable/arrow
harness.add_bom_item(line)

if output_formats:
harness.output(filename=output_file, fmt=output_formats, view=False)
harness.output(filename=output_file, fmt=output_formats, view=False, templatedir=template_dir)

if return_types:
returns = []
Expand Down
10 changes: 9 additions & 1 deletion src/wireviz/wv_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,21 @@
type=str,
help="File name (without extension) to use for output files, if different from input file name.",
)
@click.option(
"-t",
"--template-dir",
default=None,
type=Path,
help="Directory to look into for html template file (optional)",
)
@click.option(
"-V",
"--version",
is_flag=True,
default=False,
help=f"Output {APP_NAME} version and exit.",
)
def wireviz(file, format, prepend, output_dir, output_name, version):
def wireviz(file, format, prepend, output_dir, output_name, template_dir, version):
"""
Parses the provided FILE and generates the specified outputs.
"""
Expand Down Expand Up @@ -144,6 +151,7 @@ def wireviz(file, format, prepend, output_dir, output_name, version):
output_dir=_output_dir,
output_name=_output_name,
image_paths=list(image_paths),
template_dir=template_dir,
)

print()
Expand Down
11 changes: 10 additions & 1 deletion src/wireviz/wv_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@ def generate_html_output(
bom_list: List[List[str]],
metadata: Metadata,
options: Options,
templatedir: Union[str, Path],
):
print("Test")
# load HTML template
templatename = metadata.get("template", {}).get("name")
if templatename:
templatedirs = []

# if template directory is provided, add it to the lookup paths
if templatedir is not None:
templatedirs.append(templatedir)
# if relative path to template was provided, check directory of YAML file first, fall back to built-in template directory
templatedirs.extend([Path(filename).parent, Path(__file__).parent / "templates"])

templatefile = smart_file_resolve(
f"{templatename}.html",
[Path(filename).parent, Path(__file__).parent / "templates"],
templatedirs,
)
else:
# fall back to built-in simple template if no template was provided
Expand Down