Skip to content

Commit

Permalink
Merge pull request #139 from ouhammmourachid/add-examples
Browse files Browse the repository at this point in the history
Update Mermaid package to include Position enum and set_position method
  • Loading branch information
ouhammmourachid authored Apr 9, 2024
2 parents 41755ec + 59c217c commit 2fd3383
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
7 changes: 5 additions & 2 deletions mermaid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""
from enum import Enum

from ._main import Mermaid
from ._main import Mermaid, Position
from ._utils import load, text_to_snake_case
from .configuration import Config
from .graph import Graph
Expand All @@ -30,4 +30,7 @@ class Direction(Enum):
BOTTOM_TO_TOP = 'BT'


__all__ = ['Mermaid', 'load', 'Direction', 'Graph', 'Style', 'Config', 'Icon']
__all__ = [
'Mermaid', 'load', 'Direction', 'Graph', 'Style', 'Config', 'Icon',
'Position'
]
31 changes: 29 additions & 2 deletions mermaid/_main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
from enum import Enum
from pathlib import Path
from typing import Union

Expand All @@ -8,6 +9,16 @@
from .graph import Graph


class Position(Enum):
"""
This class represents the position of the node in a Mermaid diagram.
"""
LEFT = 'left'
RIGHT = 'right'
CENTER = 'center'
NONE = 'none'


class Mermaid:
"""
This class represents a Mermaid diagram.
Expand All @@ -17,16 +28,30 @@ class Mermaid:
svg_response (Response): The response from the GET request to the Mermaid SVG API.
img_response (Response): The response from the GET request to the Mermaid IMG API.
"""
def __init__(self, graph: Graph):
def __init__(self,
graph: Graph,
position: Union[Position, str] = Position.NONE):
"""
The constructor for the Mermaid class.
Parameters:
graph (Graph): The Graph object containing the Mermaid diagram script.
"""
self.__position: str = position if isinstance(position,
str) else position.value
self._diagram = self._process_diagram(graph.script)
self._make_request_to_mermaid()

def set_position(self, position: Union[Position, str]) -> None:
"""
Set the position of the node in the Mermaid diagram.
Parameters:
position (Union[Position, str]): The position of the node.
"""
self.__position = position if isinstance(position,
str) else position.value

@staticmethod
def _process_diagram(diagram: str) -> str:
"""
Expand All @@ -50,7 +75,9 @@ def _repr_html_(self) -> str:
Returns:
str: The text of the SVG response.
"""
return self.svg_response.text
if self.__position == Position.NONE.value:
return self.svg_response.text
return f'<div style="text-align:{self.__position}">{self.svg_response.text}</div>'

def _make_request_to_mermaid(self) -> None:
"""
Expand Down
20 changes: 19 additions & 1 deletion mermaid/tests/test_mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest
from pathlib import Path

from mermaid import Mermaid
from mermaid import Mermaid, Position
from mermaid.graph import Graph


Expand Down Expand Up @@ -47,6 +47,24 @@ def test_to_png_on_mermaid_with_path(self):

self.assertTrue(Path.exists(output_path))

def test_repr_html_on_mermaid_with_default_position(self):
self.assertEqual(self.mermaid_object._repr_html_(),
self.mermaid_object.svg_response.text)

def test_repr_html_on_mermaid_with_str_position(self):
position = 'center'
self.mermaid_object.set_position(position)
self.assertTrue(self.mermaid_object._repr_html_().startswith(
f'<div style="text-align:{position}">'))
self.assertTrue(self.mermaid_object._repr_html_().endswith('</div>'))

def test_repr_html_on_mermaid_with_enum_position(self):
position = Position.RIGHT
self.mermaid_object.set_position(position)
self.assertTrue(self.mermaid_object._repr_html_().startswith(
f'<div style="text-align:{position.value}">'))
self.assertTrue(self.mermaid_object._repr_html_().endswith('</div>'))

def tearDown(self) -> None:
output_svg: str = f'./{self.name}.svg'
output_png: str = f'./{self.name}.png'
Expand Down

0 comments on commit 2fd3383

Please sign in to comment.