annotate docs/tutorial/mypandocfilters/graphviz-file.py @ 4277:7edc5c148df0 stable 8.3.2

packagin: prepare release 8.3.2
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Tue, 27 Nov 2018 04:31:03 +0100
parents aad37ffd7d58
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3376
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
1 #!/usr/bin/env python
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
2
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
3 """
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
4 Pandoc filter to process code blocks with class "graphviz" into
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
5 graphviz-generated images.
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
6
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
7 Needs pygraphviz
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
8 """
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
9
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
10 import os
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
11 import sys
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
12
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
13 import pygraphviz
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
14
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
15 from pandocfilters import toJSONFilter, Para, Image, get_filename4code, get_caption, get_extension, get_value
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
16
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
17 def graphviz(key, value, format, _):
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
18 if key == 'CodeBlock':
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
19 [[ident, classes, keyvals], file] = value
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
20 if "graphviz-file" in classes:
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
21 caption, typef, keyvals = get_caption(keyvals)
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
22 prog, keyvals = get_value(keyvals, u"prog", u"dot")
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
23 filetype = get_extension(format, "svg", html="svg", latex="pdf")
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
24 with open(file) as f:
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
25 code = f.read()
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
26 dest = get_filename4code("graphviz", code, filetype)
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
27
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
28 if not os.path.isfile(dest):
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
29 g = pygraphviz.AGraph(string=code)
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
30 g.layout()
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
31 g.draw(dest, prog=prog)
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
32 sys.stderr.write('Created image ' + dest + '\n')
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
33
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
34 return Para([Image([ident, ['graphviz'], keyvals], caption, [dest, typef])])
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
35
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
36 if __name__ == "__main__":
aad37ffd7d58 doc: import the training support
Boris Feld <boris.feld@octobus.net>
parents:
diff changeset
37 toJSONFilter(graphviz)