Skip to content

Commit

Permalink
Add HTML template placeholder for diagram_png_base64
Browse files Browse the repository at this point in the history
This will enable users to replace the SVG diagram with an embedded PNG,
that is an improved work-around when the SVG output from Graphviz
is not looking good. Suggested as work-around for Graphviz bug in
#175 (comment)
  • Loading branch information
kvid committed May 31, 2024
1 parent 38995ea commit 957bb22
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
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
3 changes: 2 additions & 1 deletion src/wireviz/wv_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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 @@ -81,7 +82,7 @@ def generate_html_output(
"<!-- %fontname% -->": options.fontname,
"<!-- %bgcolor% -->": wv_colors.translate_color(options.bgcolor, "hex"),
"<!-- %diagram% -->": svgdata,
# TODO: "<!-- %diagram_png_base64% -->": base64 of png file
"<!-- %diagram_png_base64% -->": data_URI_base64(f"{filename}.png"),
"<!-- %filename% -->": str(filename),
"<!-- %filename_stem% -->": Path(filename).stem,
"<!-- %bom% -->": bom_html,
Expand Down

0 comments on commit 957bb22

Please sign in to comment.