#!/usr/bin/env python

"""
Pandoc filter to process code blocks with class "abc" containing
ABC notation into images.  Assumes that abcm2ps and ImageMagick's
convert are in the path.  Images are put in the abc-images directory.
"""

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

imagedir = "abc-images"


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


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


def abc(elem, doc):
    if type(elem) == CodeBlock and 'abc' in elem.classes:
        code = elem.text
        outfile = os.path.join(imagedir, sha1(code))
        filetype = {'html': 'png', 'latex': 'pdf'}.get(doc.format, 'png')
        src = outfile + '.' + filetype
        if not os.path.isfile(src):
            try:
                os.mkdir(imagedir)
                sys.stderr.write('Created directory ' + imagedir + '\n')
            except OSError:
                pass
            abc2eps(code.encode("utf-8"), filetype, outfile)
            sys.stderr.write('Created image ' + src + '\n')
        return Para(Image(url=src))


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