From 29f84b2c2cca3830402385183711f2099727601a Mon Sep 17 00:00:00 2001 From: ouhammmourachid Date: Thu, 2 Nov 2023 21:30:52 +0100 Subject: [PATCH] add click functionnality to flowchart --- .gitignore | 3 ++- mermaid/flowchart/node.py | 18 +++++++++++++++++- mermaid/tests/test_flowchart.py | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 82bb546..784dc50 100644 --- a/.gitignore +++ b/.gitignore @@ -89,4 +89,5 @@ local_settings.py .env db.sqlite3 -/local/ +/local/ +.vscode diff --git a/mermaid/flowchart/node.py b/mermaid/flowchart/node.py index f2037d6..5a5adbd 100644 --- a/mermaid/flowchart/node.py +++ b/mermaid/flowchart/node.py @@ -26,17 +26,28 @@ class NodeShape: 'double-circle': NodeShape('(((', ')))'), } +HREF_TYPES: dict[str, str] = { + 'blank': '_blank', + 'self': '_self', + 'parent': '_parent', + 'top': '_top' +} + class Node: def __init__(self, id_: str, content: str = '', shape: str = 'normal', - sub_nodes: list['Node'] = None) -> None: + sub_nodes: list['Node'] = None, + href: str = None, + href_type: str = 'blank') -> None: self.id_: str = text_to_snake_case(id_) self.content: str = content if content else id_ self.shape: NodeShape = NODE_SHAPES[shape] + self.href: str = href if href is not None else '#' + self.href_type: str = HREF_TYPES[href_type] self.sub_nodes: list[ 'Node'] = sub_nodes if sub_nodes is not None else [] @@ -51,4 +62,9 @@ def __str__(self) -> str: string = ''.join([ self.id_, self.shape.start, f'"{self.content}"', self.shape.end ]) + if self.href != '#': + string = ''.join([ + string, '\n', + f'click {self.id_} "{self.href}" {self.href_type}' + ]) return string diff --git a/mermaid/tests/test_flowchart.py b/mermaid/tests/test_flowchart.py index 4cebdd8..dc815fa 100644 --- a/mermaid/tests/test_flowchart.py +++ b/mermaid/tests/test_flowchart.py @@ -38,6 +38,20 @@ def test_string_repr_for_node_with_sub_nodes(self): end""" self.assertEqual(expect_string, str(node)) + def test_string_node_with_href_type_deafult(self): + node: Node = Node('Node Name', href='www.github.com') + expect_string: str = """node_name["Node Name"] +click node_name "www.github.com" _blank""" + + self.assertEqual(expect_string, str(node)) + + def test_string_node_without_href_type_deafult(self): + node: Node = Node('Node Name', href='www.github.com', href_type='top') + expect_string: str = """node_name["Node Name"] +click node_name "www.github.com" _top""" + + self.assertEqual(expect_string, str(node)) + class TestLink(unittest.TestCase): def setUp(self) -> None: