-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom versions of mermaid (GH-9)
- Loading branch information
Showing
8 changed files
with
56 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.0.2" | ||
__version__ = "0.0.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
from distutils import dir_util | ||
from os.path import dirname, join, exists | ||
from os import stat | ||
from os.path import dirname | ||
from os.path import exists | ||
from os.path import join | ||
from urllib.request import urlretrieve | ||
|
||
from django.apps import AppConfig | ||
|
||
from .templatetags import MERMAID_VERSION | ||
|
||
|
||
class MermaidConfig(AppConfig): | ||
name = "django_mermaid" | ||
|
||
def ready(self): | ||
"""Download mermaid.js from CDN if not already present""" | ||
cdn = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/9.4.3/mermaid.js" | ||
static = join(dirname(__file__), "static") | ||
if not exists(join(static, "mermaid.js")): | ||
dir_util.create_tree(static, ["mermaid.js"]) | ||
urlretrieve(cdn, join(static, "mermaid.js")) | ||
cdn = "https://cdnjs.cloudflare.com/ajax/libs/mermaid/%s/mermaid.min.js" % MERMAID_VERSION | ||
static_dir = join(dirname(__file__), "static") | ||
mermaid_dir = join(static_dir, "mermaid", MERMAID_VERSION) | ||
if not exists(join(mermaid_dir, "mermaid.js")) or \ | ||
stat(join(mermaid_dir, "mermaid.js")).st_size == 0: | ||
dir_util.create_tree(mermaid_dir, ["mermaid.js"]) | ||
urlretrieve(cdn, join(mermaid_dir, "mermaid.js")) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.conf import settings | ||
|
||
MERMAID_VERSION = getattr( | ||
settings, | ||
"MERMAID_VERSION", | ||
"9.4.3", # default to latest stable version | ||
) | ||
|
||
__all__ = ["MERMAID_VERSION"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,39 @@ | ||
from os.path import dirname | ||
from os.path import exists | ||
from os.path import join | ||
|
||
from django.template import Context | ||
from django.template import Template | ||
|
||
from django_mermaid.templatetags import MERMAID_VERSION | ||
from django_mermaid.templatetags.mermaid import mermaid | ||
|
||
|
||
def test_tag_renders(): | ||
assert mermaid("graph LR; A-->B;") == ( | ||
"""<div class="mermaid">graph LR; A-->B;</div><script src="mermaid.js"></script>""" | ||
"""<script>mermaid.initialize({"startOnLoad": true, theme: "default"});</script>""" | ||
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>" | ||
"<script>mermaid.initialize({\"startOnLoad\": true, theme: \"default\"});</script>" % MERMAID_VERSION | ||
) | ||
|
||
|
||
def test_tag_use_in_template(): | ||
template = Template("{% load mermaid %}{% mermaid content %}") | ||
template = template.render(Context({"content": "graph LR; A-->B;"})) | ||
assert template == ( | ||
"""<div class="mermaid">graph LR; A-->B;</div><script src="mermaid.js"></script>""" | ||
"""<script>mermaid.initialize({"startOnLoad": true, theme: "default"});</script>""" | ||
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>" | ||
"<script>mermaid.initialize({\"startOnLoad\": true, theme: \"default\"});</script>" % MERMAID_VERSION | ||
) | ||
|
||
|
||
def test_tag_use_in_template_with_arguments(): | ||
template = Template("{% load mermaid %}{% mermaid content \"forest\" %}") | ||
template = template.render(Context({"content": "graph LR; A-->B;"})) | ||
assert template == ( | ||
"""<div class="mermaid">graph LR; A-->B;</div><script src="mermaid.js"></script>""" | ||
"""<script>mermaid.initialize({"startOnLoad": true, theme: "forest"});</script>""" | ||
"<div class=\"mermaid\">graph LR; A-->B;</div><script src=\"mermaid/%s/mermaid.js\"></script>" | ||
"<script>mermaid.initialize({\"startOnLoad\": true, theme: \"forest\"});</script>" % MERMAID_VERSION | ||
) | ||
|
||
|
||
def test_tag_use_custom_version(): | ||
static_dir = join(dirname(__file__), "..", "src", "django_mermaid", "static") | ||
assert exists(join(static_dir, "mermaid", MERMAID_VERSION, "mermaid.js")) |