diff options
-rwxr-xr-x | scripts/graphviz.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/scripts/graphviz.py b/scripts/graphviz.py new file mode 100755 index 000000000..ec31578ab --- /dev/null +++ b/scripts/graphviz.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + +"""Pandoc filter to process code blocks with class "graphviz" into +graphviz-generated images. +""" + +import pygraphviz +import hashlib +import os +import sys +from pandoc import toJSONFilter + +def sha1(x): + return hashlib.sha1(x).hexdigest() + +imagedir = "graphviz-images" +files = [] + +def graphviz(key, value, format): + if key == 'CodeBlock': + [[ident,classes,keyvals], code] = value + caption = "caption" + if "graphviz" in classes: + G = pygraphviz.AGraph(string = code) + G.layout() + filename = sha1(code) + if format == "html": + filetype = "png" + elif format == "latex": + filetype = "pdf" + else: + filetype = "png" + alt = [{'Str': caption}] + src = imagedir + '/' + filename + '.' + filetype + if not src in files: + try: + os.mkdir(imagedir) + sys.stderr.write('Created directory ' + imagedir) + except OSError: + pass + G.draw(src) + sys.stderr.write('Created image ' + src) + tit = "" + return {'Para': [{'Image': [alt, [src,tit]]}]} + +if __name__ == "__main__": + toJSONFilter(graphviz) |