Skip to content

Commit

Permalink
graphviz: use utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AugustH committed Apr 27, 2016
1 parent 8c0b89e commit 27de716
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
8 changes: 7 additions & 1 deletion examples/graphviz-sample.md
Expand Up @@ -5,7 +5,7 @@ Use this
digraph G {Hello->World}
```

to get
to get

```graphviz
digraph G {Hello->World}
Expand All @@ -16,3 +16,9 @@ with with Äüö
```graphviz
digraph G {Hello->World with Äüö}
```

See [(this is a link to whatever)](#whatever) for an example with options `{.graphviz #whatever caption="this is the caption" width=35%}`:

```{.graphviz #whatever caption="this is the caption" width=35%}
digraph G {Hello->World}
```
46 changes: 16 additions & 30 deletions examples/graphviz.py
Expand Up @@ -3,47 +3,33 @@
"""
Pandoc filter to process code blocks with class "graphviz" into
graphviz-generated images.
Needs pygraphviz
"""

import pygraphviz
import hashlib
import os
import sys
from pandocfilters import toJSONFilter, Str, Para, Image


def sha1(x):
return hashlib.sha1(x.encode(sys.getfilesystemencoding())).hexdigest()
import pygraphviz

imagedir = "graphviz-images"
from pandocfilters import toJSONFilter, Para, Image, get_filename4code, get_caption, get_extension


def graphviz(key, value, format, meta):
def graphviz(key, value, format, _):
if key == 'CodeBlock':
[[ident, classes, keyvals], code] = value
caption = "caption"
if "graphviz" in classes:
G = pygraphviz.AGraph(string=code)
G.layout()
filename = sha1(code)
if format == "html":
filetype = "png"
elif format == "latex":
filetype = "pdf"
else:
filetype = "png"
alt = Str(caption)
src = imagedir + '/' + filename + '.' + filetype
if not os.path.isfile(src):
try:
os.mkdir(imagedir)
sys.stderr.write('Created directory ' + imagedir + '\n')
except OSError:
pass
G.draw(src)
sys.stderr.write('Created image ' + src + '\n')
tit = ""
return Para([Image(['', [], []], [alt], [src, tit])])
caption, typef, keyvals = get_caption(keyvals)
filetype = get_extension(format, "png", html="png", latex="pdf")
dest = get_filename4code("graphviz", code, filetype)

if not os.path.isfile(dest):
g = pygraphviz.AGraph(string=code)
g.layout()
g.draw(dest)
sys.stderr.write('Created image ' + dest + '\n')

return Para([Image([ident, [], keyvals], caption, [dest, typef])])

if __name__ == "__main__":
toJSONFilter(graphviz)

0 comments on commit 27de716

Please sign in to comment.