ChangeLog parser

from topdown import *

@as_parser
def parse_changelog(cs):
    sections = many(section)(cs)
    return u'<dl>%s</dl>' % '\n'.join(sections)

def section(cs):
    title = cs.parse(until('\n'))
    cs.parse(many1('\n'))
    items = many1(item)(cs)
    return u'<dt>%s</dt>%s' % (title, '\n'.join(items))

def item(cs):
    cs.parse('\t*')
    skip_ws(cs)
    i = cs.parse(until('\n'))
    cs.parse(many1('\n'))
    if i.endswith(':'):
        i = i[:-1]
        sub = many(subitem)(cs)
    else:
        sub = []
    h = u'<dd class="changelog_item">%s</dd>' % (i)
    return '\n'.join([h] + sub)

def subitem(cs):
    cs.parse('\t-')
    skip_ws(cs)
    i = cs.parse(until('\n'))
    cs.parse(many('\n'))
    return u'<dd class="changelog_sub_item">%s</dd>' % i

def main(path):
    c = unicode(open(path).read(), ENCODING)
    print c.encode(ENCODING)

if __name__ == '__main__':
    import sys
    main(sys.argv[1])