MediaWikiToCreoleTranslator extends BaseTranslator { @Override protected String postProcess(String content) { if (_strictImportMode) { content = runRegexp( content, "\\{{2}Special:(.*?)\\}{2}", StringPool.BLANK); content = runRegexp(content, "\\{{2}(.*?)\\}{2}", StringPool.BLANK); content = runRegexp( content, "(?s)\\{{2}(.*?)\\}{2}", StringPool.BLANK); } else { content = runRegexp( content, "\\{{2}Special:(.*?)\\}{2}", "{{{$1}}}\n"); content = runRegexp(content, "\\{{2}(.*?)\\}{2}", "{{{$1}}}"); content = runRegexp( content, "([^\\{])(\\{{2})([^\\{])", "$1\n{{{\n$3"); content = runRegexp( content, "([^\\}])(\\}{2})([^\\}])", "$1\n}}}\n$3"); } Matcher matcher1 = _titlePattern.matcher(content); if (matcher1.find()) { content = runRegexp(content, "^===([^=]+)===", "====$1===="); content = runRegexp(content, "^==([^=]+)==", "===$1==="); content = runRegexp(content, "^=([^=]+)=", "==$1=="); } for (Pattern pattern : _htmlTagPatterns) { matcher1 = pattern.matcher(content); content = matcher1.replaceAll(StringPool.BLANK); } for (String htmlTag : _HTML_TAGS) { content = StringUtil.removeSubstring(content, htmlTag); } matcher1 = _imagePattern.matcher(content); StringBuffer sb = new StringBuffer(content); int level = 0; int offset = 0; int originalLength = 0; int prefixLength = 0; while (matcher1.find()) { level = 0; prefixLength = matcher1.end(2) - matcher1.start(2); for (int i = matcher1.start(0) + offset; i < (sb.length() - 1); i++) { if ((sb.charAt(i) == '[') && (sb.charAt(i + 1) == '[')) { level++; } else if ((sb.charAt(i) == ']') && (sb.charAt(i + 1) == ']')) { level--; if (level == 0) { originalLength = (i + 2) - (matcher1.start(0) + offset); break; } } } int imageStartPos = matcher1.end(3) + offset; int imageEndPos = matcher1.start(2) + offset + originalLength - 4; String image = StringBundler.concat( "{{", MediaWikiImporter.SHARED_IMAGES_TITLE, "/", StringUtil.toLowerCase( sb.substring(imageStartPos, imageEndPos)), "}}"); int imageLength = image.length(); image = StringUtil.removeSubstring(image, "[["); image = StringUtil.removeSubstring(image, "]]"); sb.replace( matcher1.start(0) + offset, matcher1.start(0) + originalLength + offset, image); offset += MediaWikiImporter.SHARED_IMAGES_TITLE.length() - prefixLength - (imageLength - image.length()); } content = sb.toString(); matcher1 = _tablePattern.matcher(content); sb = new StringBuffer(content); String mediaWikiTable = null; offset = 0; originalLength = 0; while (matcher1.find()) { mediaWikiTable = sb.substring( matcher1.start(1) + offset, matcher1.end(1) + offset); originalLength = mediaWikiTable.length() + 4; Matcher matcher2 = _mediaWikiTablePattern1.matcher(mediaWikiTable); mediaWikiTable = matcher2.replaceAll(StringPool.BLANK); Matcher matcher3 = _mediaWikiTablePattern2.matcher(mediaWikiTable); mediaWikiTable = matcher3.replaceAll("$1"); Matcher matcher4 = _mediaWikiTablePattern3.matcher(mediaWikiTable); mediaWikiTable = matcher4.replaceAll("===$1==="); Matcher matcher5 = _mediaWikiTablePattern4.matcher(mediaWikiTable); mediaWikiTable = matcher5.replaceAll("|=$1|"); mediaWikiTable = StringUtil.replace( mediaWikiTable, CharPool.NEW_LINE, StringPool.BLANK); mediaWikiTable = StringUtil.replace( mediaWikiTable, CharPool.RETURN, StringPool.BLANK); mediaWikiTable = StringUtil.replace(mediaWikiTable, "|-", "\n\r"); mediaWikiTable = StringUtil.replace(mediaWikiTable, "||", "|"); mediaWikiTable = StringUtil.removeSubstring(mediaWikiTable, " sb.replace( matcher1.start(0) + offset, matcher1.start(0) + originalLength + offset, mediaWikiTable); offset += mediaWikiTable.length() - originalLength; } content = sb.toString(); content = runRegexp(content, "/{2}(\\{{3})", "$1"); content = runRegexp(content, "(\\}{3})/{2}", "$1"); matcher1 = _linkPattern.matcher(content); sb = new StringBuffer(content); while (matcher1.find()) { String link = matcher1.group(1); link = StringUtil.replace(link, CharPool.UNDERLINE, CharPool.SPACE); sb.replace(matcher1.start(1), matcher1.end(1), link); } return TABLE_OF_CONTENTS + super.postProcess(sb.toString()); } MediaWikiToCreoleTranslator(); void setStrictImportMode(boolean strictImportMode); static final String TABLE_OF_CONTENTS; }
@Test public void testHeaders1() throws Exception { String content = "= Header 1 =\n== Header 2 =="; String expected = MediaWikiToCreoleTranslator.TABLE_OF_CONTENTS + "== Header 1 ==\n=== Header 2 ==="; String actual = _mediaWikiToCreoleTranslator.postProcess( _translate(content)); Assert.assertEquals(expected, actual); }
@Test public void testHeaders2() throws Exception { String content = "== Header 1 ==\n=== Header 2 ==="; String expected = MediaWikiToCreoleTranslator.TABLE_OF_CONTENTS + content; String actual = _mediaWikiToCreoleTranslator.postProcess( _translate(content)); Assert.assertEquals(expected, actual); }
@Test public void testImage() throws Exception { String content = "test1 [[Image:Sample1.png]] test2 [[Image:Sample2.png]] test3 " + "[[Image:Sample3.png]] test4"; StringBundler sb = new StringBundler(4); sb.append(MediaWikiToCreoleTranslator.TABLE_OF_CONTENTS); sb.append("test1 {{SharedImages/sample1.png}} test2 "); sb.append("{{SharedImages/sample2.png}} test3 "); sb.append("{{SharedImages/sample3.png}} test4"); Assert.assertEquals( sb.toString(), _mediaWikiToCreoleTranslator.postProcess(_translate(content))); }
@Test public void testLinkWithUnderscores() throws Exception { String content = "[[Link_With_Underscores]]"; String expected = MediaWikiToCreoleTranslator.TABLE_OF_CONTENTS + "[[Link With Underscores]]"; String actual = _mediaWikiToCreoleTranslator.postProcess( _translate(content)); Assert.assertEquals(expected, actual); }
@Test public void testRemoveHTMLTags() throws Exception { String content = "text\n<br>\ntext<br><div align=\"right\">x</div>"; String expected = MediaWikiToCreoleTranslator.TABLE_OF_CONTENTS + "text\n\ntextx"; String actual = _mediaWikiToCreoleTranslator.postProcess( _translate(content)); Assert.assertEquals(expected, actual); }