Skip to content

Commit

Permalink
factor out texts helper for Node
Browse files Browse the repository at this point in the history
investigating styling issue in maxGraph at maxGraph/maxGraph#485
  • Loading branch information
hbmartin committed Jul 3, 2024
1 parent 51e38e5 commit 2531762
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,4 @@ test/**/*.xml
.idea/tasks.xml
.idea/modules.xml
.idea/*.iml
test/**/*.svg
Expand Down
7 changes: 4 additions & 3 deletions graphviz2drawio/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pathlib
from sys import stderr

from .graphviz2drawio import convert
Expand Down Expand Up @@ -38,9 +39,9 @@ def main() -> None:
outfile = args.outfile
else:
base = args.to_convert.split(".")
outfile = ".".join(base[:-1] + ["xml"])
with open(outfile, "w") as fd:
fd.write(output)
outfile = ".".join(base[:-1]) + ".xml"

pathlib.Path(outfile).write_text(output)
stderr.write("Converted file: " + outfile + "\n")


Expand Down
32 changes: 18 additions & 14 deletions graphviz2drawio/mx/NodeFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,7 @@ def rect_from_ellipse_svg(self, attrib):
return Rect(x=x - rx, y=y - ry, width=rx * 2, height=ry * 2)

def from_svg(self, g) -> Node:
texts = []
current_text = None
# TODO: refactor this as comprehension
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 += f"<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)
texts = self._extract_texts(g)

if SVG.has(g, "polygon"):
rect = self.rect_from_svg_points(
Expand Down Expand Up @@ -87,3 +74,20 @@ def from_svg(self, g) -> Node:
stroke=stroke,
shape=shape,
)

@staticmethod
def _extract_texts(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 += f"<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
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ line-length = 88
indent-width = 4

lint.select = ["ALL"]
lint.ignore = ["ANN001", "ANN101", "ANN201", "ANN202", "ANN205", "D100", "D101", "D102", "D103", "D104", "D105", "D107", "D203", "D213", "PLR0913", "S314", "TD002", "N999"]
lint.ignore = ["ANN001", "ANN003", "ANN101", "ANN201", "ANN202", "ANN205", "D100", "D101", "D102", "D103", "D104", "D105", "D107", "D203", "D213", "PLR0913", "S314", "TD002", "N999"]
target-version = "py312"

[tool.ruff.format]
Expand All @@ -55,7 +55,7 @@ line-ending = "auto"
"test/*" = ["ANN201", "E501", "ERA001", "FIX002", "INP001", "PLR2004", "PT011", "S101", "TD003"]
"graphviz2drawio/__main__.py" = ["T201"]
"graphviz2drawio/version.py" = ["T201"]
"doc/source/conf.py" = ["ERA001", "INP001"]
"doc/source/conf.py" = ["A001", "ERA001", "INP001"]
"graphviz2drawio/mx/Styles.py" = ["C901", "E501", "PLR0911", "PLR0912"]

[tool.pytest.ini_options]
Expand Down

0 comments on commit 2531762

Please sign in to comment.