Avoid reading diagram file to embed unless used (#371)
Add local replacement_if_used() that call function to read the file only when needed and append the return value as replacement.
This commit is contained in:
parent
ea26116c81
commit
70a33edca5
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict, List, Union
|
from typing import Callable, Dict, List, Union
|
||||||
|
|
||||||
from wireviz import APP_NAME, APP_URL, __version__, wv_colors
|
from wireviz import APP_NAME, APP_URL, __version__, wv_colors
|
||||||
from wireviz.DataClasses import Metadata, Options
|
from wireviz.DataClasses import Metadata, Options
|
||||||
@ -37,12 +37,12 @@ def generate_html_output(
|
|||||||
|
|
||||||
html = open_file_read(templatefile).read()
|
html = open_file_read(templatefile).read()
|
||||||
|
|
||||||
# embed SVG diagram
|
# embed SVG diagram (only if used)
|
||||||
with open_file_read(f"{filename}.tmp.svg") as file:
|
def svgdata() -> str:
|
||||||
svgdata = re.sub(
|
return re.sub(
|
||||||
"^<[?]xml [^?>]*[?]>[^<]*<!DOCTYPE [^>]*>",
|
"^<[?]xml [^?>]*[?]>[^<]*<!DOCTYPE [^>]*>",
|
||||||
"<!-- XML and DOCTYPE declarations from SVG file removed -->",
|
"<!-- XML and DOCTYPE declarations from SVG file removed -->",
|
||||||
file.read(),
|
open_file_read(f"{filename}.tmp.svg").read(),
|
||||||
1,
|
1,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -81,8 +81,6 @@ def generate_html_output(
|
|||||||
"<!-- %generator% -->": f"{APP_NAME} {__version__} - {APP_URL}",
|
"<!-- %generator% -->": f"{APP_NAME} {__version__} - {APP_URL}",
|
||||||
"<!-- %fontname% -->": options.fontname,
|
"<!-- %fontname% -->": options.fontname,
|
||||||
"<!-- %bgcolor% -->": wv_colors.translate_color(options.bgcolor, "hex"),
|
"<!-- %bgcolor% -->": wv_colors.translate_color(options.bgcolor, "hex"),
|
||||||
"<!-- %diagram% -->": svgdata,
|
|
||||||
"<!-- %diagram_png_base64% -->": data_URI_base64(f"{filename}.png"),
|
|
||||||
"<!-- %filename% -->": str(filename),
|
"<!-- %filename% -->": str(filename),
|
||||||
"<!-- %filename_stem% -->": Path(filename).stem,
|
"<!-- %filename_stem% -->": Path(filename).stem,
|
||||||
"<!-- %bom% -->": bom_html,
|
"<!-- %bom% -->": bom_html,
|
||||||
@ -91,6 +89,16 @@ def generate_html_output(
|
|||||||
"<!-- %sheet_total% -->": "1", # TODO: handle multi-page documents
|
"<!-- %sheet_total% -->": "1", # TODO: handle multi-page documents
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def replacement_if_used(key: str, func: Callable[[], str]) -> None:
|
||||||
|
"""Append replacement only if used in html."""
|
||||||
|
if key in html:
|
||||||
|
replacements[key] = func()
|
||||||
|
|
||||||
|
replacement_if_used("<!-- %diagram% -->", svgdata)
|
||||||
|
replacement_if_used(
|
||||||
|
"<!-- %diagram_png_base64% -->", lambda: data_URI_base64(f"{filename}.png")
|
||||||
|
)
|
||||||
|
|
||||||
# prepare metadata replacements
|
# prepare metadata replacements
|
||||||
if metadata:
|
if metadata:
|
||||||
for item, contents in metadata.items():
|
for item, contents in metadata.items():
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user