pydot: handle multiple dot images in one file

This commit is contained in:
2022-04-18 14:40:01 -05:00
parent 9a65dfffaf
commit 08305e26db
4 changed files with 48 additions and 5 deletions

View File

@@ -25,9 +25,12 @@ class InlinePydotPreprocessor(markdown.preprocessors.Preprocessor):
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)
out = text
for block_match in self.BLOCK_RE.finditer(text):
filename = block_match.group(1)
dot_string = block_match.group(2)
logger.debug("matched markdown block: %s", dot_string)
logger.debug("match start/end: %s/%s", block_match.start(), block_match.end())
# use pydot to turn the text into pydot
graphs = pydot.graph_from_dot_data(dot_string)
@@ -41,9 +44,9 @@ class InlinePydotPreprocessor(markdown.preprocessors.Preprocessor):
inline_image = f'![{filename}]({data_path})'
# replace the image in the output markdown
text = f'{text[:match.start()]}\n{inline_image}\n{text[match.end():]}'
out = out.replace(block_match.group(0), inline_image)
return text.split('\n')
return out.split('\n')
def makeExtension(*args, **kwargs):