Skip to content

Commit

Permalink
Use regex-based replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
17o2 authored and laurierloi committed Jan 19, 2023
1 parent a80192b commit ef69d7e
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/wireviz/wv_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@ def generate_html_output(filename: Union[str, Path], bom_list: List[List[str]],

replacements['"sheetsize_default"'] = '"{}"'.format(metadata.get('template',{}).get('sheetsize', '')) # include quotes so no replacement happens within <style> definition


# perform replacements
for before, after in replacements.items():
html = html.replace(before, after)
# regex replacement adapted from:
# https://gist.github.com/bgusach/a967e0587d6e01e889fd1d776c5f3729
replacements_sorted = sorted(replacements, key=len, reverse=True) # longer replacements first, just in case
replacements_escaped = map(re.escape, replacements_sorted)
pattern = re.compile("|".join(replacements_escaped))
html = pattern.sub(lambda match: replacements[match.group(0)], html)

with open_file_write(f'{filename}.html') as file:
file.write(html)

0 comments on commit ef69d7e

Please sign in to comment.