# -*- coding: utf-8 -*-
"""
= TracNav: The navigation bar for Trac =

This macro implements a fully customizable navigation bar for the Trac
wiki engine. The contents of the navigation bar is a wiki page itself
and can be edited like any other wiki page through the web
interface. The navigation bar supports hierarchical ordering of
topics. The design of TracNav mimics the design of the TracGuideToc
that was originally supplied with Trac. The drawback of TracGuideToc
is that it is not customizable without editing its source code and
that it does not support hierarchical ordering.


== Installation ==

To install TracNav, place the file `TracNav.py` in the `wiki-macros`
subdirectory and the accompanying `tracnav.css` file in the
`templates` subdirectory of your Trac project. Add this line
{{{
@import url(<?cs var:chrome.href ?>/site/tracnav.css);
}}}
to the `templates/site_css.cs` file of your Trac project.

The `tracnav.css` file defines the styles for displaying the
navigation bar. These styles build upon the styles for !TracGuideToc
that come with your Trac distribution. If you just install the macro
but miss to install the style file, TracNav will work but look
somewhat strange.


== Usage ==

To use TracNav, create an index page for your site and call the
TracNav macro on each page, where the navigation bar should be
displayed. The index page is a regular wiki page. The page with the
table of contents must include an unordered list of links that should
be displayed in the navigation bar.

To display the navigation bar on a page, you must call the TracNav
macro on that page an pass the name of your table of contents as
argument.


== Author and license ==

Copyright 2005 Bernhard Haumacher (haui at haumacher.de)

{{{
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
}}}

== Additional information and a life example ==

Please visit: http://svn.ipd.uka.de/trac/javaparty/wiki/TracNav
"""

import re
import sys
from trac.wiki.api import WikiSystem
from trac.wiki.model import WikiPage

listRule = re.compile(r"""^(?P<indent> *)\* +(?:(?P<wikilink>\[wiki:(?P<link>(&#34;([^&#34;]*)&#34;|'([^']*)')|([^ \]]+)) +(?P<label>[^\]]*)\])|(?P<text>.*))""", re.M)

def getToc(hdf, env, curpage, name):
    tocText = "* Table of contents"

    preview = hdf.getValue('args.preview', "")
    if preview and (name == curpage):
        tocText = hdf.getValue('wiki.page_source', tocText);
    else:
        if WikiSystem(env).has_page(name):
            tocText = WikiPage(env, name).text

    # env.log.debug(tocText)
    return tocText


def getTocEntry(tocText):
    nextPos = 0
    while 1:
        match = listRule.search(tocText, nextPos)
        if not match:
            # env.log.debug("No more matches")
            return

        indent = len(match.group('indent'))
        if match.group('wikilink'):
            link = match.group('link')
            label = match.group('label')
        else:
            link = None
            label = match.group('text')

        # if link == None:
        #     env.log.debug(label + " ---")
        # else:
        #     env.log.debug(label + ": " + link)
    
        yield indent, link, label
        nextPos = match.end()


def parseToc(tocText):
    stack = []
    for indent, link, label in getTocEntry(tocText):

	if len(stack) == 0:
            stack.append((indent, []))

        (lastIndent, list) = stack[len(stack) - 1]

        if indent > lastIndent:
            stack.append((indent, [(link, label, None)]))
        elif indent == lastIndent:
            list.append((link, label, None))
        else:
            while indent < lastIndent:
                (_, list) = stack.pop()
                (lastIndent, topList) = stack[len(stack) - 1]
                (topLink, topLabel, _) = topList[len(topList) - 1]
                topList[len(topList) - 1] = (topLink, topLabel, list)

            (lastIndent, list) = stack[len(stack) - 1]
            list.append((link, label, None))
                
    while len(stack) > 1:
        (_, list) = stack.pop()
        (_, topList) = stack[len(stack) - 1]
        (topLink, topLabel, _) = topList[len(topList) - 1]
        topList[len(topList) - 1] = (topLink, topLabel, list)

    (_, list) = stack.pop()
    return list


def execute(hdf, args, env):
    preview = hdf.getValue('args.preview', "")
    curpage = hdf.getValue('wiki.page_name', "")
    name = args
    if not name:
        name = 'TOC'

    db = env.get_db_cnx()
    toc = parseToc(getToc(hdf, env, curpage, name))
    if not toc:
        msg = ''
        msg += '<div class="system-message"><strong>Error: Table of contents does not exist.'
        if (not preview) and (hdf.getValue('trac.acl.WIKI_MODIFY', '')):
            msg += ' Click here to <a href="%s?edit=yes">edit</a>.' % env.href.wiki(name)
        msg += '</strong></div>\n'
        return msg

    (found, filtered) = filter(curpage, toc, 0)
    if found:
        return displayAll(hdf, env, name, curpage, filtered, 0)
    else:
        return displayAll(hdf, env, name, curpage, toc, 0)


def filter(curpage, toc, level):
    found = 0
    result = []
    for name, title, sub in toc:
        if sub == None:
            if name == curpage:
                found = 1
            result.append((name, title, None))
        else:
            (subfound, subtoc) = filter(curpage, sub, level + 1)
            if subfound:
                found = 1
            if subfound or (name == None):
                if level == 0 and name != None:
                    prepended = [(name, title, subtoc)]
                    prepended.extend(result)
                    result = prepended
                else:
                    result.append((name, title, subtoc))
            else:
                result.append((name, title, []))
    return (found, result)

def indentation(col):
    return ' ' * col

def displayAll(hdf, env, name, curpage, toc, col):
    preview = hdf.getValue('args.preview', "")
    html = ''
    html += '%s<div class="wiki-toc trac-nav">\n' % indentation(col)
    col += 1

    html += '%s<h2><a href="http://svn.ipd.uka.de/trac/javaparty/wiki/TracNav">TracNav</a> menu</h2>' % indentation(col)

    if (not preview) and hdf.getValue('trac.acl.WIKI_MODIFY', ''):
        html += '%s<div class="edit"><a href="%s?edit=yes">edit</a></div>\n' % (indentation(col), env.href.wiki(name))
    html += '%s<ul>\n' % indentation(col)
    col += 1
    html += display(env, curpage, toc, 0, col)
    col -= 1
    html += '%s</ul>\n' % indentation(col)
    col -= 1
    html += '%s</div>\n' % indentation(col)
    return html

def display(env, curpage, toc, depth, col):
    html = ''
    for name, title, sub in toc:
        liStyle = ' style="padding-left: %dem;"' % (depth + 1)
        if sub == None:
            if name == curpage:
                cls = ' class="active"'
            else:
                cls = ''
            html += '%s<li%s%s>' % (indentation(col), liStyle, cls)
            if name == None:
                html += title
            else:
                html += '<a href="%s">%s</a>' % (env.href.wiki(name), title)
            html += '</li>\n'
        else:
            html += '%s<li%s>\n' % (indentation(col), liStyle)
            col += 1
            if name == None or len(sub) > 0:
                html += '%s<h4>%s</h4>\n' % (indentation(col), title)
            else:
                html += '%s<h4><a href="%s">%s</a>...</h4>\n' % (indentation(col), env.href.wiki(name), title)
            col -= 1
            html += '%s</li>\n' % indentation(col)
            if len(sub) > 0:
                html += display(env, curpage, sub, depth + 1, col)
    return html

