From 97c64ec8a12c1f083ce478a49a172d02518a3d09 Mon Sep 17 00:00:00 2001 From: Jeff Putsch Date: Sun, 7 Jul 2024 10:27:55 -0700 Subject: [PATCH] Keep text on edges in resulting draw.io output (#59) * working neo4j -> graphviz/drawio converter * bump version --------- Co-authored-by: Jeff Putsch Co-authored-by: Harold Martin --- .gitignore | 6 ++++-- graphviz2drawio/mx/Edge.py | 17 ++++++++++++++--- graphviz2drawio/mx/EdgeFactory.py | 21 +++++++++++++++++++-- graphviz2drawio/mx/MxGraph.py | 1 + 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 33b6ebc..dde7d92 100644 --- a/.gitignore +++ b/.gitignore @@ -7,10 +7,11 @@ __pycache__/ *.so # VSCode config - .vscode +.vscode # Distribution / packaging .Python +Pipfile.lock build/ develop-eggs/ dist/ @@ -113,7 +114,8 @@ venv.bak/ # Icon must end with two \r Icon? -Icon +Icon + # Thumbnails ._* diff --git a/graphviz2drawio/mx/Edge.py b/graphviz2drawio/mx/Edge.py index d887523..4b41dcd 100644 --- a/graphviz2drawio/mx/Edge.py +++ b/graphviz2drawio/mx/Edge.py @@ -13,7 +13,7 @@ def __init__( fr: str, to: str, curve: Curve | None, - label: str, + labels: str, ) -> None: super().__init__(sid=sid, gid=f"{fr}->{to}") self.fr = fr @@ -22,13 +22,23 @@ def __init__( self.line_style = None self.dir = None self.arrowtail = None - self.label = label + self.labels = labels def curve_start_end(self): if self.dir == DotAttr.BACK: return self.curve.end, self.curve.start return self.curve.start, self.curve.end + def text_to_mx_value(self): + value = "" + last_text = len(self.labels) - 1 + for i, t in enumerate(self.labels): + style = t.get_mx_style() + value += "

" + t.text + "

" + if i != last_text: + value += "
" + return value + @property def key_for_label(self) -> str: return f"{self.gid}-{self.curve}" @@ -36,5 +46,6 @@ def key_for_label(self) -> str: def __repr__(self) -> str: return ( f"{self.fr}->{self.to}: " - f"{self.label}, {self.line_style}, {self.dir}, {self.arrowtail}" + f"{self.labels}, {self.line_style}, {self.dir}, {self.arrowtail}" ) + diff --git a/graphviz2drawio/mx/EdgeFactory.py b/graphviz2drawio/mx/EdgeFactory.py index a7067bd..5c58da7 100644 --- a/graphviz2drawio/mx/EdgeFactory.py +++ b/graphviz2drawio/mx/EdgeFactory.py @@ -3,6 +3,7 @@ from ..models.Errors import MissingTitleError from .CurveFactory import CurveFactory from .Edge import Edge +from .Text import Text class EdgeFactory: @@ -10,6 +11,22 @@ def __init__(self, coords) -> None: super().__init__() self.curve_factory = CurveFactory(coords) + def _get_labels(g): + texts = [] + current_text = None + for t in g: + if SVG.is_tag(t, "text"): + if current_text is None: + current_text = Text.from_svg(t) + else: + current_text.text += "
" + t.text + elif current_text is not None: + texts.append(current_text) + current_text = None + if current_text is not None: + texts.append(current_text) + return texts + def from_svg(self, g) -> Edge: title = SVG.get_title(g) if title is None: @@ -18,8 +35,8 @@ def from_svg(self, g) -> Edge: fr = fr.split(":")[0] to = to.split(":")[0] curve = None - label = SVG.get_text(g) or "" + labels = _get_labels(g) if (path := SVG.get_first(g, "path")) is not None: if "d" in path.attrib: curve = self.curve_factory.from_svg(path.attrib["d"]) - return Edge(sid=g.attrib["id"], fr=fr, to=to, curve=curve, label=label) + return Edge(sid=g.attrib["id"], fr=fr, to=to, curve=curve, labels=labels) diff --git a/graphviz2drawio/mx/MxGraph.py b/graphviz2drawio/mx/MxGraph.py index 0e44915..ae497af 100644 --- a/graphviz2drawio/mx/MxGraph.py +++ b/graphviz2drawio/mx/MxGraph.py @@ -37,6 +37,7 @@ def add_edge(self, edge: Edge) -> None: "edge": "1", "source": source.sid, "target": target.sid, + "value": edge.text_to_mx_value(), }, )