'Create the real index from the collected index entries.'

def create_index(self, builder, _fixre=re.compile('(.*)    ([(][^()]*[)])')):

    new = {}
    def add_entry(word, subword, dic=new):
        entry = dic.get(word)
        if (not entry):
            dic[word] = entry = [[], {}]
        if subword:
            add_entry(subword, '', dic=entry[1])
        else:
            try:
                entry[0].append(((builder.get_relative_uri('genindex', fn) + '#') + tid))
            except NoUri:
                pass
    for (fn, entries) in self.indexentries.iteritems():
        for (type, value, tid, alias) in entries:
            if (type == 'single'):
                try:
                    (entry, subentry) = value.split(';', 1)
                except ValueError:
                    (entry, subentry) = (value, '')
                if (not entry):
                    self.warn(fn, ('invalid    index    entry    %r' % value))
                    continue
                add_entry(entry.strip(), subentry.strip())
            elif (type == 'pair'):
                try:
                    (first, second) = map((lambda x: x.strip()), value.split(';', 1))
                    if ((not first) or (not second)):
                        raise ValueError
                except ValueError:
                    self.warn(fn, ('invalid    pair    index    entry    %r' % value))
                    continue
                add_entry(first, second)
                add_entry(second, first)
            elif (type == 'triple'):
                try:
                    (first, second, third) = map((lambda x: x.strip()), value.split(';', 2))
                    if ((not first) or (not second) or (not third)):
                        raise ValueError
                except ValueError:
                    self.warn(fn, ('invalid    triple    index    entry    %r' % value))
                    continue
                add_entry(first, ((second + '    ') + third))
                add_entry(second, ((third + ',    ') + first))
                add_entry(third, ((first + '    ') + second))
            else:
                self.warn(fn, ('unknown    index    entry    type    %r' % type))
    def keyfunc(entry, lcletters=(string.ascii_lowercase + '_')):
        lckey = entry[0].lower()
        if (lckey[0:1] in lcletters):
            return (chr(127) + lckey)
        return lckey
    newlist = new.items()
    newlist.sort(key=keyfunc)
    oldkey = ''
    oldsubitems = None
    i = 0
    while (i < len(newlist)):
        (key, (targets, subitems)) = newlist[i]
        if (not subitems):
            m = _fixre.match(key)
            if m:
                if (oldkey == m.group(1)):
                    oldsubitems.setdefault(m.group(2), [[], {}])[0].extend(targets)
                    del newlist[i]
                    continue
                oldkey = m.group(1)
            else:
                oldkey = key
        oldsubitems = subitems
        i += 1
    def keyfunc((k, v), letters=(string.ascii_uppercase + '_')):
        v[1] = sorted(((si, se) for (si, (se, void)) in v[1].iteritems()))
        letter = k[0].upper()
        if (letter in letters):
            return letter
        else:
            return 'Symbols'
    return [(key, list(group)) for (key, group) in groupby(newlist, keyfunc)]
