From bc3b5f99d6a7437e401f4155e111089341e5a18d Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Wed, 14 Aug 2013 12:46:48 -0700 Subject: Added graphviz.py example script. --- scripts/graphviz.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 scripts/graphviz.py (limited to 'scripts') 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) -- cgit v1.2.3