handle graphviz parsing errors more cleanly

This commit is contained in:
2021-06-24 11:37:57 -05:00
parent 41a53a2a13
commit e61c55bed2
4 changed files with 30 additions and 0 deletions

View File

@@ -1,10 +1,13 @@
"""Serve dot diagrams inline."""
import base64
import logging
import re
import markdown
import pydot
logger = logging.getLogger(__name__)
class InlinePydot(markdown.Extension):
"""Wrap the markdown prepcoressor."""
@@ -29,6 +32,9 @@ class InlinePydotPreprocessor(markdown.preprocessors.Preprocessor):
# use pydot to turn the text into pydot
graphs = pydot.graph_from_dot_data(dot_string)
if not graphs:
logger.debug("some kind of issue with parsed 'dot' %s", dot_string)
raise ValueError("error parsing dot text!")
# encode the image and provide as an inline image in markdown
encoded_image = base64.b64encode(graphs[0].create_png()).decode('ascii')