author | Anton Shestakov <av6@dwimlabs.net> |
Fri, 27 Apr 2018 16:14:10 +0800 | |
branch | stable |
changeset 3728 | 0fc78fdca4b8 |
parent 3376 | aad37ffd7d58 |
permissions | -rwxr-xr-x |
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) |