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  public class TableOfContents extends com.ecyrd.jspwiki.plugin.TableOfContents {
47  
48      public String execute(WikiContext context, Map params)
49          throws PluginException {
50  
51          if (!params.containsKey(PARAM_NUMBERED)) {
52              params.put(PARAM_NUMBERED, Boolean.TRUE.toString());
53          }
54  
55          String result = super.execute(context, params);
56  
57          if (_count < 2) {
58              return StringPool.BLANK;
59          }
60  
61          int x = result.indexOf("<div class=\"collapsebox\">\n");
62  
63          x = result.indexOf("</h4>", x);
64  
65          int y = x + "</h4>".length();
66  
67          if ((x != -1) && (y != -1)) {
68              String id = "toc_" + PwdGenerator.getPassword();
69  
70              StringBuilder sb = new StringBuilder();
71  
72              sb.append(result.substring(0, x));
73              sb.append(StringPool.NBSP);
74              sb.append("<span style=\"cursor: pointer;\" ");
75              sb.append("onClick=\"Liferay.Util.toggleByIdSpan(this, '");
76              sb.append(id);
77              sb.append("'); self.focus();\">[");
78              sb.append("<span>-</span>");
79              sb.append("<span style=\"display: none;\">+</span>");
80              sb.append("]</span>\n");
81              sb.append("</h4>");
82  
83              sb.append("<div id=\"");
84              sb.append(id);
85              sb.append("\">\n");
86              sb.append(result.substring(y));
87              sb.append("</div>\n");
88  
89              result = sb.toString();
90          }
91  
92          return result;
93      }
94  
95      public void headingAdded(WikiContext context, Heading heading) {
96          super.headingAdded(context, heading);
97  
98          _count++;
99      }
100 
101     private int _count;
102 
103 }