# Path: panflute/elements.py
# class Para(Block):
#     """Paragraph
# 
#     :param args: contents of the paragraph
#     :type args: :class:`Inline`
#     :Base: :class:`Block`
# 
#     :Example:
# 
#         >>> content = [Str('Some'), Space, Emph(Str('words.'))]
#         >>> para1 = Para(*content)
#         >>> para2 = Para(Str('More'), Space, Str('words.'))
#     """
#     __slots__ = ['_content']
#     _children = ['content']
# 
#     def __init__(self, *args):
#         self._set_content(args, Inline)
# 
#     def _slots_to_json(self):
#         return self.content.to_json()
# 
# class CodeBlock(Block):
#     """
#     Code block (literal text) with optional attributes
# 
#     :param text: literal text (preformatted text, code, etc.)
#     :type text: :class:`str`
#     :param identifier: element identifier (usually unique)
#     :type identifier: :class:`str`
#     :param classes: class names of the element
#     :type classes: :class:`list` of :class:`str`
#     :param attributes: additional attributes
#     :type attributes: :class:`dict`
#     :Base: :class:`Block`
#      """
# 
#     __slots__ = ['text', 'identifier', 'classes', 'attributes']
# 
#     def __init__(self, text, identifier='', classes=[], attributes={}):
#         self._set_ica(identifier, classes, attributes)
#         self.text = check_type(text, str)
# 
#     def _slots_to_json(self):
#         return [self._ica_to_json(), self.text]
# 
# class Image(Inline):
#     """
#     Image
# 
#     :param args: text with the image description
#     :type args: :class:`Inline`
#     :param url: URL or path of the image
#     :type url: ``str``
#     :param title: Alt. title
#     :type title: ``str``
#     :param identifier: element identifier (usually unique)
#     :type identifier: :class:`str`
#     :param classes: class names of the element
#     :type classes: :class:`list` of :class:`str`
#     :param attributes: additional attributes
#     :type attributes: :class:`dict`
#     :Base: :class:`Inline`
#      """
# 
#     __slots__ = ['_content', 'url', 'title',
#                  'identifier', 'classes', 'attributes']
#     _children = ['content']
# 
#     def __init__(self, *args, url='', title='',
#                  identifier='', classes=[], attributes={}):
#         self._set_ica(identifier, classes, attributes)
#         self._set_content(args, Inline)
#         self.url = check_type(url, str)
#         self.title = check_type(title, str)
# 
#     def _slots_to_json(self):
#         ut = [self.url, self.title]
#         return [self._ica_to_json(), self.content.to_json(), ut]
# 
# Path: panflute/io.py
# def toJSONFilter(*args, **kwargs):
#     """
#     Wrapper for :func:`.run_filter`, which calls :func:`.run_filters`
# 
#     toJSONFilter(action, prepare=None, finalize=None, input_stream=None, output_stream=None, \*\*kwargs)
#     Receive a Pandoc document from stdin, apply the *action* function to each element, and write it back to stdout.
# 
#     See also :func:`.toJSONFilters`
#     """
#     return run_filter(*args, **kwargs)


import hashlib
import os
import sys
from panflute import toJSONFilter, Para, Image, CodeBlock
from subprocess import Popen, PIPE, call
#!/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.
"""


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):

# Path: panflute/elements.py
# class Para(Block):
#     """Paragraph
# 
#     :param args: contents of the paragraph
#     :type args: :class:`Inline`
#     :Base: :class:`Block`
# 
#     :Example:
# 
#         >>> content = [Str('Some'), Space, Emph(Str('words.'))]
#         >>> para1 = Para(*content)
#         >>> para2 = Para(Str('More'), Space, Str('words.'))
#     """
#     __slots__ = ['_content']
#     _children = ['content']
# 
#     def __init__(self, *args):
#         self._set_content(args, Inline)
# 
#     def _slots_to_json(self):
#         return self.content.to_json()
# 
# class CodeBlock(Block):
#     """
#     Code block (literal text) with optional attributes
# 
#     :param text: literal text (preformatted text, code, etc.)
#     :type text: :class:`str`
#     :param identifier: element identifier (usually unique)
#     :type identifier: :class:`str`
#     :param classes: class names of the element
#     :type classes: :class:`list` of :class:`str`
#     :param attributes: additional attributes
#     :type attributes: :class:`dict`
#     :Base: :class:`Block`
#      """
# 
#     __slots__ = ['text', 'identifier', 'classes', 'attributes']
# 
#     def __init__(self, text, identifier='', classes=[], attributes={}):
#         self._set_ica(identifier, classes, attributes)
#         self.text = check_type(text, str)
# 
#     def _slots_to_json(self):
#         return [self._ica_to_json(), self.text]
# 
# class Image(Inline):
#     """
#     Image
# 
#     :param args: text with the image description
#     :type args: :class:`Inline`
#     :param url: URL or path of the image
#     :type url: ``str``
#     :param title: Alt. title
#     :type title: ``str``
#     :param identifier: element identifier (usually unique)
#     :type identifier: :class:`str`
#     :param classes: class names of the element
#     :type classes: :class:`list` of :class:`str`
#     :param attributes: additional attributes
#     :type attributes: :class:`dict`
#     :Base: :class:`Inline`
#      """
# 
#     __slots__ = ['_content', 'url', 'title',
#                  'identifier', 'classes', 'attributes']
#     _children = ['content']
# 
#     def __init__(self, *args, url='', title='',
#                  identifier='', classes=[], attributes={}):
#         self._set_ica(identifier, classes, attributes)
#         self._set_content(args, Inline)
#         self.url = check_type(url, str)
#         self.title = check_type(title, str)
# 
#     def _slots_to_json(self):
#         ut = [self.url, self.title]
#         return [self._ica_to_json(), self.content.to_json(), ut]
# 
# Path: panflute/io.py
# def toJSONFilter(*args, **kwargs):
#     """
#     Wrapper for :func:`.run_filter`, which calls :func:`.run_filters`
# 
#     toJSONFilter(action, prepare=None, finalize=None, input_stream=None, output_stream=None, \*\*kwargs)
#     Receive a Pandoc document from stdin, apply the *action* function to each element, and write it back to stdout.
# 
#     See also :func:`.toJSONFilters`
#     """
#     return run_filter(*args, **kwargs)


# Path: examples/panflute/abc.py
import hashlib
import os
import sys
from panflute import toJSONFilter, Para, Image, CodeBlock
from subprocess import Popen, PIPE, call

#!/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.
"""


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: