# 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
            part = parts[i]
            srcpieces = find_tag_with_AmznPageBreak_pattern.split(part)
            for j in range(len(srcpieces)):
                tag = srcpieces[j]
                if tag.startswith(b'<'):
                    srcpieces[j] = within_tag_AmznPageBreak_position_pattern.sub(
                        lambda m:b' style="page-break-after:' + m.group(1) + b'"', tag)
            part = b"".join(srcpieces)
            parts[i] = part

        # we have to handle substitutions for the flows  pieces first as they may
        # be inlined into the xhtml text
        #   kindle:embed:XXXX?mime=image/gif (png, jpeg, etc) (used for images)
        #   kindle:flow:XXXX?mime=YYYY/ZZZ (used for style sheets, svg images, etc)
        #   kindle:embed:XXXX   (used for fonts)

        flows = []
        flows.append(None)
        flowinfo = []
        flowinfo.append([None, None, None, None])

        # regular expression search patterns
        img_pattern = re.compile(br'''(<[img\s|image\s][^>]*>)''', re.IGNORECASE)
        img_index_pattern = re.compile(br'''[('"]kindle:embed:([0-9|A-V]+)[^'"]*['")]''', re.IGNORECASE)

        tag_pattern = re.compile(br'''(<[^>]*>)''')
        flow_pattern = re.compile(br'''['"]kindle:flow:([0-9|A-V]+)\?mime=([^'"]+)['"]''', re.IGNORECASE)

        url_pattern = re.compile(br'''(url\(.*?\))''', re.IGNORECASE)
        url_img_index_pattern = re.compile(br'''[('"]kindle:embed:([0-9|A-V]+)\?mime=image/[^\)]*["')]''', re.IGNORECASE)

# 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

            part = parts[i]
            srcpieces = find_tag_with_AmznPageBreak_pattern.split(part)
            for j in range(len(srcpieces)):
                tag = srcpieces[j]
                if tag.startswith(b'<'):
                    srcpieces[j] = within_tag_AmznPageBreak_position_pattern.sub(
                        lambda m:b' style="page-break-after:' + m.group(1) + b'"', tag)
            part = b"".join(srcpieces)
            parts[i] = part

        # we have to handle substitutions for the flows  pieces first as they may
        # be inlined into the xhtml text
        #   kindle:embed:XXXX?mime=image/gif (png, jpeg, etc) (used for images)
        #   kindle:flow:XXXX?mime=YYYY/ZZZ (used for style sheets, svg images, etc)
        #   kindle:embed:XXXX   (used for fonts)

        flows = []
        flows.append(None)
        flowinfo = []
        flowinfo.append([None, None, None, None])

        # regular expression search patterns
        img_pattern = re.compile(br'''(<[img\s|image\s][^>]*>)''', re.IGNORECASE)
        img_index_pattern = re.compile(br'''[('"]kindle:embed:([0-9|A-V]+)[^'"]*['")]''', re.IGNORECASE)

        tag_pattern = re.compile(br'''(<[^>]*>)''')
        flow_pattern = re.compile(br'''['"]kindle:flow:([0-9|A-V]+)\?mime=([^'"]+)['"]''', re.IGNORECASE)

        url_pattern = re.compile(br'''(url\(.*?\))''', re.IGNORECASE)
        url_img_index_pattern = re.compile(br'''[('"]kindle:embed:([0-9|A-V]+)\?mime=image/[^\)]*["')]''', re.IGNORECASE)

        font_index_pattern = re.compile(br'''[('"]kindle:embed:([0-9|A-V]+)["')]''', re.IGNORECASE)