"""
This module contains functions that does convertion of code and equations to
PDF and png formats

"""

from __future__ import print_function
import os
import sys
import hashlib
import errno
import shutil
from xml.sax.saxutils import unescape

from lxml import etree
from termcolor import colored

from . import pstikz2png
from .pstikz2png import LatexPictureError
from . import utils


def get_code_hash(codetext):
    '''
    Calculate the hash and output path for a given image, given the code as
    string
    '''
    codetext = ''.join([c for c in codetext if ord(c) < 128])
    code_hash = hashlib.md5(
        ''.join(codetext.encode('utf-8').split())).hexdigest()

    return code_hash


def cleanup_after_latex(figpath):
    ''' clean up after the image generation
    '''
    tmpdir = os.path.dirname(figpath)
    try:
        shutil.rmtree(tmpdir)
    except OSError as exc: