Skip to content

Commit

Permalink
Support both bgcolor and bgcolor_title attributes
Browse files Browse the repository at this point in the history
Solves #210 completely by supporting bgcolor of both the node title
and the whole node independently using separate attributes.
  • Loading branch information
kvid committed Feb 19, 2021
1 parent e06f663 commit 5673ee4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/wireviz/DataClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def description(self) -> str:
class Connector:
name: Designator
bgcolor: Optional[Color] = None
bgcolor_title: Optional[Color] = None
manufacturer: Optional[MultilineHypertext] = None
mpn: Optional[MultilineHypertext] = None
pn: Optional[Hypertext] = None
Expand Down Expand Up @@ -169,6 +170,7 @@ def get_qty_multiplier(self, qty_multiplier: Optional[ConnectorMultiplier]) -> i
class Cable:
name: Designator
bgcolor: Optional[Color] = None
bgcolor_title: Optional[Color] = None
manufacturer: Union[MultilineHypertext, List[MultilineHypertext], None] = None
mpn: Union[MultilineHypertext, List[MultilineHypertext], None] = None
pn: Union[Hypertext, List[Hypertext], None] = None
Expand Down
13 changes: 8 additions & 5 deletions src/wireviz/Harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from wireviz import wv_colors, __version__, APP_NAME, APP_URL
from wireviz.DataClasses import Connector, Cable
from wireviz.wv_colors import get_color_hex
from wireviz.wv_gv_html import nested_html_table, html_bgcolor, html_colorbar, \
from wireviz.wv_gv_html import nested_html_table, \
html_bgcolor_attr, html_bgcolor, html_colorbar, \
html_image, html_caption, remove_links, html_line_breaks
from wireviz.wv_bom import manufacturer_info_field, component_table_entry, \
get_additional_component_table, bom_list, generate_bom
Expand Down Expand Up @@ -113,7 +114,8 @@ def create_graph(self) -> Graph:

html = []

rows = [[f'{html_bgcolor(connector.bgcolor)}{remove_links(connector.name)}' if connector.show_name else None],
rows = [[f'{html_bgcolor(connector.bgcolor_title)}{remove_links(connector.name)}'
if connector.show_name else None],
[f'P/N: {remove_links(connector.pn)}' if connector.pn else None,
html_line_breaks(manufacturer_info_field(connector.manufacturer, connector.mpn))],
[html_line_breaks(connector.type),
Expand All @@ -125,7 +127,7 @@ def create_graph(self) -> Graph:
[html_caption(connector.image)]]
rows.extend(get_additional_component_table(self, connector))
rows.append([html_line_breaks(connector.notes)])
html.extend(nested_html_table(rows))
html.extend(nested_html_table(rows, html_bgcolor_attr(connector.bgcolor)))

if connector.style != 'simple':
pinhtml = []
Expand Down Expand Up @@ -194,7 +196,8 @@ def create_graph(self) -> Graph:
elif cable.gauge_unit.upper() == 'AWG':
awg_fmt = f' ({mm2_equiv(cable.gauge)} mm\u00B2)'

rows = [[f'{html_bgcolor(cable.bgcolor)}{remove_links(cable.name)}' if cable.show_name else None],
rows = [[f'{html_bgcolor(cable.bgcolor_title)}{remove_links(cable.name)}'
if cable.show_name else None],
[f'P/N: {remove_links(cable.pn)}' if (cable.pn and not isinstance(cable.pn, list)) else None,
html_line_breaks(manufacturer_info_field(
cable.manufacturer if not isinstance(cable.manufacturer, list) else None,
Expand All @@ -211,7 +214,7 @@ def create_graph(self) -> Graph:

rows.extend(get_additional_component_table(self, cable))
rows.append([html_line_breaks(cable.notes)])
html.extend(nested_html_table(rows))
html.extend(nested_html_table(rows, html_bgcolor_attr(cable.bgcolor)))

wirehtml = []
wirehtml.append('<table border="0" cellspacing="0" cellborder="0">') # conductor table
Expand Down
12 changes: 8 additions & 4 deletions src/wireviz/wv_gv_html.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import List, Union
from typing import List, Optional, Union
import re

from wireviz.DataClasses import Color
from wireviz.wv_colors import translate_color
from wireviz.wv_helper import remove_links

def nested_html_table(rows):
def nested_html_table(rows: List[Union[str, List[Optional[str]], None]], table_attrs: str = '') -> str:
# input: list, each item may be scalar or list
# output: a parent table with one child table per parent item that is list, and one cell per parent item that is scalar
# purpose: create the appearance of one table, where cell widths are independent between rows
# attributes in any leading <tdX> inside a list are injected into to the preceeding <td> tag
html = []
html.append('<table border="0" cellspacing="0" cellpadding="0">')
html.append(f'<table border="0" cellspacing="0" cellpadding="0"{table_attrs or ""}>')
for row in rows:
if isinstance(row, List):
if len(row) > 0 and any(row):
Expand All @@ -33,9 +33,13 @@ def nested_html_table(rows):
html.append('</table>')
return html

def html_bgcolor_attr(color: Color) -> str:
"""Return attributes for bgcolor or '' if no color."""
return f' bgcolor="{translate_color(color, "HEX")}"' if color else ''

def html_bgcolor(color: Color, _extra_attr: str = '') -> str:
"""Return <td> attributes prefix for bgcolor or '' if no color."""
return f'<tdX bgcolor="{translate_color(color, "HEX")}"{_extra_attr}>' if color else ''
return f'<tdX{html_bgcolor_attr(color)}{_extra_attr}>' if color else ''

def html_colorbar(color: Color) -> str:
"""Return <tdX> attributes prefix for bgcolor and minimum width or None if no color."""
Expand Down

0 comments on commit 5673ee4

Please sign in to comment.