diff options
author | John MacFarlane <jgm@berkeley.edu> | 2013-08-14 12:46:48 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2013-08-14 12:46:48 -0700 |
commit | bc3b5f99d6a7437e401f4155e111089341e5a18d (patch) | |
tree | 4b05893d7e2c2a7bdca41d202295823da5493b92 /scripts | |
parent | 96c2d542977e5cc510f315aad7c2ee357fe988c9 (diff) | |
download | pandoc-bc3b5f99d6a7437e401f4155e111089341e5a18d.tar.gz |
Added graphviz.py example script.
Diffstat (limited to 'scripts')
-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) |