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.filters;
16  
17  import com.liferay.portal.kernel.util.StringBundler;
18  import com.liferay.portal.kernel.util.StringUtil;
19  
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  import org.stringtree.factory.AbstractStringFetcher;
24  
25  /**
26   * <a href="CodeBlock.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Raymond Augé
29   */
30  public class CodeBlock extends AbstractStringFetcher {
31  
32      public Object getObject(String content) {
33          StringBundler sb = new StringBundler();
34  
35          Matcher matcher = _pattern.matcher(content);
36  
37          if (matcher.matches()) {
38              sb.append("<div class=\"wiki-code\">");
39  
40              String[] lines = matcher.group(1).split("\\n");
41  
42              for (int i = 0; i < lines.length; i++) {
43                  if (i != 0) {
44                      sb.append("<br />");
45                  }
46  
47                  String translation = StringUtil.replace(
48                      lines[i],
49                      new String[] {
50                          "\\s",
51                          "<",
52                          ">",
53                          "=",
54                          "\"",
55                          "'",
56                          "\t"
57                      },
58                      new String[] {
59                          "&nbsp;",
60                          "&lt;",
61                          "&gt;",
62                          "&#x003D;",
63                          "&#0034;",
64                          "&#0039;",
65                          "&nbsp;&#8594;&nbsp;"
66                      });
67  
68                  int padlength =
69                      String.valueOf(lines.length).length() -
70                      String.valueOf(i + 1).length();
71  
72                  String padding = "";
73  
74                  for (int j = 0; j < padlength; j++) {
75                      padding += "&#0149;";
76                  }
77  
78                  sb.append("<span class=\"code-lines\">");
79                  sb.append(padding + (i + 1));
80                  sb.append("</span>");
81  
82                  sb.append(translation);
83              }
84  
85              sb.append("</div>");
86  
87              content = sb.toString();
88          }
89  
90          return content;
91      }
92  
93      private static Pattern _pattern = Pattern.compile(
94          "\\[code\\]((.|\\n)*?)\\[/code\\]",
95          Pattern.MULTILINE | Pattern.UNIX_LINES);
96  
97  }