"""
Inserts a link to the "parent" wiki entry.  

This only applies to wikis that have a "/" in their name indicating heirarchy.  

e.g. an entry named Java/Introduction will have a parent of Java.  All other wiki entries have a parent of WikiStart.
"""

import re
from StringIO import StringIO
from trac import util

def execute(hdf, args, env):
    db = env.get_db_cnx()
    cursor = db.cursor()

    buf = StringIO()

    prefix = None
    if args:
        prefix = args.replace('\'', '\'\'')
    else :
	prefix = hdf.getValue('wiki.page_name', '') + '/'


    parent = 'WikiStart'

    m = re.search("(\S+)/(\S+)$", prefix)
    if m:
    	parent = m.group(1)

    buf.write('<a href="%s">' % env.href.wiki(util.escape(parent)) )
    buf.write(parent)
    buf.write('</a>\n')

    return buf.getvalue()
