# vim: expandtab tabstop=4
from StringIO import StringIO
import re
import string

rules = re.compile(r"""(?P<heading>^\s*(?P<hdepth>=+)\s(?P<header>.*)\s(?P=hdepth)\s*$)""")
anchor = re.compile('[^\w\d]+')

def parse_toc(env, out, page, body):
    depth = 1
    for line in body.splitlines():
        match = rules.match(line)
        if match:
            header = match.group('header')
            new_depth = len(match.group('hdepth'))
            if new_depth < depth:
                while new_depth < depth:
                    depth -= 1
                    out.write("</li></ol><li>\n")
            elif new_depth > depth:
                while new_depth > depth:
                    depth += 1
                    out.write("<ol><li>\n")
            else:
                out.write("</li><li>\n")
            link = page + "#" + anchor.sub("", header)
            out.write('<a href="%s">%s</a>' % (env.href.wiki(link), header))
    while depth > 1:
        out.write("</li></ol>\n")
        depth -= 1

def execute(hdf, args, env):
    db = env.get_db_cnx()
    out = StringIO()
    # Has the user supplied a list of pages?
    if args:
        pages = re.split('\s*,\s*', args)
        out.write("<ol>\n")
        for page in pages:
            cursor = db.cursor()
            cursor.execute("select text from wiki where name='%s' order by version desc" % page)
            row = cursor.fetchone()
            if row:
                parse_toc(env, out, page, row[0])
            else:
                out.write('<div class="system-message"><strong>Error: Page %s does not exists</strong></div>' % page)
        out.write("</ol>\n")
        return out.getvalue()
    else:
        # return current page
        out.write("<ol>\n")
        page = hdf.getValue("args.page", "")
        parse_toc(env, out, page, hdf.getValue("wiki.page_source", ""))
        out.write("</ol>\n")
        return out.getvalue()# + "<pre>" + hdf.dump() + "</pre>"

