1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.wiki.engines.jspwiki.plugin;
24  
25  import com.ecyrd.jspwiki.WikiContext;
26  import com.ecyrd.jspwiki.parser.Heading;
27  import com.ecyrd.jspwiki.plugin.PluginException;
28  
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.util.PwdGenerator;
31  
32  import java.util.Map;
33  
34  /**
35   * <a href="TableOfContents.java.html"><b><i>View Source</i></b></a>
36   *
37   * <p>
38   * This is a modification of JSPWiki's core TableOfContents plugin for use
39   * within Liferay. This plugin modifies the original behavior by producing
40   * ordered lists and making contents collapsable.
41   * </p>
42   *
43   * @author Alexander Chow
44   * @author Jorge Ferrer
45   *
46   */
47  public class TableOfContents extends com.ecyrd.jspwiki.plugin.TableOfContents {
48  
49      public String execute(WikiContext context, Map params)
50          throws PluginException {
51  
52          if (!params.containsKey(PARAM_NUMBERED)) {
53              params.put(PARAM_NUMBERED, Boolean.TRUE.toString());
54          }
55  
56          String result = super.execute(context, params);
57  
58          if (_count < 2) {
59              return StringPool.BLANK;
60          }
61  
62          int x = result.indexOf("<div class=\"collapsebox\">\n");
63  
64          x = result.indexOf("</h4>", x);
65  
66          int y = x + "</h4>".length();
67  
68          if ((x != -1) && (y != -1)) {
69              String id = "toc_" + PwdGenerator.getPassword();
70  
71              StringBuilder sb = new StringBuilder();
72  
73              sb.append(result.substring(0, x));
74              sb.append(StringPool.NBSP);
75              sb.append("<span style=\"cursor: pointer;\" ");
76              sb.append("onClick=\"Liferay.Util.toggleByIdSpan(this, '");
77              sb.append(id);
78              sb.append("'); self.focus();\">[");
79              sb.append("<span>-</span>");
80              sb.append("<span style=\"display: none;\">+</span>");
81              sb.append("]</span>\n");
82              sb.append("</h4>");
83  
84              sb.append("<div id=\"");
85              sb.append(id);
86              sb.append("\">\n");
87              sb.append(result.substring(y));
88              sb.append("</div>\n");
89  
90              result = sb.toString();
91          }
92  
93          return result;
94      }
95  
96      public void headingAdded(WikiContext context, Heading heading) {
97          super.headingAdded(context, heading);
98  
99          _count++;
100     }
101 
102     private int _count;
103 
104 }