templates: shared library for methods
This commit is contained in:
parent
e4a4e7d188
commit
f5b95772b9
@ -5,10 +5,9 @@ import re
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List, Union
|
from typing import List, Union
|
||||||
|
|
||||||
import jinja2
|
|
||||||
|
|
||||||
import wireviz # for doing wireviz.__file__
|
import wireviz # for doing wireviz.__file__
|
||||||
from wireviz.wv_dataclasses import Metadata, Options
|
from wireviz.wv_dataclasses import Metadata, Options
|
||||||
|
from wireviz.wv_templates import get_template
|
||||||
|
|
||||||
mime_subtype_replacements = {"jpg": "jpeg", "tif": "tiff"}
|
mime_subtype_replacements = {"jpg": "jpeg", "tif": "tiff"}
|
||||||
|
|
||||||
@ -57,14 +56,6 @@ def embed_svg_images_file(
|
|||||||
filename_out.replace(filename_in)
|
filename_out.replace(filename_in)
|
||||||
|
|
||||||
|
|
||||||
def get_template_html(template_name):
|
|
||||||
template_file_path = jinja2.FileSystemLoader(
|
|
||||||
Path(wireviz.__file__).parent / "templates"
|
|
||||||
)
|
|
||||||
jinja_env = jinja2.Environment(loader=template_file_path)
|
|
||||||
|
|
||||||
return jinja_env.get_template(template_name + ".html")
|
|
||||||
|
|
||||||
|
|
||||||
def generate_html_output(
|
def generate_html_output(
|
||||||
filename: Path,
|
filename: Path,
|
||||||
@ -150,22 +141,17 @@ def generate_html_output(
|
|||||||
replacements = {**replacements, **added_metadata}
|
replacements = {**replacements, **added_metadata}
|
||||||
|
|
||||||
# prepare BOM
|
# prepare BOM
|
||||||
bom_template = get_template_html("bom")
|
replacements["bom"] = get_template("bom.html").render(replacements)
|
||||||
replacements["bom"] = bom_template.render(replacements)
|
|
||||||
|
|
||||||
# prepare titleblock
|
# prepare titleblock
|
||||||
titleblock_template = get_template_html("titleblock")
|
replacements["titleblock"] = get_template("titleblock.html").render(replacements)
|
||||||
replacements["titleblock"] = titleblock_template.render(replacements)
|
|
||||||
|
|
||||||
# preparate Notes
|
# preparate Notes
|
||||||
if "notes" in replacements:
|
if "notes" in replacements:
|
||||||
notes_template = get_template_html("notes")
|
replacements["notes"] = get_template("notes.html").render(replacements)
|
||||||
replacements["notes"] = notes_template.render(replacements)
|
|
||||||
|
|
||||||
|
|
||||||
# generate page template
|
# generate page template
|
||||||
page_template = get_template_html(template_name)
|
page_rendered = get_template(template_name, ".html").render(replacements)
|
||||||
page_rendered = page_template.render(replacements)
|
|
||||||
|
|
||||||
# save generated file
|
# save generated file
|
||||||
filename.with_suffix(".html").open("w").write(page_rendered)
|
filename.with_suffix(".html").open("w").write(page_rendered)
|
||||||
|
|||||||
11
src/wireviz/wv_templates.py
Normal file
11
src/wireviz/wv_templates.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
import jinja2
|
||||||
|
|
||||||
|
def get_template(template_name, extension=""):
|
||||||
|
template_file_path = jinja2.FileSystemLoader(
|
||||||
|
Path(__file__).parent / "templates"
|
||||||
|
)
|
||||||
|
jinja_env = jinja2.Environment(loader=template_file_path)
|
||||||
|
|
||||||
|
return jinja_env.get_template(template_name + extension)
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user