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 HTML template placeholders for filename #371

Merged
merged 6 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 14 additions & 0 deletions src/wireviz/svgembed.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
mime_subtype_replacements = {"jpg": "jpeg", "tif": "tiff"}


# TODO: Share cache and code between data_URI_base64() and embed_svg_images()
def data_URI_base64(file: Union[str, Path], media: str = "image") -> str:
"""Return Base64-encoded data URI of input file."""
file = Path(file)
b64 = base64.b64encode(file.read_bytes()).decode("utf-8")
uri = f"data:{media}/{get_mime_subtype(file)};base64, {b64}"
# print(f"data_URI_base64('{file}', '{media}') -> {len(uri)}-character URI")
if len(uri) > 65535:
print(
"data_URI_base64(): Warning: Browsers might have different URI length limitations"
)
return uri


def embed_svg_images(svg_in: str, base_path: Union[str, Path] = Path.cwd()) -> str:
images_b64 = {} # cache of base64-encoded images

Expand Down
24 changes: 18 additions & 6 deletions src/wireviz/wv_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import re
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.DataClasses import Metadata, Options
from wireviz.svgembed import data_URI_base64
from wireviz.wv_gv_html import html_line_breaks
from wireviz.wv_helper import (
flatten2d,
Expand Down Expand Up @@ -36,12 +37,12 @@ def generate_html_output(

html = open_file_read(templatefile).read()

# embed SVG diagram
with open_file_read(f"{filename}.tmp.svg") as file:
svgdata = re.sub(
# embed SVG diagram (only if used)
def svgdata() -> str:
return re.sub(
"^<[?]xml [^?>]*[?]>[^<]*<!DOCTYPE [^>]*>",
"<!-- XML and DOCTYPE declarations from SVG file removed -->",
file.read(),
open_file_read(f"{filename}.tmp.svg").read(),
1,
)

Expand Down Expand Up @@ -80,13 +81,24 @@ def generate_html_output(
"<!-- %generator% -->": f"{APP_NAME} {__version__} - {APP_URL}",
"<!-- %fontname% -->": options.fontname,
"<!-- %bgcolor% -->": wv_colors.translate_color(options.bgcolor, "hex"),
"<!-- %diagram% -->": svgdata,
"<!-- %filename% -->": str(filename),
"<!-- %filename_stem% -->": Path(filename).stem,
"<!-- %bom% -->": bom_html,
"<!-- %bom_reversed% -->": bom_html_reversed,
"<!-- %sheet_current% -->": "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
if metadata:
for item, contents in metadata.items():
Expand Down
Loading