provide markdown extension to render graphviz
this is server side, and a more standard format, and thus I like it more than mermaid, which I've been using at work. but, I really wanted a server-side option (see my manifesto) for drawing relationship graphs, for D&D stuff of all things. this adds an optional 'graphviz' feature to package installation which consequently depends on pydot
This commit is contained in:
parent
1583e3be99
commit
da055acda6
1
incorporealcms/mdx/__init__.py
Normal file
1
incorporealcms/mdx/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
"""Markdown extensions."""
|
46
incorporealcms/mdx/pydot.py
Normal file
46
incorporealcms/mdx/pydot.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
"""Serve dot diagrams inline."""
|
||||||
|
import base64
|
||||||
|
import re
|
||||||
|
|
||||||
|
import markdown
|
||||||
|
import pydot
|
||||||
|
|
||||||
|
|
||||||
|
class InlinePydot(markdown.Extension):
|
||||||
|
"""Wrap the markdown prepcoressor."""
|
||||||
|
|
||||||
|
def extendMarkdown(self, md, md_globals):
|
||||||
|
"""Add InlinePydotPreprocessor to the Markdown instance."""
|
||||||
|
md.registerExtension(self)
|
||||||
|
md.preprocessors.add('dot_block', InlinePydotPreprocessor(md), '_begin')
|
||||||
|
|
||||||
|
|
||||||
|
class InlinePydotPreprocessor(markdown.preprocessors.Preprocessor):
|
||||||
|
"""Identify dot codeblocks and run them through pydot."""
|
||||||
|
|
||||||
|
BLOCK_RE = re.compile(r'~~~pydot:(?P<filename>[^\s]+)\n(?P<content>.*?)~~~', re.DOTALL)
|
||||||
|
|
||||||
|
def run(self, lines):
|
||||||
|
"""Match and generate diagrams from dot code blocks."""
|
||||||
|
text = '\n'.join(lines)
|
||||||
|
for match in self.BLOCK_RE.finditer(text):
|
||||||
|
filename = match.group(1)
|
||||||
|
dot_string = match.group(2)
|
||||||
|
|
||||||
|
# use pydot to turn the text into pydot
|
||||||
|
graphs = pydot.graph_from_dot_data(dot_string)
|
||||||
|
|
||||||
|
# encode the image and provide as an inline image in markdown
|
||||||
|
encoded_image = base64.b64encode(graphs[0].create_png()).decode('ascii')
|
||||||
|
data_path = f'data:image/png;base64,{encoded_image}'
|
||||||
|
inline_image = f''
|
||||||
|
|
||||||
|
# replace the image in the output markdown
|
||||||
|
text = f'{text[:match.start()]}\n{inline_image}\n{text[match.end():]}'
|
||||||
|
|
||||||
|
return text.split('\n')
|
||||||
|
|
||||||
|
|
||||||
|
def makeExtension(*args, **kwargs):
|
||||||
|
"""Provide the extension to the markdown extension loader."""
|
||||||
|
return InlinePydot(*args, **kwargs)
|
@ -1,6 +1,7 @@
|
|||||||
-r requirements.in
|
-r requirements.in
|
||||||
|
|
||||||
# testing runner, test reporting, packages used during testing (e.g. requests-mock), etc.
|
# testing runner, test reporting, packages used during testing (e.g. requests-mock), etc.
|
||||||
|
pydot
|
||||||
pytest
|
pytest
|
||||||
pytest-cov
|
pytest-cov
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#
|
#
|
||||||
appdirs==1.4.4
|
appdirs==1.4.4
|
||||||
# via virtualenv
|
# via virtualenv
|
||||||
attrs==20.3.0
|
attrs==21.2.0
|
||||||
# via pytest
|
# via pytest
|
||||||
bandit==1.6.2
|
bandit==1.6.2
|
||||||
# via -r requirements/requirements-dev.in
|
# via -r requirements/requirements-dev.in
|
||||||
@ -18,7 +18,7 @@ click==7.1.2
|
|||||||
# pip-tools
|
# pip-tools
|
||||||
coverage==5.5
|
coverage==5.5
|
||||||
# via pytest-cov
|
# via pytest-cov
|
||||||
distlib==0.3.1
|
distlib==0.3.2
|
||||||
# via virtualenv
|
# via virtualenv
|
||||||
dlint==0.11.0
|
dlint==0.11.0
|
||||||
# via -r requirements/requirements-dev.in
|
# via -r requirements/requirements-dev.in
|
||||||
@ -42,7 +42,7 @@ flake8-logging-format==0.6.0
|
|||||||
# via -r requirements/requirements-dev.in
|
# via -r requirements/requirements-dev.in
|
||||||
flake8-mutable==1.2.0
|
flake8-mutable==1.2.0
|
||||||
# via -r requirements/requirements-dev.in
|
# via -r requirements/requirements-dev.in
|
||||||
flake8==3.9.1
|
flake8==3.9.2
|
||||||
# via
|
# via
|
||||||
# -r requirements/requirements-dev.in
|
# -r requirements/requirements-dev.in
|
||||||
# dlint
|
# dlint
|
||||||
@ -55,11 +55,11 @@ flask==1.1.2
|
|||||||
# via -r requirements/requirements.in
|
# via -r requirements/requirements.in
|
||||||
gitdb==4.0.7
|
gitdb==4.0.7
|
||||||
# via gitpython
|
# via gitpython
|
||||||
gitpython==3.1.14
|
gitpython==3.1.18
|
||||||
# via bandit
|
# via bandit
|
||||||
iniconfig==1.1.1
|
iniconfig==1.1.1
|
||||||
# via pytest
|
# via pytest
|
||||||
isort==5.8.0
|
isort==5.9.1
|
||||||
# via flake8-isort
|
# via flake8-isort
|
||||||
itsdangerous==1.1.0
|
itsdangerous==1.1.0
|
||||||
# via flask
|
# via flask
|
||||||
@ -80,11 +80,11 @@ packaging==20.9
|
|||||||
# bleach
|
# bleach
|
||||||
# pytest
|
# pytest
|
||||||
# tox
|
# tox
|
||||||
pbr==5.5.1
|
pbr==5.6.0
|
||||||
# via stevedore
|
# via stevedore
|
||||||
pep517==0.10.0
|
pep517==0.10.0
|
||||||
# via pip-tools
|
# via pip-tools
|
||||||
pip-tools==6.1.0
|
pip-tools==6.2.0
|
||||||
# via -r requirements/requirements-dev.in
|
# via -r requirements/requirements-dev.in
|
||||||
pluggy==0.13.1
|
pluggy==0.13.1
|
||||||
# via
|
# via
|
||||||
@ -96,15 +96,19 @@ py==1.10.0
|
|||||||
# tox
|
# tox
|
||||||
pycodestyle==2.7.0
|
pycodestyle==2.7.0
|
||||||
# via flake8
|
# via flake8
|
||||||
pydocstyle==6.0.0
|
pydocstyle==6.1.1
|
||||||
# via flake8-docstrings
|
# via flake8-docstrings
|
||||||
|
pydot==1.4.2
|
||||||
|
# via -r requirements/requirements-dev.in
|
||||||
pyflakes==2.3.1
|
pyflakes==2.3.1
|
||||||
# via flake8
|
# via flake8
|
||||||
pyparsing==2.4.7
|
pyparsing==2.4.7
|
||||||
# via packaging
|
# via
|
||||||
pytest-cov==2.11.1
|
# packaging
|
||||||
|
# pydot
|
||||||
|
pytest-cov==2.12.1
|
||||||
# via -r requirements/requirements-dev.in
|
# via -r requirements/requirements-dev.in
|
||||||
pytest==6.2.3
|
pytest==6.2.4
|
||||||
# via
|
# via
|
||||||
# -r requirements/requirements-dev.in
|
# -r requirements/requirements-dev.in
|
||||||
# pytest-cov
|
# pytest-cov
|
||||||
@ -130,10 +134,11 @@ toml==0.10.2
|
|||||||
# via
|
# via
|
||||||
# pep517
|
# pep517
|
||||||
# pytest
|
# pytest
|
||||||
|
# pytest-cov
|
||||||
# tox
|
# tox
|
||||||
tox-wheel==0.6.0
|
tox-wheel==0.6.0
|
||||||
# via -r requirements/requirements-dev.in
|
# via -r requirements/requirements-dev.in
|
||||||
tox==3.23.0
|
tox==3.23.1
|
||||||
# via
|
# via
|
||||||
# -r requirements/requirements-dev.in
|
# -r requirements/requirements-dev.in
|
||||||
# tox-wheel
|
# tox-wheel
|
||||||
@ -141,14 +146,16 @@ tzlocal==2.1
|
|||||||
# via -r requirements/requirements.in
|
# via -r requirements/requirements.in
|
||||||
versioneer==0.19
|
versioneer==0.19
|
||||||
# via -r requirements/requirements-dev.in
|
# via -r requirements/requirements-dev.in
|
||||||
virtualenv==20.4.3
|
virtualenv==20.4.7
|
||||||
# via tox
|
# via tox
|
||||||
webencodings==0.5.1
|
webencodings==0.5.1
|
||||||
# via bleach
|
# via bleach
|
||||||
werkzeug==1.0.1
|
werkzeug==1.0.1
|
||||||
# via flask
|
# via flask
|
||||||
wheel==0.36.2
|
wheel==0.36.2
|
||||||
# via tox-wheel
|
# via
|
||||||
|
# pip-tools
|
||||||
|
# tox-wheel
|
||||||
|
|
||||||
# The following packages are considered to be unsafe in a requirements file:
|
# The following packages are considered to be unsafe in a requirements file:
|
||||||
# pip
|
# pip
|
||||||
|
3
setup.py
3
setup.py
@ -27,4 +27,7 @@ setup(
|
|||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
zip_safe=False,
|
zip_safe=False,
|
||||||
install_requires=extract_requires(),
|
install_requires=extract_requires(),
|
||||||
|
extras_require={
|
||||||
|
'graphviz': ['pydot'],
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
29
tests/functional_markdown_tests.py
Normal file
29
tests/functional_markdown_tests.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
"""Test graphviz functionality."""
|
||||||
|
import os
|
||||||
|
|
||||||
|
from incorporealcms import create_app
|
||||||
|
|
||||||
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
|
||||||
|
def app_with_pydot():
|
||||||
|
"""Create the test app, including the pydot extension."""
|
||||||
|
return create_app(instance_path=os.path.join(HERE, 'instance'),
|
||||||
|
test_config={'MARKDOWN_EXTENSIONS': ['incorporealcms.mdx.pydot']})
|
||||||
|
|
||||||
|
|
||||||
|
def test_functional_initialization():
|
||||||
|
"""Test initialization with the graphviz config."""
|
||||||
|
app = app_with_pydot()
|
||||||
|
assert app is not None
|
||||||
|
|
||||||
|
|
||||||
|
def test_graphviz_is_rendered():
|
||||||
|
"""Initialize the app with the graphviz extension and ensure it does something."""
|
||||||
|
app = app_with_pydot()
|
||||||
|
client = app.test_client()
|
||||||
|
|
||||||
|
response = client.get('/test-graphviz')
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert b'~~~pydot' not in response.data
|
||||||
|
assert b'data:image/png;base64' in response.data
|
12
tests/instance/pages/test-graphviz.md
Normal file
12
tests/instance/pages/test-graphviz.md
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# test
|
||||||
|
|
||||||
|
test
|
||||||
|
~~~pydot:attack-plan
|
||||||
|
digraph G {
|
||||||
|
rankdir=LR
|
||||||
|
Earth
|
||||||
|
Mars
|
||||||
|
Earth -> Mars
|
||||||
|
}
|
||||||
|
~~~
|
||||||
|
more test
|
Loading…
x
Reference in New Issue
Block a user