1
22
23 package com.liferay.portlet.wiki.translators;
24
25 import com.liferay.portal.kernel.util.StringPool;
26 import com.liferay.portlet.wiki.importers.mediawiki.MediaWikiImporter;
27
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31
36 public class MediaWikiToCreoleTranslator extends BaseTranslator {
37
38 public static final String TABLE_OF_CONTENTS = "<<TableOfContents>>\n\n";
39
40 public MediaWikiToCreoleTranslator() {
41 initRegexps();
42 initNowikiRegexps();
43 }
44
45 protected void initNowikiRegexps() {
46
47
49 nowikiRegexps.add("(<nowiki>)(.*?)(</nowiki>)");
50 nowikiRegexps.add("(<pre>)(.*?)(</pre>)");
51
52
54 nowikiRegexps.add(
55 "~(\\*\\*|~|//|-|#|\\{\\{|}}|\\\\|~\\[~~[|]]|----|=|\\|)");
56 }
57
58 protected void initRegexps() {
59
60
62 regexps.put("= '''([^=]+)''' =", "= $1 =");
63 regexps.put("== '''([^=]+)''' ==", "== $1 ==");
64 regexps.put("== '''([^=]+)''' ===", "=== $1 ===");
65
66
68 regexps.put("<", "<");
69 regexps.put(">", ">");
70
71
73 regexps.put("\\[\\[[Cc]ategory:([^\\]]*)\\]\\][\\n]*", "");
74
75
77 regexps.put("\\{{2}OtherTopics\\|([^\\}]*)\\}{2}", StringPool.BLANK);
78
79
81 regexps.put("\\{{2}Work in progress\\}{2}", StringPool.BLANK);
82
83
85 regexps.put(
86 "''''((?s:.)*?)(''''|(\n\n|\r\r|\r\n\r\n))", "**//$1//**$3");
87
88
90 regexps.put("'''((?s:.)*?)('''|(\n\n|\r\r|\r\n\r\n))", "**$1**$3");
91
92
94 regexps.put("''((?s:.)*?)(''|(\n\n|\r\r|\r\n\r\n))", "//$1//$3");
95
96
98 regexps.put("\\[{2}((http|ftp)[^ ]*) ([^\\]]*)\\]{2}", "[$1 $3]");
99
100
102 regexps.put("\\[((http|ftp)[^ ]*)\\]", "[[$1]]");
103
104
106 regexps.put("\\[((http|ftp)[^ ]*) ([^\\]]*)\\]", "[[$1|$3]]");
107
108
110 regexps.put("^\\t([\\w]+):\\t(.*)", "**$1**:\n$2");
111
112
114 regexps.put("^\\t:\\t(.*)", "$1");
115
116
118 regexps.put("(^ (.+))(\\n (.+))*", "{{{\n$0\n}}}");
119
120
122 regexps.put("<nowiki>([^<]*)</nowiki>", "{{{$1}}}");
123
124
126 regexps.put("<pre>([^<]*)</pre>", "{{{$1}}}");
127
128
130 regexps.put("[-]*\\[{2}User:([^\\]]*)\\]{2}", "$1");
131 }
132
133 protected String postProcess(String content) {
134
135
137 Matcher matcher = Pattern.compile(
138 "^=([^=]+)=", Pattern.MULTILINE).matcher(content);
139
140 if (matcher.find()) {
141 content = runRegexp(content, "^===([^=]+)===", "====$1====");
142 content = runRegexp(content, "^==([^=]+)==", "===$1===");
143 content = runRegexp(content, "^=([^=]+)=", "==$1==");
144 }
145
146
148 for (int i = 0; i < _HTML_TAGS.length; i++) {
149 content = content.replaceAll(_HTML_TAGS[i], StringPool.BLANK);
150 }
151
152
154 matcher = Pattern.compile(
155 "\\[{2}Image:([^\\]]*)\\]{2}", Pattern.DOTALL).matcher(content);
156
157 StringBuffer sb = new StringBuffer(content);
158
159 int offset = 0;
160
161 while (matcher.find()) {
162 String image =
163 "{{" + MediaWikiImporter.SHARED_IMAGES_TITLE + "/" +
164 matcher.group(1).toLowerCase() + "}}";
165
166 sb.replace(
167 matcher.start(0) + offset, matcher.end(0) + offset, image);
168
169 offset += MediaWikiImporter.SHARED_IMAGES_TITLE.length() - 5;
170 }
171
172 content = sb.toString();
173
174
176 matcher = Pattern.compile(
177 "\\[{2}([^\\]]*)\\]{2}", Pattern.DOTALL).matcher(content);
178
179 sb = new StringBuffer(content);
180
181 while (matcher.find()) {
182 String link = matcher.group(1).replace(
183 StringPool.UNDERLINE, StringPool.SPACE);
184
185 sb.replace(matcher.start(1), matcher.end(1), link);
186 }
187
188 return TABLE_OF_CONTENTS + super.postProcess(sb.toString());
189 }
190
191 private static final String[] _HTML_TAGS = {
192 "<blockquote>", "</blockquote>", "<br>", "<br/>", "<br />", "<center>",
193 "</center>", "<cite>", "</cite>","<code>", "</code>", "<div[^>]*>",
194 "</div>", "<font[^>]*>", "</font>", "<hr>", "<hr/>", "<hr />", "<p>",
195 "</p>", "<tt>", "</tt>", "<var>", "</var>"};
196
197 }