/* 
    JSPWiki - a JSP-based WikiWiki clone.

    Copyright (C) 2004 Janne Jalkanen (Janne.Jalkanen@iki.fi)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser 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
 */
package com.ecyrd.jspwiki.plugin;

import org.apache.log4j.Logger;
import com.ecyrd.jspwiki.*;
import com.ecyrd.jspwiki.parser.Heading;
import com.ecyrd.jspwiki.parser.HeadingListener;
import com.ecyrd.jspwiki.parser.JSPWikiMarkupParser;
import com.ecyrd.jspwiki.render.RenderingManager;

import java.util.*;
import java.io.StringReader;
import java.io.IOException;

/**
 *  Provides a table of contents.
 *
 *  @since 2.2
 *  @author Janne Jalkanen
 */
public class TableOfContents
    implements WikiPlugin, HeadingListener
{
    private static Logger log = Logger.getLogger( TableOfContents.class );

    public static final String PARAM_TITLE = "title";

    StringBuffer m_buf = new StringBuffer();

    public void headingAdded( WikiContext context, Heading hd )
    {
        log.debug("HD: "+hd.m_level+", "+hd.m_titleText+", "+hd.m_titleAnchor);

        switch( hd.m_level )
        {
          case Heading.HEADING_SMALL:
            m_buf.append("***");
            break;
          case Heading.HEADING_MEDIUM:
            m_buf.append("**");
            break;
          case Heading.HEADING_LARGE:
            m_buf.append("*");
            break;
          default:
            throw new InternalWikiException("Unknown depth in toc! (Please submit a bug report.)");
        }

        m_buf.append(" ["+hd.m_titleText+"|"+context.getPage().getName()+"#"+hd.m_titleSection+"]\n");
    }

    public String execute( WikiContext context, Map params )
        throws PluginException
    {
        WikiEngine engine = context.getEngine();
        WikiPage   page   = context.getPage();

        StringBuffer sb = new StringBuffer();

        sb.append("<div class=\"toc\">\n");

        String title = (String) params.get(PARAM_TITLE);
        
        if( title != null )
        {
            sb.append("<h4>"+TextUtil.replaceEntities(title)+"</h4>\n");
        }
        else
        {
            sb.append("<h4>Table of Contents</h4>\n");
        }

        try
        {
            String wikiText = engine.getPureText( page );
            
            RenderingManager mgr = engine.getRenderingManager();

            JSPWikiMarkupParser parser = new JSPWikiMarkupParser( context,
                                                                  new StringReader(wikiText) );
            parser.addHeadingListener( this );

            parser.parse();
            
            parser.setInputReader( new StringReader(m_buf.toString()) );

            context.setVariable( JSPWikiMarkupParser.PROP_RUNPLUGINS, "false" );
            
            sb.append( mgr.getHTML( context, parser.parse() ) );
        }
        catch( IOException e )
        {
            log.error("Could not construct table of contents", e);
            throw new PluginException("Unable to construct table of contents (see logs)");
        }

        sb.append("</div>\n");

        return sb.toString();
    }

}

@@ -1813,11 +1813,33 @@
 nd(%22
-***
+%3Cli class=%5C%22toclevel-3%5C%22%3E
 %22);%0A
@@ -1922,18 +1922,41 @@
 append(%22
-**
+%3Cli class=%5C%22toclevel-2%5C%22%3E
 %22);%0A    
@@ -2034,17 +2034,41 @@
 append(%22
-*
+%3Cli class=%5C%22toclevel-1%5C%22%3E
 %22);%0A    
@@ -2224,45 +2224,154 @@
     
-m_buf.append(%22 %5B%22+hd.m_titleText+%22%7C%22+
+String url = context.getURL( WikiContext.VIEW, context.getPage().getName() );%0A        String sectref = %22#section-%22+context.getEngine().encodeName(
 cont
@@ -2397,11 +2397,12 @@
 me()
+)
 +%22
-#
+-
 %22+hd
@@ -2420,14 +2420,112 @@
 tion
-+%22%5D
+;%0A%0A        m_buf.append( %22%3Ca class=%5C%22wikipage%5C%22 href=%5C%22%22+url+sectref+%22%5C%22%3E%22+hd.m_titleText+%22%3C/a%3E%3C/li%3E
 %5Cn%22
+ 
 );%0A 
@@ -3190,74 +3190,8 @@
    %0A
-            RenderingManager mgr = engine.getRenderingManager();%0A%0A
     
@@ -3433,28 +3433,16 @@
 arse();%0A
-            
 %0A       
@@ -3450,225 +3450,54 @@
     
-parser.setInputReader( new StringReader(m_buf.toString()) );%0A%0A            context.setVariable( JSPWikiMarkupParser.PROP_RUNPLUGINS, %22false%22 );%0A            %0A            sb.append( mgr.getHTML( context, parser.parse() )
+sb.append( %22%3Cul%3E%5Cn%22+m_buf.toString()+%22%3C/ul%3E%5Cn%22
  );%0A

No longer generates a nested list, but named <li> -entries.