Assigns id-less headers a slug that is derived from their
    titles. Slugs are generated by lower-casing the titles, stripping
    all punctuation, and converting spaces to hyphens (-).

def slug_sub(match):
    """Assigns id-less headers a slug that is derived from their
    titles. Slugs are generated by lower-casing the titles, stripping
    all punctuation, and converting spaces to hyphens (-).
    """
    level = match.group(1)
    title = match.group(2)
    slug = title.lower()
    slug = re.sub(r'<.+?>|[^\w-]', ' ', slug)
    slug = re.sub(r'[ \t]+', ' ', slug).strip()
    slug = slug.replace(' ', '-')
    if slug:
        return '<{0} id="{1}">{2}</{0}>'.format(level, slug, title)
    return match.group(0)