@register.filter
def toc(text):
    """Extract IDs from text and return Table of Contents,                                          
    as list of tuples (id, name)."""
    items = []
    for i in re.finditer(r'<[^>]*?id="(?P<id>[^"]*?)".*?>', text):
        _id = i.group('id')
        items.append({'id':_id, 'name': _id.replace('_', ' ').strip()})
    return items