# Macros for the HierWiki plugin

from trac.wiki.api import parse_args
from trac.wiki.macros import WikiMacroBase
from trac.wiki.model import WikiPage

from genshi.builder import tag

from StringIO import StringIO
import re, string, inspect

__all__ = ['AutoNavMacro']

class SubWikiMacro(WikiMacroBase):
    """
    Inserts an alphabetic list of sub-wiki pages into the output.
    A sub-wiki page is a page that is is deeper in the hierachy than the current page.  e.g. if the current page is People, the this will return a list of all wiki entries that start with "People/"
    
    Accepts a prefix string as parameter: if provided, only pages with names that
    start with the prefix are included in the resulting list. If this parameter is
    omitted, all pages are listed.
    
    This now takes the text of the first heading and displays it as the link name.
    """

    # TODO: Everything until render_macro can be removed once switched to be based on WikiMacroBase
        
    def expand_macro(self, formatter, name, content):
        # Args seperated by commas:
        # prefix,level
        #
        # Page Name prefix to search for.
        # how many 'levels' in the hierarchy to go down.
        #db = self.env.get_db_cnx()
        args, kw = parse_args(content)

        req = formatter.req
        context = formatter.context
        resource = context.resource

        if not resource.realm == 'wiki':
            raise TracError('\'SubWikiMacro\' macro can only be used in Wiki pages.')

        cursor = formatter.db.cursor()
        cs = formatter.db.cursor()

        page = WikiPage(self.env, resource)
        page_url = req.href.wiki(resource.id)
        prefix = page.name + '/'


        level = None
        if args:
            if args[0] != 'None':
                prefix = args[0]
            if len(args) > 1 and args[1] != 'None':
                level = args[1]
        
        sql = 'SELECT DISTINCT name FROM wiki '
        if prefix:
            sql += 'WHERE name LIKE \'%s%%\' ' % prefix
        sql += 'ORDER BY name'
        cursor.execute(sql)

        buf = StringIO()
        buf.write('<ul>')

        while 1:
            row = cursor.fetchone()
            if row == None:
                break
            name = row[0]
            if level:
                len_name = name.split('/')
                #buf.write(len_name)
                if len(len_name) > int(level)+1:
                    continue
            # Get the latest revision only.
            
            sql = 'SELECT name,text from wiki where name = \'%s\' order by version desc limit 1' % name
            cs.execute(sql)
            while 1:
                csrow = cs.fetchone()
                if csrow == None:
                    break
                name = csrow[0]
                text = csrow[1]
                (linktext,title,desc) = self._getinfo(formatter.db,name)

                l = formatter.href.wiki(name)

                buf.write('<li><a title="%s" href="%s">' % (title,l))
                buf.write(linktext)
                buf.write('</a> %s</li>\n' % desc)
            
        buf.write('</ul>')

        return buf.getvalue()

    def _getinfo(self, db, name):
        cs = db.cursor()
        desc = name
        # Get the latest revision only.
        cs.execute('SELECT text from wiki where name = \'%s\' order by version desc limit 1' % name)
        csrow = cs.fetchone()
        prefix = ''

        if csrow != None:
            text = csrow[0]
            m  = re.search('=+\s([^=]*)=+',text)
            if m != None:
                desc = string.strip(m.group(1))
        else:
            prefix = "Create "

        title = StringIO()
        title.write("%s%s"%(prefix, desc))
        if prefix != '' or desc == name:
            desc = ''

        return (name,title.getvalue(),desc)

