From 4f661a4ea37edd077c801990f73b06c12fe00d0b Mon Sep 17 00:00:00 2001 From: Robby Date: Wed, 30 Oct 2024 18:33:42 -0400 Subject: [PATCH] feat: add shape links support --- README.md | 2 ++ src/py_d2/shape.py | 6 ++++++ tests/test_py_d2/test_d2_shape.py | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/README.md b/README.md index 24e31a0..2b85c82 100644 --- a/README.md +++ b/README.md @@ -65,9 +65,11 @@ See the [tests](/tests/test_py_d2) for more detailed usage examples. - [x] Markdown / block strings / code in shapes - [x] Icons in shapes - [x] Support for empty labels +- [x] Shape links - [ ] SQL table shapes - [ ] Class shapes - [ ] Comments +- [ ] Layers ## Development diff --git a/src/py_d2/shape.py b/src/py_d2/shape.py index d61e837..ecec2e7 100644 --- a/src/py_d2/shape.py +++ b/src/py_d2/shape.py @@ -78,6 +78,8 @@ def __init__( connections: Optional[List[D2Connection]] = None, # A shape this is near near: Optional[str] = None, + # A link for a shape (when clicked) + link: Optional[str] = None, **kwargs: D2Text, ): self.name = name @@ -88,6 +90,7 @@ def __init__( self.icon = icon self.connections = connections or [] self.near = near + self.link = link self.kwargs = kwargs def add_shape(self, shape: D2Shape): @@ -107,6 +110,9 @@ def lines(self) -> List[str]: if self.near: properties.append(f"near: {self.near}") + if self.link: + properties.append(f"link: {self.link}") + if self.style: properties += self.style.lines() diff --git a/tests/test_py_d2/test_d2_shape.py b/tests/test_py_d2/test_d2_shape.py index 03ecb83..5ac2c1e 100644 --- a/tests/test_py_d2/test_d2_shape.py +++ b/tests/test_py_d2/test_d2_shape.py @@ -152,6 +152,11 @@ def test_d2_shape_near(): assert str(shape) == "shape_name: {\n near: some_other_shape\n}" +def test_d2_shape_link(): + shape = D2Shape(name="shape_name", link="https://github.com/MrBlenny/py-d2") + assert str(shape) == "shape_name: {\n link: https://github.com/MrBlenny/py-d2\n}" + + def test_d2_shape_other_properties(): text = "Some text" shape = D2Shape(name="shape_name", thing=D2Text(text=text, formatting="md"))