1
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
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
58 _regexps.put("'''((?s:.)*?)('''|(\n\n|\r\r|\r\n\r\n))", "**$1**$3");
59
60
62 _regexps.put("''((?s:.)*?)(''|(\n\n|\r\r|\r\n\r\n))", "//$1//$3");
63
64
66 _regexps.put(
67 "'''''((?s:.)*?)('''''|(\n\n|\r\r|\r\n\r\n))", "**//$1//**$3");
68
69
71 _regexps.put("\\[([^ ]*)\\]", "[[$1]]");
72
73
75 _regexps.put("\\[([^ ]+) (.*)\\]", "[[$1|$2]]");
76
77
79 _regexps.put("(^ (.+))(\\n (.+))*", "{{{\n$0\n}}}");
80
81
83 _regexps.put("^\\t[\\*] (.*)", "* $1");
84
85
87 _regexps.put("^\\t\\t[\\*] (.*)", "** $1");
88
89
91 _regexps.put("^\\t\\t\\t[\\*] (.*)", "*** $1");
92
93
95 _regexps.put("^\\t\\t\\t\\t[\\*] (.*)", "**** $1");
96
97
99 _regexps.put("^\\t1 (.*)", "# $1");
100
101
103 _regexps.put("^\\t\\t1 (.*)", "## $1");
104
105
107 _regexps.put("^\\t\\t\\t1 (.*)", "### $1");
108
109
111 _regexps.put("^\\t\\t\\t\\t1 (.*)", "#### $1");
112
113
115 _regexps.put("^\\t([\\w]+):\\t(.*)", "**$1**:\n$2");
116
117
119 _regexps.put("^\\t:\\t(.*)", "$1");
120
121
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 }