📄 creoletojspwikitranslator.java
字号:
{ String[] lines = content.split("\n"); StringBuffer result = new StringBuffer(); int counter = 0; int inList = -1; for (int i = 0; i < lines.length; i++) { String line = lines[i]; String actSourceSymbol = ""; while ((line.startsWith(sourceSymbol) || line.startsWith(sourceSymbol2)) && (actSourceSymbol.equals("") || line.substring(0, 1).equals(actSourceSymbol))) { actSourceSymbol = line.substring(0, 1); line = line.substring(1, line.length()); counter++; } if ((inList == -1 && counter != 1) || (inList != -1 && inList + 1 < counter)) { for (int c = 0; c < counter; c++) { result.append(actSourceSymbol); } inList = -1; } else { for (int c = 0; c < counter; c++) { if (actSourceSymbol.equals(sourceSymbol2)) { result.append(sourceSymbol2); } else { result.append(targetSymbol); } } inList = counter; } result.append(line); if (i < lines.length - 1) { result.append("\n"); } counter = 0; } // Fixes testExtensions5 if( content.endsWith( "\n" ) && result.charAt( result.length()-1 ) != '\n' ) { result.append( '\n' ); } return result.toString(); } private String translateVariables(String result, boolean blogLineBreaks) { result = result.replace("[{$creolepagefilter.version}]", VAR_VERSION); result = result.replace("[{$creolepagefilter.creoleversion}]", VAR_CREOLE_VERSION); String linebreaks = blogLineBreaks ? VAR_LINEBREAK_BLOGLIKE : VAR_LINEBREAK_C2LIKE; result = result.replace("[{$creolepagefilter.linebreak}]", linebreaks); return result; } /** * Undoes the protection. This is done by replacing the md5 hashes by the * original markup. * * @see #protectMarkup(String) */ private String unprotectMarkup(String content,boolean replacePlugins) { Object[] it = this.m_hashList.toArray(); for (int i = it.length - 1; i >= 0; i--) { String hash = (String) it[i]; String protectedMarkup = c_protectionMap.get(hash); content = content.replace(hash, protectedMarkup); if ((protectedMarkup.length() < 3 || (protectedMarkup.length() > 2 && !protectedMarkup.substring(0, 3).equals("{{{")))&&replacePlugins) content = translateElement(content, CREOLE_PLUGIN, JSPWIKI_PLUGIN); } return content; } /** * Protects markup that should not be processed. For now this includes: * <ul> * <li>Preformatted sections, they should be ignored</li> * </li> * <li>Protocol strings like <code>http://</code>, they cause problems * because of the <code>//</code> which is interpreted as italic</li> * </ul> * This protection is a simple method to keep the regular expressions for * the other markup simple. Internally the protection is done by replacing * the protected markup with the the md5 hash of the markup. * * @param content * @return The content with protection */ private String protectMarkup(String content) { c_protectionMap.clear(); m_hashList = new ArrayList<String>(); content = protectMarkup(content, PREFORMATTED_PROTECTED, "", ""); content = protectMarkup(content, URL_PROTECTED, "", ""); content = protectMarkup(content, ESCAPE_PROTECTED, "", ""); content = protectMarkup(content, CREOLE_PLUGIN, "", ""); // content = protectMarkup(content, LINE_PROTECTED); // content = protectMarkup(content, SIGNATURE_PROTECTED); return content; } private ArrayList readPlaceholderProperties(Properties wikiProps) { Set keySet = wikiProps.keySet(); Object[] keys = keySet.toArray(); ArrayList<String[]> result = new ArrayList<String[]>(); for( int i = 0; i < keys.length; i++ ) { String key = keys[i] + ""; String value = wikiProps.getProperty( keys[i] + "" ); if( key.indexOf( "creole.imagePlugin.para.%" ) > -1 ) { String[] pair = new String[2]; pair[0] = key.replaceAll( "creole.imagePlugin.para.%", "" ); pair[1] = value; result.add( pair ); } } return result; } private String replaceImageArea(Properties wikiProps, String content, String markupRegex, String replaceContent, int groupPos, String imagePlugin) { Matcher matcher = Pattern.compile(markupRegex, Pattern.MULTILINE | Pattern.DOTALL).matcher(content); String contentCopy = content; ArrayList plProperties = readPlaceholderProperties(wikiProps); while (matcher.find()) { String protectedMarkup = matcher.group(0); String paramsField = matcher.group(groupPos); String paramsString = ""; if (paramsField != null) { String[] params = paramsField.split(","); for (int i = 0; i < params.length; i++) { String param = params[i].replaceAll("\\||\\s", "").toUpperCase(); // Replace placeholder params for (int j = 0; j < plProperties.size(); j++) { String[] pair = (String[]) plProperties.get(j); String key = pair[0]; String value = pair[1]; String code = param.replaceAll("(?i)([0-9]+)" + key, value + "<check>" + "$1" + "</check>"); code = code.replaceAll("(.*?)%(.*?)<check>(.*?)</check>", "$1$3$2"); if (!code.equals(param)) paramsString += code; } // Check if it is a number try { Integer.parseInt(param); paramsString += " width='" + param + "px'"; } catch (Exception e) { if (wikiProps.getProperty("creole.imagePlugin.para." + param) != null) paramsString += " " + wikiProps.getProperty("creole.imagePlugin.para." + param) .replaceAll("^(\"|')(.*)(\"|')$", "$2"); } } } String temp = protectedMarkup; protectedMarkup = translateElement(protectedMarkup, markupRegex, replaceContent); protectedMarkup = protectedMarkup.replaceAll("\u2015", paramsString); protectedMarkup = protectedMarkup.replaceAll("\u2016", imagePlugin); protectedMarkup = protectedMarkup.replaceAll("caption=''", ""); protectedMarkup = protectedMarkup.replaceAll("\\s+", " "); int pos = contentCopy.indexOf(temp); contentCopy = contentCopy.substring(0, pos) + protectedMarkup + contentCopy.substring(pos + temp.length(), contentCopy.length()); } return contentCopy; } private String replaceArea(String content, String markupRegex, String replaceSource, String replaceTarget) { Matcher matcher = Pattern.compile(markupRegex, Pattern.MULTILINE | Pattern.DOTALL).matcher(content); String contentCopy = content; while (matcher.find()) { String protectedMarkup = matcher.group(0); String temp = protectedMarkup; protectedMarkup = protectedMarkup.replaceAll(replaceSource, replaceTarget); int pos = contentCopy.indexOf(temp); contentCopy = contentCopy.substring(0, pos) + protectedMarkup + contentCopy.substring(pos + temp.length(), contentCopy.length()); } return contentCopy; } /** * Protects a specific markup * * @see #protectMarkup(String) */ private String protectMarkup(String content, String markupRegex, String replaceSource, String replaceTarget) { Matcher matcher = Pattern.compile(markupRegex, Pattern.MULTILINE | Pattern.DOTALL).matcher(content); StringBuffer result = new StringBuffer(); while (matcher.find()) { String protectedMarkup = matcher.group(); protectedMarkup = protectedMarkup.replaceAll(replaceSource, replaceTarget); try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.reset(); digest.update(protectedMarkup.getBytes("UTF-8")); String hash = bytesToHash(digest.digest()); matcher.appendReplacement(result, hash); c_protectionMap.put(hash, protectedMarkup); m_hashList.add(hash); } catch (NoSuchAlgorithmException e) { // FIXME: Should log properly e.printStackTrace(); } catch (UnsupportedEncodingException e) { // FIXME: Auto-generated catch block e.printStackTrace(); } } matcher.appendTail(result); return result.toString(); } private String bytesToHash(byte[] b) { String hash = ""; for (int i = 0; i < b.length; i++) { hash += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1); } return hash; } private String translateElement(String content, String fromMarkup, String toMarkup) { Matcher matcher = Pattern.compile(fromMarkup, Pattern.MULTILINE).matcher(content); StringBuffer result = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(result, toMarkup); } matcher.appendTail(result); return result.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -