comparison docs/tutorial/mypandocfilters/graphviz-file.py @ 3376:aad37ffd7d58

doc: import the training support Import the training support which was stored in a private-repository before.
author Boris Feld <boris.feld@octobus.net>
date Mon, 08 Jan 2018 11:46:53 +0100
parents
children
comparison
equal deleted inserted replaced
3375:1cb549cd6236 3376:aad37ffd7d58
1 #!/usr/bin/env python
2
3 """
4 Pandoc filter to process code blocks with class "graphviz" into
5 graphviz-generated images.
6
7 Needs pygraphviz
8 """
9
10 import os
11 import sys
12
13 import pygraphviz
14
15 from pandocfilters import toJSONFilter, Para, Image, get_filename4code, get_caption, get_extension, get_value
16
17 def graphviz(key, value, format, _):
18 if key == 'CodeBlock':
19 [[ident, classes, keyvals], file] = value
20 if "graphviz-file" in classes:
21 caption, typef, keyvals = get_caption(keyvals)
22 prog, keyvals = get_value(keyvals, u"prog", u"dot")
23 filetype = get_extension(format, "svg", html="svg", latex="pdf")
24 with open(file) as f:
25 code = f.read()
26 dest = get_filename4code("graphviz", code, filetype)
27
28 if not os.path.isfile(dest):
29 g = pygraphviz.AGraph(string=code)
30 g.layout()
31 g.draw(dest, prog=prog)
32 sys.stderr.write('Created image ' + dest + '\n')
33
34 return Para([Image([ident, ['graphviz'], keyvals], caption, [dest, typef])])
35
36 if __name__ == "__main__":
37 toJSONFilter(graphviz)