001    /**
002     * Copyright (c) 2000-2013 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            @Override
039            @SuppressWarnings("rawtypes")
040            public String execute(WikiContext context, Map params)
041                    throws PluginException {
042    
043                    if (!params.containsKey(PARAM_NUMBERED)) {
044                            params.put(PARAM_NUMBERED, Boolean.TRUE.toString());
045                    }
046    
047                    String result = super.execute(context, params);
048    
049                    if (_count < 2) {
050                            return StringPool.BLANK;
051                    }
052    
053                    int x = result.indexOf("<div class=\"collapsebox\">\n");
054    
055                    x = result.indexOf("</h4>", x);
056    
057                    int y = x + "</h4>".length();
058    
059                    if ((x != -1) && (y != -1)) {
060                            StringBundler sb = new StringBundler(15);
061    
062                            sb.append(result.substring(0, x));
063                            sb.append(StringPool.NBSP);
064                            sb.append("<a class=\"toc-trigger\" href=\"javascript:;\">[-]</a>");
065                            sb.append("</h4>");
066    
067                            sb.append("<div class=\"toc-index\">\n");
068                            sb.append(result.substring(y));
069                            sb.append("</div>\n");
070    
071                            result = sb.toString();
072                    }
073    
074                    return result;
075            }
076    
077            @Override
078            public void headingAdded(WikiContext context, Heading heading) {
079                    super.headingAdded(context, heading);
080    
081                    _count++;
082            }
083    
084            private int _count;
085    
086    }