aboutsummaryrefslogtreecommitdiff
path: root/scripts/graphviz.py
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2013-08-14 12:46:48 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2013-08-14 12:46:48 -0700
commitbc3b5f99d6a7437e401f4155e111089341e5a18d (patch)
tree4b05893d7e2c2a7bdca41d202295823da5493b92 /scripts/graphviz.py
parent96c2d542977e5cc510f315aad7c2ee357fe988c9 (diff)
downloadpandoc-bc3b5f99d6a7437e401f4155e111089341e5a18d.tar.gz
Added graphviz.py example script.
Diffstat (limited to 'scripts/graphviz.py')
-rwxr-xr-xscripts/graphviz.py47
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)