Skip to content

Commit b49b51f

Browse files
Implement startmermaid block template tag
Co-authored-by: Maik Sprenger <[email protected]>
2 parents 23c9c27 + 361c4c5 commit b49b51f

File tree

9 files changed

+126
-38
lines changed

9 files changed

+126
-38
lines changed

.github/workflows/publish.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ jobs:
1818
- name: Install dependencies
1919
run: |
2020
python -m pip install --upgrade pip
21-
pip install setuptools wheel twine
21+
pip install setuptools wheel twine build
2222
- name: Build and publish
2323
env:
2424
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
2525
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
2626
run: |
27-
python setup.py sdist bdist_wheel
27+
python -m build
2828
twine upload dist/*

.github/workflows/tests.yml

+19-11
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,21 @@ jobs:
1212
strategy:
1313
matrix:
1414
include:
15-
- python: "3.6"
16-
env: py36-django11
17-
os: ubuntu-20.04 # 3.6 is not available on ubuntu-20.04
15+
- python: "3.7"
16+
env: py37-django11
17+
os: ubuntu-20.04 # 3.7 is not available on ubuntu-latest
1818
- python: "3.8"
1919
env: py38-django11
20-
- python: "3.9"
21-
env: py39-django11
2220

23-
- python: "3.6"
24-
env: py36-django21
25-
os: ubuntu-20.04 # 3.6 is not available on ubuntu-20.04
2621
- python: "3.8"
2722
env: py38-django21
2823
- python: "3.9"
2924
env: py39-django21
3025

31-
- python: "3.6"
32-
env: py36-django32
33-
os: ubuntu-20.04 # 3.6 is not available on ubuntu-20.04
3426
- python: "3.8"
3527
env: py38-django32
28+
- python: "3.9"
29+
env: py39-django32
3630
- python: "3.10"
3731
env: py310-django32
3832

@@ -57,6 +51,20 @@ jobs:
5751
- python: "3.11"
5852
env: py311-django42
5953

54+
- python: "3.10"
55+
env: py310-django50
56+
- python: "3.11"
57+
env: py311-django50
58+
- python: "3.12"
59+
env: py312-django50
60+
61+
- python: "3.10"
62+
env: py310-django51
63+
- python: "3.11"
64+
env: py311-django51
65+
- python: "3.12"
66+
env: py312-django51
67+
6068
steps:
6169
- uses: actions/checkout@v2
6270
- name: Set up Python ${{ matrix.python }}

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,19 @@ INSTALLED_APPS = [
2929
## Usage
3030

3131
After you configure the `INSTALLED_APPS`, you will be able to load the `mermaid` in your template and use the template
32-
tag for rendering mermaid diagrams.
32+
tags for rendering mermaid diagrams.
3333

3434
```jinja2
3535
{% load mermaid %}
36+
37+
{# for small diagrams #}
3638
{% mermaid "graph LR; A-->B;" %}
39+
40+
{# for larger diagrams #}
41+
{% startmermaid %}
42+
graph LR
43+
A-->B
44+
{% endmermaid %}
3745
```
3846

3947
### Mermaid version
@@ -65,6 +73,7 @@ the `MERMAID_THEME` variable.
6573

6674
```jinja2
6775
{% mermaid "graph LR; A-->B;" "dark" %}
76+
{% startmermaid "dark" %}graph LR; A--B;{% endmermaid %}
6877
```
6978

7079
### Mermaid theme variables

build.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
pip install build==0.9.0
55

66
# build the wheel and install it
7-
WHEEL_NAME=$(python -m build | grep -Po "django_mermaid-.*\.whl" | tail -n 1)
8-
pip install dist/$WHEEL_NAME
7+
python -m build
8+
pip install dist/*.whl

setup.py

-4
This file was deleted.

src/django_mermaid/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.7"
1+
__version__ = "0.0.8"

src/django_mermaid/templatetags/mermaid.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ast
12
import json
23

34
from django import template
@@ -14,7 +15,9 @@
1415
@register.simple_tag
1516
@mark_safe
1617
def mermaid(diagram=None, theme=None):
17-
"""Render a mermaid diagram.
18+
"""Render a mermaid diagram, using a simple tag.
19+
20+
{% mermaid "graph LR; A-->B;" "dark" %}
1821
1922
:param diagram: The mermaid diagram definition
2023
:param theme: The mermaid theme to use (default, forest, dark, neutral, base). See https://mermaid.js.org/config/theming.
@@ -28,3 +31,38 @@ def mermaid(diagram=None, theme=None):
2831
html = "<div class=\"mermaid\">%s</div><script src=\"%s\"></script>" % (diagram or "", mermaid_uri)
2932
init_properties = {"startOnLoad": True, "theme": theme, "themeVariables": theme_variables}
3033
return html + "<script>mermaid.initialize(%s);</script>" % json.dumps(init_properties)
34+
35+
36+
@register.tag(name="startmermaid")
37+
def startmermaid(parser, token):
38+
"""Render a mermaid diagram, using a block tag.
39+
40+
{% startmermaid "dark" %}
41+
graph LR
42+
A-->B
43+
{% endmermaid %}
44+
45+
This tag is identical to the {% mermaid %} simple tag but allows usage as a block.
46+
That can be useful for longer diagrams. Specifying the theme is optional.
47+
"""
48+
49+
bits = token.split_contents()
50+
if len(bits) > 1:
51+
theme = ast.literal_eval(bits[1])
52+
else:
53+
theme = None
54+
55+
nodelist = parser.parse(('endmermaid',))
56+
parser.delete_first_token()
57+
58+
return MermaidNode(nodelist, theme)
59+
60+
61+
class MermaidNode(template.Node):
62+
def __init__(self, nodelist, theme):
63+
self.nodelist = nodelist
64+
self.theme = theme
65+
66+
def render(self, context):
67+
diagram_content = self.nodelist.render(context)
68+
return mermaid(diagram=diagram_content, theme=self.theme)

tests/test_tag.py

+46-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from os.path import exists
22
from os.path import join
33

4+
import pytest
45
from django.conf import settings
56
from django.template import Context
67
from django.template import Template
@@ -17,9 +18,16 @@
1718
site_packages = sysconfig.get_paths()["purelib"]
1819

1920

20-
def test_tag_use_in_template(version):
21+
@pytest.mark.parametrize(
22+
"template_code",
23+
[
24+
"{% load mermaid %}{% mermaid content %}",
25+
"{% load mermaid %}{% startmermaid %}{{ content|safe }}{% endmermaid %}"
26+
]
27+
)
28+
def test_tag_use_in_template(version, template_code):
2129
theme = getattr(settings, "MERMAID_THEME", DEFAULT_THEME)
22-
template = Template("{% load mermaid %}{% mermaid content %}")
30+
template = Template(template_code)
2331
template = template.render(Context({"content": "graph LR; A-->B;"}))
2432
assert template == (
2533
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
@@ -29,8 +37,15 @@ def test_tag_use_in_template(version):
2937

3038

3139
@override_settings(MERMAID_THEME="forest")
32-
def test_tag_use_settings_theme(version):
33-
template = Template("{% load mermaid %}{% mermaid content %}")
40+
@pytest.mark.parametrize(
41+
"template_code",
42+
[
43+
"{% load mermaid %}{% mermaid content %}",
44+
"{% load mermaid %}{% startmermaid %}{{ content|safe }}{% endmermaid %}"
45+
]
46+
)
47+
def test_tag_use_settings_theme(version, template_code):
48+
template = Template(template_code)
3449
template = template.render(Context({"content": "graph LR; A-->B;"}))
3550
assert template == (
3651
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
@@ -39,8 +54,15 @@ def test_tag_use_settings_theme(version):
3954
)
4055

4156

42-
def test_tag_use_custom_theme(version):
43-
template = Template("{% load mermaid %}{% mermaid content \"dark\" %}")
57+
@pytest.mark.parametrize(
58+
"template_code",
59+
[
60+
"{% load mermaid %}{% mermaid content \"dark\" %}",
61+
"{% load mermaid %}{% startmermaid \"dark\" %}{{ content|safe }}{% endmermaid %}"
62+
]
63+
)
64+
def test_tag_use_custom_theme(version, template_code):
65+
template = Template(template_code)
4466
template = template.render(Context({"content": "graph LR; A-->B;"}))
4567
assert template == (
4668
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
@@ -50,8 +72,15 @@ def test_tag_use_custom_theme(version):
5072

5173

5274
@override_settings(MERMAID_THEME_VARIABLES={"primaryColor": "red"})
53-
def test_tag_use_custom_theme_variables(version):
54-
template = Template("{% load mermaid %}{% mermaid content \"dark\" %}")
75+
@pytest.mark.parametrize(
76+
"template_code",
77+
[
78+
"{% load mermaid %}{% mermaid content \"dark\" %}",
79+
"{% load mermaid %}{% startmermaid \"dark\" %}{{ content|safe }}{% endmermaid %}"
80+
]
81+
)
82+
def test_tag_use_custom_theme_variables(version, template_code):
83+
template = Template(template_code)
5584
template = template.render(Context({"content": "graph LR; A-->B;"}))
5685
assert template == (
5786
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
@@ -61,8 +90,15 @@ def test_tag_use_custom_theme_variables(version):
6190

6291

6392
@override_settings(MERMAID_THEME="base", MERMAID_THEME_VARIABLES={"primaryColor": "#efefef"})
64-
def test_tag_use_custom_theme_variables_with_base_theme(version):
65-
template = Template("{% load mermaid %}{% mermaid content %}")
93+
@pytest.mark.parametrize(
94+
"template_code",
95+
[
96+
"{% load mermaid %}{% mermaid content %}",
97+
"{% load mermaid %}{% startmermaid %}{{ content|safe }}{% endmermaid %}"
98+
]
99+
)
100+
def test_tag_use_custom_theme_variables_with_base_theme(version, template_code):
101+
template = Template(template_code)
66102
template = template.render(Context({"content": "graph LR; A-->B;"}))
67103
assert template == (
68104
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"

tox.ini

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
[tox]
22
envlist =
3-
py{36,38,39}-django11
4-
py{36,38,39}-django21
5-
py{36,38,310}-django32
3+
py{37,38}-django11
4+
py{38,39}-django21
5+
py{38,39,310}-django32
66
py{38,39,310}-django40
77
py{39,310,311}-django{41,42}
8-
py{310,311}-djangomain
8+
py{310,311,312}-django{50,51}
99

1010
[testenv]
1111
deps =
12-
djangomain: https://github.com/django/django/tarball/main
12+
django51: django==5.1
13+
django50: django==5.0
1314
django42: django<4.3
1415
django41: django<4.2
1516
django40: django<4.1
1617
django32: django<3.3
1718
django21: django<2.2
18-
django11: django<2.0
19+
django11: django~=1.0
1920
-r{toxinidir}/tests/requirements.txt
2021
allowlist_externals = sh
2122
commands =

0 commit comments

Comments
 (0)