blob: ec31578abad2a10a4961a162443c7f796746a39d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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)
|