1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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      public String execute(WikiContext context, Map params)
42          throws PluginException {
43  
44          if (!params.containsKey(PARAM_NUMBERED)) {
45              params.put(PARAM_NUMBERED, Boolean.TRUE.toString());
46          }
47  
48          String result = super.execute(context, params);
49  
50          if (_count < 2) {
51              return StringPool.BLANK;
52          }
53  
54          int x = result.indexOf("<div class=\"collapsebox\">\n");
55  
56          x = result.indexOf("</h4>", x);
57  
58          int y = x + "</h4>".length();
59  
60          if ((x != -1) && (y != -1)) {
61              String id = "toc_" + PwdGenerator.getPassword();
62  
63              StringBundler sb = new StringBundler(15);
64  
65              sb.append(result.substring(0, x));
66              sb.append(StringPool.NBSP);
67              sb.append("<span style=\"cursor: pointer;\" ");
68              sb.append("onClick=\"Liferay.Util.toggleByIdSpan(this, '");
69              sb.append(id);
70              sb.append("'); self.focus();\">[");
71              sb.append("<span>-</span>");
72              sb.append("<span style=\"display: none;\">+</span>");
73              sb.append("]</span>\n");
74              sb.append("</h4>");
75  
76              sb.append("<div id=\"");
77              sb.append(id);
78              sb.append("\">\n");
79              sb.append(result.substring(y));
80              sb.append("</div>\n");
81  
82              result = sb.toString();
83          }
84  
85          return result;
86      }
87  
88      public void headingAdded(WikiContext context, Heading heading) {
89          super.headingAdded(context, heading);
90  
91          _count++;
92      }
93  
94      private int _count;
95  
96  }