001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.engines.jspwiki.plugin;
016    
017    import com.ecyrd.jspwiki.WikiContext;
018    import com.ecyrd.jspwiki.parser.Heading;
019    import com.ecyrd.jspwiki.plugin.PluginException;
020    
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    
024    import java.util.Map;
025    
026    /**
027     * <p>
028     * This is a modification of JSPWiki's core TableOfContents plugin for use
029     * within Liferay. This plugin modifies the original behavior by producing
030     * ordered lists and making contents collapsable.
031     * </p>
032     *
033     * @author Alexander Chow
034     * @author Jorge Ferrer
035     */
036    public class TableOfContents extends com.ecyrd.jspwiki.plugin.TableOfContents {
037    
038            public String execute(WikiContext context, Map params)
039                    throws PluginException {
040    
041                    if (!params.containsKey(PARAM_NUMBERED)) {
042                            params.put(PARAM_NUMBERED, Boolean.TRUE.toString());
043                    }
044    
045                    String result = super.execute(context, params);
046    
047                    if (_count < 2) {
048                            return StringPool.BLANK;
049                    }
050    
051                    int x = result.indexOf("<div class=\"collapsebox\">\n");
052    
053                    x = result.indexOf("</h4>", x);
054    
055                    int y = x + "</h4>".length();
056    
057                    if ((x != -1) && (y != -1)) {
058                            StringBundler sb = new StringBundler(15);
059    
060                            sb.append(result.substring(0, x));
061                            sb.append(StringPool.NBSP);
062                            sb.append("<a class=\"toc-trigger\" href=\"javascript:;\">[-]</a>");
063                            sb.append("</h4>");
064    
065                            sb.append("<div class=\"toc-index\">\n");
066                            sb.append(result.substring(y));
067                            sb.append("</div>\n");
068    
069                            result = sb.toString();
070                    }
071    
072                    return result;
073            }
074    
075            public void headingAdded(WikiContext context, Heading heading) {
076                    super.headingAdded(context, heading);
077    
078                    _count++;
079            }
080    
081            private int _count;
082    
083    }