Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
pip install setuptools wheel twine build
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
python -m build
twine upload dist/*
30 changes: 19 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,21 @@ jobs:
strategy:
matrix:
include:
- python: "3.6"
env: py36-django11
os: ubuntu-20.04 # 3.6 is not available on ubuntu-20.04
- python: "3.7"
env: py37-django11
os: ubuntu-20.04 # 3.7 is not available on ubuntu-latest
- python: "3.8"
env: py38-django11
- python: "3.9"
env: py39-django11

- python: "3.6"
env: py36-django21
os: ubuntu-20.04 # 3.6 is not available on ubuntu-20.04
- python: "3.8"
env: py38-django21
- python: "3.9"
env: py39-django21

- python: "3.6"
env: py36-django32
os: ubuntu-20.04 # 3.6 is not available on ubuntu-20.04
- python: "3.8"
env: py38-django32
- python: "3.9"
env: py39-django32
- python: "3.10"
env: py310-django32

Expand All @@ -57,6 +51,20 @@ jobs:
- python: "3.11"
env: py311-django42

- python: "3.10"
env: py310-django50
- python: "3.11"
env: py311-django50
- python: "3.12"
env: py312-django50

- python: "3.10"
env: py310-django51
- python: "3.11"
env: py311-django51
- python: "3.12"
env: py312-django51

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python }}
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@ INSTALLED_APPS = [
## Usage

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

```jinja2
{% load mermaid %}

{# for small diagrams #}
{% mermaid "graph LR; A-->B;" %}

{# for larger diagrams #}
{% startmermaid %}
graph LR
A-->B
{% endmermaid %}
```

### Mermaid version
Expand Down Expand Up @@ -65,6 +73,7 @@ the `MERMAID_THEME` variable.

```jinja2
{% mermaid "graph LR; A-->B;" "dark" %}
{% startmermaid "dark" %}graph LR; A--B;{% endmermaid %}
```

### Mermaid theme variables
Expand Down
4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
pip install build==0.9.0

# build the wheel and install it
WHEEL_NAME=$(python -m build | grep -Po "django_mermaid-.*\.whl" | tail -n 1)
pip install dist/$WHEEL_NAME
python -m build
pip install dist/*.whl
4 changes: 0 additions & 4 deletions setup.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/django_mermaid/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.7"
__version__ = "0.0.8"
40 changes: 39 additions & 1 deletion src/django_mermaid/templatetags/mermaid.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import json

from django import template
Expand All @@ -14,7 +15,9 @@
@register.simple_tag
@mark_safe
def mermaid(diagram=None, theme=None):
"""Render a mermaid diagram.
"""Render a mermaid diagram, using a simple tag.

{% mermaid "graph LR; A-->B;" "dark" %}

:param diagram: The mermaid diagram definition
:param theme: The mermaid theme to use (default, forest, dark, neutral, base). See https://mermaid.js.org/config/theming.
Expand All @@ -28,3 +31,38 @@ def mermaid(diagram=None, theme=None):
html = "<div class=\"mermaid\">%s</div><script src=\"%s\"></script>" % (diagram or "", mermaid_uri)
init_properties = {"startOnLoad": True, "theme": theme, "themeVariables": theme_variables}
return html + "<script>mermaid.initialize(%s);</script>" % json.dumps(init_properties)


@register.tag(name="startmermaid")
def startmermaid(parser, token):
"""Render a mermaid diagram, using a block tag.

{% startmermaid "dark" %}
graph LR
A-->B
{% endmermaid %}

This tag is identical to the {% mermaid %} simple tag but allows usage as a block.
That can be useful for longer diagrams. Specifying the theme is optional.
"""

bits = token.split_contents()
if len(bits) > 1:
theme = ast.literal_eval(bits[1])
else:
theme = None

nodelist = parser.parse(('endmermaid',))
parser.delete_first_token()

return MermaidNode(nodelist, theme)


class MermaidNode(template.Node):
def __init__(self, nodelist, theme):
self.nodelist = nodelist
self.theme = theme

def render(self, context):
diagram_content = self.nodelist.render(context)
return mermaid(diagram=diagram_content, theme=self.theme)
56 changes: 46 additions & 10 deletions tests/test_tag.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from os.path import exists
from os.path import join

import pytest
from django.conf import settings
from django.template import Context
from django.template import Template
Expand All @@ -17,9 +18,16 @@
site_packages = sysconfig.get_paths()["purelib"]


def test_tag_use_in_template(version):
@pytest.mark.parametrize(
"template_code",
[
"{% load mermaid %}{% mermaid content %}",
"{% load mermaid %}{% startmermaid %}{{ content|safe }}{% endmermaid %}"
]
)
def test_tag_use_in_template(version, template_code):
theme = getattr(settings, "MERMAID_THEME", DEFAULT_THEME)
template = Template("{% load mermaid %}{% mermaid content %}")
template = Template(template_code)
template = template.render(Context({"content": "graph LR; A-->B;"}))
assert template == (
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
Expand All @@ -29,8 +37,15 @@ def test_tag_use_in_template(version):


@override_settings(MERMAID_THEME="forest")
def test_tag_use_settings_theme(version):
template = Template("{% load mermaid %}{% mermaid content %}")
@pytest.mark.parametrize(
"template_code",
[
"{% load mermaid %}{% mermaid content %}",
"{% load mermaid %}{% startmermaid %}{{ content|safe }}{% endmermaid %}"
]
)
def test_tag_use_settings_theme(version, template_code):
template = Template(template_code)
template = template.render(Context({"content": "graph LR; A-->B;"}))
assert template == (
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
Expand All @@ -39,8 +54,15 @@ def test_tag_use_settings_theme(version):
)


def test_tag_use_custom_theme(version):
template = Template("{% load mermaid %}{% mermaid content \"dark\" %}")
@pytest.mark.parametrize(
"template_code",
[
"{% load mermaid %}{% mermaid content \"dark\" %}",
"{% load mermaid %}{% startmermaid \"dark\" %}{{ content|safe }}{% endmermaid %}"
]
)
def test_tag_use_custom_theme(version, template_code):
template = Template(template_code)
template = template.render(Context({"content": "graph LR; A-->B;"}))
assert template == (
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
Expand All @@ -50,8 +72,15 @@ def test_tag_use_custom_theme(version):


@override_settings(MERMAID_THEME_VARIABLES={"primaryColor": "red"})
def test_tag_use_custom_theme_variables(version):
template = Template("{% load mermaid %}{% mermaid content \"dark\" %}")
@pytest.mark.parametrize(
"template_code",
[
"{% load mermaid %}{% mermaid content \"dark\" %}",
"{% load mermaid %}{% startmermaid \"dark\" %}{{ content|safe }}{% endmermaid %}"
]
)
def test_tag_use_custom_theme_variables(version, template_code):
template = Template(template_code)
template = template.render(Context({"content": "graph LR; A-->B;"}))
assert template == (
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
Expand All @@ -61,8 +90,15 @@ def test_tag_use_custom_theme_variables(version):


@override_settings(MERMAID_THEME="base", MERMAID_THEME_VARIABLES={"primaryColor": "#efefef"})
def test_tag_use_custom_theme_variables_with_base_theme(version):
template = Template("{% load mermaid %}{% mermaid content %}")
@pytest.mark.parametrize(
"template_code",
[
"{% load mermaid %}{% mermaid content %}",
"{% load mermaid %}{% startmermaid %}{{ content|safe }}{% endmermaid %}"
]
)
def test_tag_use_custom_theme_variables_with_base_theme(version, template_code):
template = Template(template_code)
template = template.render(Context({"content": "graph LR; A-->B;"}))
assert template == (
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>"
Expand Down
13 changes: 7 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
[tox]
envlist =
py{36,38,39}-django11
py{36,38,39}-django21
py{36,38,310}-django32
py{37,38}-django11
py{38,39}-django21
py{38,39,310}-django32
py{38,39,310}-django40
py{39,310,311}-django{41,42}
py{310,311}-djangomain
py{310,311,312}-django{50,51}

[testenv]
deps =
djangomain: https://github.com/django/django/tarball/main
django51: django==5.1
django50: django==5.0
django42: django<4.3
django41: django<4.2
django40: django<4.1
django32: django<3.3
django21: django<2.2
django11: django<2.0
django11: django~=1.0
-r{toxinidir}/tests/requirements.txt
allowlist_externals = sh
commands =
Expand Down
Loading