Skip to content

Instantly share code, notes, and snippets.



Created Oct 21, 2017
What would you like to do?
graphviz pandoc filter
#!/usr/bin/env python
Pandoc filter to process code blocks with class "graphviz" into
graphviz-generated images.
Needs pygraphviz
import os
import sys
import pygraphviz
from pandocfilters import toJSONFilter, Para, Image, get_filename4code, get_caption, get_extension
def graphviz(key, value, format, _):
if key == 'CodeBlock':
[[ident, classes, keyvals], code] = value
if "graphviz" in classes:
caption, typef, keyvals = get_caption(keyvals)
filetype = get_extension(format, "png", html="png", latex="pdf")
dest = get_filename4code("graphviz", code, filetype)
if not os.path.isfile(dest):
g = pygraphviz.AGraph(string=code)
sys.stderr.write('Created image ' + dest + '\n')
return Para([Image([ident, [], keyvals], caption, [dest, typef])])
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment