Skip to content

Commit

Permalink
Keep text on edges in resulting draw.io output (#59)
Browse files Browse the repository at this point in the history
* working neo4j -> graphviz/drawio converter

* bump version

---------

Co-authored-by: Jeff Putsch <jputsch@analog.com>
Co-authored-by: Harold Martin <harold.martin@gmail.com>
  • Loading branch information
3 people authored Jul 7, 2024
1 parent dc93163 commit 97c64ec
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ __pycache__/
*.so

# VSCode config
.vscode
.vscode

# Distribution / packaging
.Python
Pipfile.lock
build/
develop-eggs/
dist/
Expand Down Expand Up @@ -113,7 +114,8 @@ venv.bak/

# Icon must end with two \r
Icon?
Icon
Icon


# Thumbnails
._*
Expand Down
17 changes: 14 additions & 3 deletions graphviz2drawio/mx/Edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,19 +22,30 @@ 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 += "<p style='" + style + "'>" + t.text + "</p>"
if i != last_text:
value += "<hr size='1'/>"
return value

@property
def key_for_label(self) -> str:
return f"{self.gid}-{self.curve}"

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}"
)

21 changes: 19 additions & 2 deletions graphviz2drawio/mx/EdgeFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@
from ..models.Errors import MissingTitleError
from .CurveFactory import CurveFactory
from .Edge import Edge
from .Text import Text


class EdgeFactory:
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 += "<br/>" + 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:
Expand All @@ -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)
1 change: 1 addition & 0 deletions graphviz2drawio/mx/MxGraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
)

Expand Down

0 comments on commit 97c64ec

Please sign in to comment.