# -*- coding: utf-8 -*- import re from pathlib import Path from typing import Dict, List, Optional from wireviz import APP_NAME, APP_URL, __version__, wv_colors from wireviz.DataClasses import Metadata, Options from wireviz.wv_gv_html import html_line_breaks from wireviz.wv_helper import ( flatten2d, open_file_read, smart_file_resolve, ) def generate_html_output( svg_input: str, output_dir: Optional[Path], bom_list: List[List[str]], metadata: Metadata, options: Options, ) -> str: # load HTML template template_name = metadata.get("template", {}).get("name") builtin_template_directory = Path(__file__).parent / "templates" # built-in template directory if template_name: possible_paths = [] # if relative path to template was provided, check directory of YAML file first if output_dir is not None: possible_paths.append(output_dir) possible_paths.append(builtin_template_directory) # fallback template_file = smart_file_resolve(f"{template_name}.html", possible_paths) else: # fallback to built-in simple template if no template was provided template_file = builtin_template_directory / "simple.html" html = open_file_read(template_file).read() # embed SVG diagram svg_data = re.sub( "^<[?]xml [^?>]*[?]>[^<]*]*>", "", svg_input, 1, ) # generate BOM table bom = flatten2d(bom_list) # generate BOM header (might be at the top or bottom of the table) bom_header_html = " \n" for item in bom[0]: th_class = f"bom_col_{item.lower()}" bom_header_html = f'{bom_header_html} {item}\n' bom_header_html = f"{bom_header_html} \n" # generate BOM contents bom_contents = [] for row in bom[1:]: row_html = " \n" for i, item in enumerate(row): td_class = f"bom_col_{bom[0][i].lower()}" row_html = f'{row_html} {item}\n' row_html = f"{row_html} \n" bom_contents.append(row_html) bom_html = ( '\n' + bom_header_html + "".join(bom_contents) + "
\n" ) bom_html_reversed = ( '\n' + "".join(list(reversed(bom_contents))) + bom_header_html + "
\n" ) # prepare simple replacements replacements = { "": f"{APP_NAME} {__version__} - {APP_URL}", "": options.fontname, "": wv_colors.translate_color(options.bgcolor, "hex"), "": svg_data, "": bom_html, "": bom_html_reversed, "": "1", # TODO: handle multi-page documents "": "1", # TODO: handle multi-page documents } # prepare metadata replacements if metadata: for item, contents in metadata.items(): if isinstance(contents, (str, int, float)): replacements[f""] = html_line_breaks(str(contents)) elif isinstance(contents, Dict): # useful for authors, revisions for index, (category, entry) in enumerate(contents.items()): if isinstance(entry, Dict): replacements[f""] = str(category) for entry_key, entry_value in entry.items(): replacements[ f"" ] = html_line_breaks(str(entry_value)) replacements['"sheetsize_default"'] = '"{}"'.format( metadata.get("template", {}).get("sheetsize", "") ) # include quotes so no replacement happens within