| ClassicToCreoleTranslator.java |
1 /**
2 * Copyright (c) 2000-2008 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.translators;
24
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 /**
31 * <a href="ClassicToCreoleTranslator.java.html"><b><i>View Source</i></b></a>
32 *
33 * @author Jorge Ferrer
34 *
35 */
36 public class ClassicToCreoleTranslator {
37
38 public ClassicToCreoleTranslator() {
39 initRegexps();
40 }
41
42 public String translate(String content) {
43 content = normalize(content);
44
45 for (String regexp: _regexps.keySet()) {
46 String replacement = _regexps.get(regexp);
47
48 content = runRegexp(content, regexp, replacement);
49 }
50
51 return content;
52 }
53
54 protected void initRegexps() {
55
56 // Bold
57
58 _regexps.put("'''((?s:.)*?)('''|(\n\n|\r\r|\r\n\r\n))", "**$1**$3");
59
60 // Italics
61
62 _regexps.put("''((?s:.)*?)(''|(\n\n|\r\r|\r\n\r\n))", "//$1//$3");
63
64 // Bold and italics
65
66 _regexps.put(
67 "'''''((?s:.)*?)('''''|(\n\n|\r\r|\r\n\r\n))", "**//$1//**$3");
68
69 // Link
70
71 _regexps.put("\\[([^ ]*)\\]", "[[$1]]");
72
73 // Link with label
74
75 _regexps.put("\\[([^ ]+) (.*)\\]", "[[$1|$2]]");
76
77 // Monospace
78
79 _regexps.put("(^ (.+))(\\n (.+))*", "{{{\n$0\n}}}");
80
81 // List item
82
83 _regexps.put("^\\t[\\*] (.*)", "* $1");
84
85 // List subitem
86
87 _regexps.put("^\\t\\t[\\*] (.*)", "** $1");
88
89 // List subsubitem
90
91 _regexps.put("^\\t\\t\\t[\\*] (.*)", "*** $1");
92
93 // List subsubsubitem
94
95 _regexps.put("^\\t\\t\\t\\t[\\*] (.*)", "**** $1");
96
97 // Ordered list item
98
99 _regexps.put("^\\t1 (.*)", "# $1");
100
101 // Ordered list subitem
102
103 _regexps.put("^\\t\\t1 (.*)", "## $1");
104
105 // Ordered list subsubitem
106
107 _regexps.put("^\\t\\t\\t1 (.*)", "### $1");
108
109 // Ordered list subsubsubitem
110
111 _regexps.put("^\\t\\t\\t\\t1 (.*)", "#### $1");
112
113 // Term and definition
114
115 _regexps.put("^\\t([\\w]+):\\t(.*)", "**$1**:\n$2");
116
117 // Indented paragraph
118
119 _regexps.put("^\\t:\\t(.*)", "$1");
120
121 // CamelCase
122
123 _regexps.put(
124 "(^|\\p{Punct}|\\p{Space})((\\p{Lu}\\p{Ll}+){2,})" +
125 "(\\z|\\n|\\p{Punct}|\\p{Space})", " [[$2]] ");
126 }
127
128 protected String normalize(String content) {
129 content = content.replace("\r\n", "\n");
130 content = content.replace("\r", "\n");
131
132 return content;
133 }
134
135 protected String runRegexp(
136 String content, String regexp, String replacement) {
137
138 Matcher matcher =
139 Pattern.compile(regexp, Pattern.MULTILINE).matcher(content);
140
141 StringBuffer sb = new StringBuffer();
142
143 while (matcher.find()) {
144 matcher.appendReplacement(sb, replacement);
145 }
146
147 matcher.appendTail(sb);
148
149 return sb.toString();
150 }
151
152 private Map<String, String> _regexps = new LinkedHashMap<String, String>();
153
154 }