def atx_header_sub(match):
    """Substitutes atx headers (headers defined using #'s)."""
    level = len(match.group(1))
    title = match.group(2)

    id_class = ''
    ids = match.group(3) if match.group(3) else ''
    id_match = re.search('#([\w-]+)', ids)
    if id_match:
        id_class += ' id="' + id_match.group(1) + '"'
    classes = ' '.join(re.findall('\.([\w-]+)', ids))
    if classes:
        id_class += ' class="' + classes + '"'
    return '\n<h{0}{2}>{1}</h{0}>\n'.format(level, title, id_class)
Substitutes atx headers (headers defined using #'s).