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
|
#!/usr/bin/env python3
"""
Filter to process code blocks with class "blockdiag" into
generated images.
Needs utils from http://blockdiag.com
"""
import os
import sys
from subprocess import call
from pandocfilters import toJSONFilter, Para, Image, get_filename4code, get_caption, get_extension
def blockdiag(key, value, format, _):
if key == 'CodeBlock':
[[ident, classes, keyvals], code] = value
all_kw = { "actdiag", "blockdiag", "nwdiag", "packetdiag", "rackdiag", "seqdiag" }
kw = all_kw & set(classes)
if len(kw) == 1:
caption, typef, keyvals = get_caption(keyvals)
filename = get_filename4code("blockdiag", code)
filetype = get_extension(format, "png", html="svg", latex="pdf")
src = filename + '.diag'
dest = filename + '.' + filetype
if not os.path.isfile(dest):
cmd = str(list(kw)[0])
if not code.startswith(cmd):
code = cmd + "{\n" + code + "\n}\n"
with open(src, "w") as f:
f.write(code)
call([cmd, "-a", "-T"+filetype, src])
sys.stderr.write('Created image ('+ cmd + ") " + dest + '\n')
return Para([Image([ident, [], keyvals], caption, [dest, typef])])
if __name__ == "__main__":
toJSONFilter(blockdiag)
|