'Update the internal link placeholders to point to the correct
location, based on the chunk table.'

def set_internal_links(self, text, rebuilt_text):

    aid_map = {}
    for match in re.finditer('<[^>]+?    [ac]id=[\\\'"]([cA-Z0-9]+)[\\\'"]', rebuilt_text):
        offset = match.start()
        pos_fid = None
        for chunk in self.chunk_table:
            if (chunk.insert_pos <= offset < (chunk.insert_pos + chunk.length)):
                pos_fid = (chunk.sequence_number, (offset - chunk.insert_pos), offset)
                break
            if (chunk.insert_pos > offset):
                pos_fid = (chunk.sequence_number, 0, offset)
                break
            if (chunk is self.chunk_table[(-1)]):
                pos_fid = (chunk.sequence_number, (offset - chunk.insert_pos), offset)
        if (pos_fid is None):
            raise ValueError((u'Could    not    find    chunk    for    aid:    %r' % match.group(1)))
        aid_map[match.group(1)] = pos_fid
    self.aid_offset_map = aid_map
    def to_placeholder(aid):
        (pos, fid, _) = aid_map[aid]
        (pos, fid) = (to_base(pos, min_num_digits=4), to_href(fid))
        return bytes(u':off:'.join((pos, fid)))
    placeholder_map = {bytes(k): to_placeholder(v) for (k, v) in self.placeholder_map.iteritems()}
    def sub(match):
        raw = match.group()
        pl = match.group(1)
        try:
            return (raw[:(-19)] + placeholder_map[pl])
        except KeyError:
            pass
        return raw
    return re.sub('<[^>]+(kindle:pos:fid:0000:off:[0-9A-Za-z]{10})', sub, text)
