1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.wiki.engines.jspwiki.plugin;
16  
17  import com.ecyrd.jspwiki.WikiContext;
18  import com.ecyrd.jspwiki.parser.Heading;
19  import com.ecyrd.jspwiki.plugin.PluginException;
20  
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.util.PwdGenerator;
24  
25  import java.util.Map;
26  
27  /**
28   * <a href="TableOfContents.java.html"><b><i>View Source</i></b></a>
29   *
30   * <p>
31   * This is a modification of JSPWiki's core TableOfContents plugin for use
32   * within Liferay. This plugin modifies the original behavior by producing
33   * ordered lists and making contents collapsable.
34   * </p>
35   *
36   * @author Alexander Chow
37   * @author Jorge Ferrer
38   */
39  public class TableOfContents extends com.ecyrd.jspwiki.plugin.TableOfContents {
40  
41      @SuppressWarnings("rawtypes")
42      public String execute(WikiContext context, Map params)
43          throws PluginException {
44  
45          if (!params.containsKey(PARAM_NUMBERED)) {
46              params.put(PARAM_NUMBERED, Boolean.TRUE.toString());
47          }
48  
49          String result = super.execute(context, params);
50  
51          if (_count < 2) {
52              return StringPool.BLANK;
53          }
54  
55          int x = result.indexOf("<div class=\"collapsebox\">\n");
56  
57          x = result.indexOf("</h4>", x);
58  
59          int y = x + "</h4>".length();
60  
61          if ((x != -1) && (y != -1)) {
62              String id = "toc_" + PwdGenerator.getPassword();
63  
64              StringBundler sb = new StringBundler(15);
65  
66              sb.append(result.substring(0, x));
67              sb.append(StringPool.NBSP);
68              sb.append("<span style=\"cursor: pointer;\" ");
69              sb.append("onClick=\"Liferay.Util.toggleByIdSpan(this, '");
70              sb.append(id);
71              sb.append("'); self.focus();\">[");
72              sb.append("<span>-</span>");
73              sb.append("<span style=\"display: none;\">+</span>");
74              sb.append("]</span>\n");
75              sb.append("</h4>");
76  
77              sb.append("<div id=\"");
78              sb.append(id);
79              sb.append("\">\n");
80              sb.append(result.substring(y));
81              sb.append("</div>\n");
82  
83              result = sb.toString();
84          }
85  
86          return result;
87      }
88  
89      public void headingAdded(WikiContext context, Heading heading) {
90          super.headingAdded(context, heading);
91  
92          _count++;
93      }
94  
95      private int _count;
96  
97  }