View Javadoc
1   /*
2    * Copyright (C) 2003-2010 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.wiki.rendering.render.confluence;
18  
19  import java.util.Map;
20  
21  import org.xwiki.rendering.internal.renderer.ParametersPrinter;
22  
23  /**
24   * Generates Confluence Syntax for a Macro Block.
25   */
26  public class ConfluenceSyntaxMacroRenderer {
27    
28    private ParametersPrinter parametersPrinter = new ParametersPrinter();
29  
30    public String renderMacro(String id, Map<String, String> parameters, String content, boolean isInline) {
31      StringBuffer buffer = new StringBuffer();
32  
33      // Print begin macro
34      buffer.append("{");
35      buffer.append(id);
36  
37      // Print parameters
38      if (!parameters.isEmpty()) {
39        buffer.append(':');
40        buffer.append(renderMacroParameters(parameters));
41      }
42  
43      buffer.append("}");
44      // Print content and end macro
45      if (content != null && content.length() > 0) {
46        if (!isInline && !content.startsWith("\n")) {
47          buffer.append("\n");
48        }
49        buffer.append(content);
50        if (!isInline && !content.endsWith("\n")) {
51          buffer.append("\n");
52        }
53        //TODO: Confluence parser has bug in some case, ex:
54        //  ||What you need to type||What you will get||
55        //  |{color:red}look ma, red text!{color}|{color:red}look ma, red text!{color}|
56        //content is : look ma, red text!{color}
57        if (!content.endsWith("{" + id + "}")) {
58          buffer.append("{").append(id).append("}");
59        }
60      }
61      
62      return buffer.toString();
63    }
64  
65    public String renderMacroParameters(Map<String, String> parameters) {
66      return this.parametersPrinter.print(parameters, '~').replace("}", "~}");
67    }
68  }