'Add an item to the manifest with href derived from the given
name. Ensures uniqueness of href and id automatically. Returns
generated item.'

def generate_item(self, name, id_prefix=None, media_type=None, unique_href=True):

    id_prefix = (id_prefix or u'id')
    media_type = (media_type or guess_type(name))
    if unique_href:
        name = self.make_name_unique(name)
    href = self.name_to_href(name, self.opf_name)
    (base, ext) = href.rpartition(u'.')[0::2]
    all_ids = {x.get(u'id') for x in self.opf_xpath(u'//*[@id]')}
    c = 0
    item_id = id_prefix
    while (item_id in all_ids):
        c += 1
        item_id = (id_prefix + (u'%d' % c))
    manifest = self.opf_xpath(u'//opf:manifest')[0]
    item = manifest.makeelement(OPF(u'item'), id=item_id, href=href)
    item.set(u'media-type', media_type)
    self.insert_into_xml(manifest, item)
    self.dirty(self.opf_name)
    name = self.href_to_name(href, self.opf_name)
    self.name_path_map[name] = path = self.name_to_abspath(name)
    self.mime_map[name] = media_type
    base = os.path.dirname(path)
    if (not os.path.exists(base)):
        os.makedirs(base)
    open(path, u'wb').close()
    return item
