# Path: resources/lib/kindleunpack/compatibility_utils.py
# PY2 = sys.version_info[0] == 2
# 
# def utf8_str(p, enc='utf-8'):
#     if p is None:
#         return None
#     if isinstance(p, text_type):
#         return p.encode('utf-8')
#     if enc != 'utf-8':
#         return p.decode(enc).encode('utf-8')
#     return p
# 
# Path: resources/lib/kindleunpack/mobi_utils.py
# def fromBase32(str_num):
#     if isinstance(str_num, text_type):
#         str_num = str_num.encode('latin-1')
#     scalelst = [1,32,1024,32768,1048576,33554432,1073741824,34359738368]
#     value = 0
#     j = 0
#     n = len(str_num)
#     scale = 0
#     for i in range(n):
#         c = str_num[n-i-1:n-i]
#         if c in b'0123456789':
#             v = ord(c) - ord(b'0')
#         else:
#             v = ord(c) - ord(b'A') + 10
#         if j < len(scalelst):
#             scale = scalelst[j]
#         else:
#             scale = scale * 32
#         j += 1
#         if v != 0:
#             value = value + (v * scale)
#     return value


from .compatibility_utils import PY2, utf8_str
from .mobi_utils import fromBase32
import re

        # remove empty anchors
        print("Remove empty anchors from html")
        srctext = re.sub(br"<a\s*/>",br"", srctext)
        srctext = re.sub(br"<a\s*>\s*</a>",br"", srctext)

        # convert image references
        print("Insert image references into html")
        # split string into image tag pieces and other pieces
        image_pattern = re.compile(br'''(<img.*?>)''', re.IGNORECASE)
        image_index_pattern = re.compile(br'''recindex=['"]{0,1}([0-9]+)['"]{0,1}''', re.IGNORECASE)
        srcpieces = image_pattern.split(srctext)
        srctext = self.srctext = None

        # all odd pieces are image tags (nulls string on even pieces if no space between them in srctext)
        for i in range(1, len(srcpieces), 2):
            tag = srcpieces[i]
            for m in image_index_pattern.finditer(tag):
                imageNumber = int(m.group(1))
                imageName = rscnames[imageNumber-1]
                if imageName is None:
                    print("Error: Referenced image %s was not recognized as a valid image" % imageNumber)
                else:
                    replacement = b'src="Images/' + utf8_str(imageName) + b'"'
                    tag = image_index_pattern.sub(replacement, tag, 1)
            srcpieces[i] = tag
        srctext = b"".join(srcpieces)

        # add in character set meta into the html header if needed
        if 'Codec' in metadata:

# Path: resources/lib/kindleunpack/compatibility_utils.py
# PY2 = sys.version_info[0] == 2
# 
# def utf8_str(p, enc='utf-8'):
#     if p is None:
#         return None
#     if isinstance(p, text_type):
#         return p.encode('utf-8')
#     if enc != 'utf-8':
#         return p.decode(enc).encode('utf-8')
#     return p
# 
# Path: resources/lib/kindleunpack/mobi_utils.py
# def fromBase32(str_num):
#     if isinstance(str_num, text_type):
#         str_num = str_num.encode('latin-1')
#     scalelst = [1,32,1024,32768,1048576,33554432,1073741824,34359738368]
#     value = 0
#     j = 0
#     n = len(str_num)
#     scale = 0
#     for i in range(n):
#         c = str_num[n-i-1:n-i]
#         if c in b'0123456789':
#             v = ord(c) - ord(b'0')
#         else:
#             v = ord(c) - ord(b'A') + 10
#         if j < len(scalelst):
#             scale = scalelst[j]
#         else:
#             scale = scale * 32
#         j += 1
#         if v != 0:
#             value = value + (v * scale)
#     return value


# Path: resources/lib/kindleunpack/mobi_html.py
from .compatibility_utils import PY2, utf8_str
from .mobi_utils import fromBase32
import re


        # remove empty anchors
        print("Remove empty anchors from html")
        srctext = re.sub(br"<a\s*/>",br"", srctext)
        srctext = re.sub(br"<a\s*>\s*</a>",br"", srctext)

        # convert image references
        print("Insert image references into html")
        # split string into image tag pieces and other pieces
        image_pattern = re.compile(br'''(<img.*?>)''', re.IGNORECASE)
        image_index_pattern = re.compile(br'''recindex=['"]{0,1}([0-9]+)['"]{0,1}''', re.IGNORECASE)
        srcpieces = image_pattern.split(srctext)
        srctext = self.srctext = None

        # all odd pieces are image tags (nulls string on even pieces if no space between them in srctext)
        for i in range(1, len(srcpieces), 2):
            tag = srcpieces[i]
            for m in image_index_pattern.finditer(tag):
                imageNumber = int(m.group(1))
                imageName = rscnames[imageNumber-1]
                if imageName is None:
                    print("Error: Referenced image %s was not recognized as a valid image" % imageNumber)
                else:
                    replacement = b'src="Images/' + utf8_str(imageName) + b'"'
                    tag = image_index_pattern.sub(replacement, tag, 1)
            srcpieces[i] = tag
        srctext = b"".join(srcpieces)

        # add in character set meta into the html header if needed
        if 'Codec' in metadata:

            srctext = srctext[0:12]+b'<meta http-equiv="content-type" content="text/html; charset='+utf8_str(metadata.get('Codec')[0])+b'" />'+srctext[12:]