docs/tutorial/mypandocfilters/graphviz-file.py
author Pulkit Goyal <pulkit@yandex-team.ru>
Fri, 12 Oct 2018 02:12:23 +0300
changeset 4158 5dd45784a8e0
parent 3376 aad37ffd7d58
permissions -rwxr-xr-x
rewind: alias the command to undo This is a step in unifiying rewind and undo and not talk about them as different commands or concepts. If there exists rewind command, that should be an alias of undo and the vice versa. The exact implmentation detail and how the command works etc. is a different thing but there should be just one command.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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)