'Replace links to resources (raster images/fonts) with pointers to
the MOBI record containing the resource. The pointers are of the form:
kindle:embed:XXXX?mime=image/* The ?mime= is apparently optional and
not used for fonts.'

def replace_resource_links(self):

    def pointer(item, oref):
        ref = urlnormalize(item.abshref(oref))
        idx = self.resources.item_map.get(ref, None)
        if (idx is not None):
            is_image = (self.resources.records[(idx - 1)][:4] not in {'FONT'})
            idx = to_ref(idx)
            if is_image:
                self.used_images.add(ref)
                return (u'kindle:embed:%s?mime=%s' % (idx, self.resources.mime_map[ref]))
            else:
                return (u'kindle:embed:%s' % idx)
        return oref
    for item in self.oeb.manifest:
        if (item.media_type in XML_DOCS):
            root = self.data(item)
            for tag in XPath(u'//h:img|//svg:image')(root):
                for (attr, ref) in tag.attrib.iteritems():
                    if (attr.split(u'}')[(-1)].lower() in {u'src', u'href'}):
                        tag.attrib[attr] = pointer(item, ref)
            for tag in XPath(u'//h:style')(root):
                if tag.text:
                    sheet = cssutils.parseString(tag.text, validate=False)
                    replacer = partial(pointer, item)
                    cssutils.replaceUrls(sheet, replacer, ignoreImportRules=True)
                    repl = sheet.cssText
                    if isbytestring(repl):
                        repl = repl.decode(u'utf-8')
                    tag.text = ((u'\n' + repl) + u'\n')
        elif (item.media_type in OEB_STYLES):
            sheet = self.data(item)
            replacer = partial(pointer, item)
            cssutils.replaceUrls(sheet, replacer, ignoreImportRules=True)
