Skip to content

Commit

Permalink
abc: use utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AugustH committed Apr 27, 2016
1 parent 28d6746 commit 8c0b89e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 30 deletions.
31 changes: 27 additions & 4 deletions examples/abc-sample.md
Expand Up @@ -9,9 +9,9 @@ C:André Raison
M:3/4
L:1/4
Q:1/4=92
%%staves {(Pos1 Pos2) Trompette}
%%staves {(Pos1 Pos2) Trompette}
K:F
%
%
V:Pos1
%%MIDI program 78
"Positif"x3 |x3 |c'>ba|Pga/g/f|:g2a |ba2 |g2c- |c2P=B |c>de |fga |
Expand All @@ -32,9 +32,9 @@ C:André Raison
M:3/4
L:1/4
Q:1/4=92
%%staves {(Pos1 Pos2) Trompette}
%%staves {(Pos1 Pos2) Trompette}
K:F
%
%
V:Pos1
%%MIDI program 78
"Positif"x3 |x3 |c'>ba|Pga/g/f|:g2a |ba2 |g2c- |c2P=B |c>de |fga |
Expand All @@ -46,3 +46,26 @@ V:Trompette
"Trompette"z3|z3 |z3 |z3 |:Mc>BA|PGA/G/F|PE>EF|PEF/E/D|C>CPB,|A,G,F,-|
```

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

```{.abc #whatever caption="this is the caption" width=50%}
X:7
T:Qui Tolis (Trio)
C:André Raison
M:3/4
L:1/4
Q:1/4=92
%%staves {(Pos1 Pos2) Trompette}
K:F
%
V:Pos1
%%MIDI program 78
"Positif"x3 |x3 |c'>ba|Pga/g/f|:g2a |ba2 |g2c- |c2P=B |c>de |fga |
V:Pos2
%%MIDI program 78
Mf>ed|cd/c/B|PA2d |ef/e/d |:e2f |ef2 |c>BA |GA/G/F |E>FG |ABc- |
V:Trompette
%%MIDI program 56
"Trompette"z3|z3 |z3 |z3 |:Mc>BA|PGA/G/F|PE>EF|PEF/E/D|C>CPB,|A,G,F,-|
```
40 changes: 14 additions & 26 deletions examples/abc.py
Expand Up @@ -6,48 +6,36 @@
convert are in the path. Images are put in the abc-images directory.
"""

import hashlib
import os
import sys
from pandocfilters import toJSONFilter, Para, Image
from subprocess import Popen, PIPE, call

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


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


def abc2eps(abc, filetype, outfile):
def abc2eps(abc_src, filetype, outfile):
p = Popen(["abcm2ps", "-O", outfile + '.eps', "-"], stdin=PIPE)
p.stdin.write(abc)
p.stdin.write(abc_src)
p.communicate()
p.stdin.close()
call(["convert", outfile + '.eps', outfile + '.' + filetype])


def abc(key, value, format, meta):
def abc(key, value, format, _):
if key == 'CodeBlock':
[[ident, classes, keyvals], code] = value
if "abc" in classes:
outfile = imagedir + '/' + sha1(code)
if format == "html":
filetype = "png"
elif format == "latex":
filetype = "pdf"
else:
filetype = "png"
src = outfile + '.' + filetype
if not os.path.isfile(src):
try:
os.mkdir(imagedir)
sys.stderr.write('Created directory ' + imagedir + '\n')
except OSError:
pass
caption, typef, keyvals = get_caption(keyvals)
outfile = get_filename4code("abc", code)
filetype = get_extension(format, "png", html="png", latex="pdf")
dest = outfile + '.' + filetype

if not os.path.isfile(dest):
abc2eps(code.encode("utf-8"), filetype, outfile)
sys.stderr.write('Created image ' + src + '\n')
return Para([Image(['', [], []], [], [src, ""])])
sys.stderr.write('Created image ' + dest + '\n')

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


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

0 comments on commit 8c0b89e

Please sign in to comment.