# 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 RawBlock(Block):
#     """
#     Raw block
# 
#     :param text: a string of raw text with another underlying format
#     :type text: :class:`str`
#     :param format: Format of the raw text ('html', 'tex', 'latex', 'context', etc.)
#     :type format: ``str``
#     :Base: :class:`Block`
#      """
# 
#     __slots__ = ['text', 'format']
# 
#     def __init__(self, text, format='html'):
#         self.text = check_type(text, str)
#         self.format = check_group(format, RAW_FORMATS)
# 
#     def _slots_to_json(self):
#         return [self.format, 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 re
import os
import sys
import shutil
from panflute import toJSONFilter, Para, Image, RawBlock
from subprocess import Popen, PIPE, call
from tempfile import mkdtemp

imagedir = "tikz-images"


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


def tikz2image(tikz, filetype, outfile):
    tmpdir = mkdtemp()
    olddir = os.getcwd()
    os.chdir(tmpdir)
    f = open('tikz.tex', 'w')
    f.write("""\\documentclass{standalone}
             \\usepackage{tikz}
             \\begin{document}
             """)
    f.write(tikz)
    f.write("\n\\end{document}\n")
    f.close()
    p = call(["pdflatex", 'tikz.tex'], stdout=sys.stderr)
    os.chdir(olddir)
    if filetype == 'pdf':
        shutil.copyfile(tmpdir + '/tikz.pdf', outfile + '.pdf')
    else:
        call(["convert", tmpdir + '/tikz.pdf', outfile + '.' + filetype])
    shutil.rmtree(tmpdir)


def tikz(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 RawBlock(Block):
#     """
#     Raw block
# 
#     :param text: a string of raw text with another underlying format
#     :type text: :class:`str`
#     :param format: Format of the raw text ('html', 'tex', 'latex', 'context', etc.)
#     :type format: ``str``
#     :Base: :class:`Block`
#      """
# 
#     __slots__ = ['text', 'format']
# 
#     def __init__(self, text, format='html'):
#         self.text = check_type(text, str)
#         self.format = check_group(format, RAW_FORMATS)
# 
#     def _slots_to_json(self):
#         return [self.format, 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/tikz.py
import hashlib
import re
import os
import sys
import shutil
from panflute import toJSONFilter, Para, Image, RawBlock
from subprocess import Popen, PIPE, call
from tempfile import mkdtemp


imagedir = "tikz-images"


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


def tikz2image(tikz, filetype, outfile):
    tmpdir = mkdtemp()
    olddir = os.getcwd()
    os.chdir(tmpdir)
    f = open('tikz.tex', 'w')
    f.write("""\\documentclass{standalone}
             \\usepackage{tikz}
             \\begin{document}
             """)
    f.write(tikz)
    f.write("\n\\end{document}\n")
    f.close()
    p = call(["pdflatex", 'tikz.tex'], stdout=sys.stderr)
    os.chdir(olddir)
    if filetype == 'pdf':
        shutil.copyfile(tmpdir + '/tikz.pdf', outfile + '.pdf')
    else:
        call(["convert", tmpdir + '/tikz.pdf', outfile + '.' + filetype])
    shutil.rmtree(tmpdir)


def tikz(elem, doc):

    if type(elem) == RawBlock and elem.format == "latex":