git.iko.soy/typeable/pandoc
on master help sign in
Added scripts/abc.py.
commit5d2afd389478667d0c44ddf584bd93b9377e927e authorJohn MacFarlane <fiddlosopher@gmail.com> dateWed 14 Aug 2013, 22:19 (13 years ago) parent8926230
1 file changed+50
scripts/abc.py+50
scripts/abc.pynew file+50
@@ -0,0 +1,50 @@
1+#!/usr/bin/env python
2+
3+"""
4+Pandoc filter to process code blocks with class "abc" containing
5+ABC notation into images. Assumes that abcm2ps and ImageMagick's
6+convert are in the path. Images are put in the abc-images directory.
7+"""
8+
9+import hashlib
10+import os
11+import sys
12+from pandoc import toJSONFilter
13+from subprocess import Popen, PIPE, call
14+
15+imagedir = "abc-images"
16+
17+def sha1(x):
18+ return hashlib.sha1(x).hexdigest()
19+
20+def abc2eps(abc, filetype, outfile):
21+ p = Popen(["abcm2ps", "-O", outfile + '.eps', "-"],stdin=PIPE)
22+ p.stdin.write(abc)
23+ p.communicate()
24+ p.stdin.close()
25+ p = call(["convert", outfile + '.eps', outfile + '.' + filetype])
26+
27+def abc(key, value, format):
28+ if key == 'CodeBlock':
29+ [[ident,classes,keyvals], code] = value
30+ if "abc" in classes:
31+ outfile = imagedir + '/' + sha1(code)
32+ if format == "html":
33+ filetype = "png"
34+ elif format == "latex":
35+ filetype = "pdf"
36+ else:
37+ filetype = "png"
38+ src = outfile + '.' + filetype
39+ if not os.path.isfile(src):
40+ try:
41+ os.mkdir(imagedir)
42+ sys.stderr.write('Created directory ' + imagedir + '\n')
43+ except OSError:
44+ pass
45+ abc2eps(code, filetype, outfile)
46+ sys.stderr.write('Created image ' + src + '\n')
47+ return {'Para': [{'Image': [[], [src,""]]}]}
48+
49+if __name__ == "__main__":
50+ toJSONFilter(abc)