forked from oruelle/md_mermaid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
md_mermaid.py
96 lines (75 loc) · 3.25 KB
/
md_mermaid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Mermaid Extension for Python-Markdown
========================================
Adds mermaid parser (like github-markdown) to standard Python-Markdown code blocks.
Original code Copyright 2018-2020 [Olivier Ruelle].
License: GNU GPLv3
"""
from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor
import re
import string
def strip_notprintable(myStr):
return ''.join(filter(lambda x: x in string.printable, myStr))
MermaidRegex = re.compile(r"^(?P<mermaid_sign>[\~\`]){3}[\ \t]*[Mm]ermaid[\ \t]*$")
# ------------------ The Markdown Extension -------------------------------
class MermaidPreprocessor(Preprocessor):
def run(self, lines):
old_line = ""
new_lines = []
mermaid_sign = ""
m_start = None
m_end = None
in_mermaid_code = False
is_mermaid = False
for line in lines:
# Wait for starting line with MermaidRegex (~~~ or ``` following by [mM]ermaid )
if not in_mermaid_code:
m_start = MermaidRegex.match(line)
else:
m_end = re.match(r"^["+mermaid_sign+"]{3}[\ \t]*$", line)
if m_end:
in_mermaid_code = False
if m_start:
in_mermaid_code = True
mermaid_sign = m_start.group("mermaid_sign")
if not re.match(r"^[\ \t]*$", old_line):
new_lines.append("")
if not is_mermaid:
is_mermaid = True
#new_lines.append('<style type="text/css"> @import url("https://cdn.rawgit.com/knsv/mermaid/0.5.8/dist/mermaid.css"); </style>')
new_lines.append('<div class="mermaid">')
m_start = None
elif m_end:
new_lines.append('</div>')
new_lines.append("")
m_end = None
elif in_mermaid_code:
new_lines.append(strip_notprintable(line).strip())
else:
new_lines.append(line)
old_line = line
if is_mermaid:
new_lines.append('')
# This will initialize mermaid renderer. It's done only when the HTML document is ready,
# to ensure the loading of mermaid.js file is finished.
new_lines.append('''<script>
function initializeMermaid() {
mermaid.initialize({startOnLoad:true})
}
if (document.readyState === "complete" || document.readyState === "interactive") {
setTimeout(initializeMermaid, 1);
} else {
document.addEventListener("DOMContentLoaded", initializeMermaid);
}
</script>''')
return new_lines
class MermaidExtension(Extension):
""" Add source code hilighting to markdown codeblocks. """
def extendMarkdown(self, md, md_globals):
""" Add HilitePostprocessor to Markdown instance. """
# Insert a preprocessor before ReferencePreprocessor
md.preprocessors.register(MermaidPreprocessor(md), 'mermaid', 35)
md.registerExtension(self)
def makeExtension(**kwargs): # pragma: no cover
return MermaidExtension(**kwargs)