Skip to content

Commit

Permalink
Merge branch 'master' of github.com:i2mint/titbit
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwhalen committed Nov 10, 2023
2 parents 4572e9a + a11d591 commit 0f0671f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
root_url = https://github.com/i2mint/
license = apache-2.0
author = OtoSense
version = 0.0.1
version = 0.1.0
description = A place to dump might-be-useful-again code as an alternative of leaving in a notebook where it will never be found again
description_file = README.md
long_description = file:README.md
Expand All @@ -17,5 +17,6 @@ packages = find:
include_package_data = True
zip_safe = False
install_requires =
lkj


32 changes: 24 additions & 8 deletions titbit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,25 @@ def _git_action(project):


def mermaid_to_graphviz(
mermaid_code, extra_replacements=(), *, prefix="", suffix="", egress=None
mermaid_code, extra_replacements=(), *, prefix='', suffix='', egress=None
):
"""Converts mermaid code to graphviz code."""
"""Converts mermaid code to graphviz code.
>>> mermaid_code = '''
... graph TD
... A --> B & C
... B & C --> D
... '''
>>> graphviz_code = mermaid_to_graphviz(mermaid_code)
>>> print(graphviz_code) # doctest: +NORMALIZE_WHITESPACE
digraph G {
<BLANKLINE>
graph TD
A -> B , C
B , C -> D
<BLANKLINE>
}
"""

from lkj import regex_based_substitution, import_object

Expand All @@ -79,20 +95,20 @@ def mermaid_to_graphviz(
elif isinstance(egress, str):
egress = import_object(egress)
else:
assert callable(egress), f"egress must be a callable or a string, not {egress}"
assert callable(egress), f'egress must be a callable or a string, not {egress}'

mermaid_to_graphviz_replacements = (
("-->", "->"),
("&", ","),
('-->', '->'),
('&', ','),
)
mermaid_to_graphviz_replacements = mermaid_to_graphviz_replacements + tuple(
extra_replacements
)
s = mermaid_code
# remove the first line if it starts with 'graph'
s = "\n".join(s.split("\n")[1:]) if s.startswith("graph") else s
s = '\n'.join(s.split('\n')[1:]) if s.startswith('graph') else s
# carry out the replacements
s = regex_based_substitution(mermaid_to_graphviz_replacements)(s)
# add the prefix and suffix and wrap it in the graphviz graph declaration
s = "digraph G {" + "\n" + f"{prefix}" + s + "\n" + suffix
return s + "}"
s = 'digraph G {' + '\n' + f'{prefix}' + s + '\n' + suffix
return s + '}'

0 comments on commit 0f0671f

Please sign in to comment.