📄 creoletojspwikitranslator.java
字号:
/* JSPWiki - a JSP-based WikiWiki clone. Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package com.ecyrd.jspwiki.parser;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.HashMap;import java.util.Map;import java.util.Properties;import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * <p>Translates Creole markp to JSPWiki markup. Simple translator uses regular expressions. * See http://www.wikicreole.org for the WikiCreole spec.</p> * * <p>This translator can be configured through properties defined in * jspwiki.properties starting with "creole.*". See the * jspwiki.properties file for an explanation of the properties</p> * * <p><b>WARNING</b>: This is an experimental feature, and known to be * broken. Use at your own risk.</o> * * @author Steffen Schramm * @author Hanno Eichelberger * @author Christoph Sauer * * @see <a href="http://www.wikicreole.org/">Wiki Creole Spec</a> */public class CreoleToJSPWikiTranslator{ // These variables are expanded so that admins // can display information about the current installed // pagefilter // // The syntax is the same as a wiki var. Unlike a wiki // war though, the CreoleTranslator itself // // [{$creolepagefilter.version}] // [{$creolepagefilter.creoleversion}] // [{$creolepagefilter.linebreak}] -> bloglike/wikilike /** The version of the filter. */ public static final String VAR_VERSION = "1.0.3"; /** The version of Creole that this filter supports. */ public static final String VAR_CREOLE_VERSION = "1.0"; /** The linebreak style "bloglike". */ public static final String VAR_LINEBREAK_BLOGLIKE = "bloglike"; /** The linebreak style "c2like". */ public static final String VAR_LINEBREAK_C2LIKE = "c2like"; private static final String CREOLE_BOLD = "\\*\\*((?s:.)*?)(\\*\\*|(\n\n|\r\r|\r\n\r\n))"; private static final String JSPWIKI_BOLD = "__$1__$3"; private static final String CREOLE_ITALIC = "//((?s:.)*?)(//|(\n\n|\r\r|\r\n\r\n))"; private static final String JSPWIKI_ITALIC = "''$1''$3"; private static final String CREOLE_SIMPLELINK = "\\[\\[([^\\]]*?)\\]\\]"; private static final String JSPWIKI_SIMPLELINK = "[$1]"; private static final String CREOLE_LINK = "\\[\\[([^\\]]*?)\\|([^\\[\\]]*?)\\]\\]"; private static final String JSPWIKI_LINK = "[$2|$1]"; private static final String CREOLE_HEADER_0 = "(\n|\r|\r\n|^)=([^=\\r\\n]*)={0,2}"; private static final String JSPWIKI_HEADER_0 = "$1!!!$2"; private static final String CREOLE_HEADER_1 = "(\n|\r|\r\n|^)==([^=\\r\\n]*)={0,2}"; private static final String JSPWIKI_HEADER_1 = "$1!!!$2"; private static final String CREOLE_HEADER_2 = "(\n|\r|\r\n|^)===([^=\\r\\n]*)={0,3}"; private static final String JSPWIKI_HEADER_2 = "$1!!$2"; private static final String CREOLE_HEADER_3 = "(\n|\r|\r\n|^)====([^=\\r\\n]*)={0,4}"; private static final String JSPWIKI_HEADER_3 = "$1!$2"; private static final String CREOLE_HEADER_4 = "(\n|\r|\r\n|^)=====([^=\\r\\n]*)={0,5}"; private static final String JSPWIKI_HEADER_4 = "$1__$2__"; private static final String CREOLE_SIMPLEIMAGE = "\\{\\{([^\\}]*?)\\}\\}"; private static final String JSPWIKI_SIMPLEIMAGE = "[{Image src='$1'}]"; private static final String CREOLE_IMAGE = "\\{\\{([^\\}]*?)\\|([^\\}]*?)\\}\\}"; private static final String JSPWIKI_IMAGE = "[{Image src='$1' caption='$2'}]"; private static final String CREOLE_IMAGE_LINK = "\\[\\[(.*?)\\|\\{\\{(.*?)\\}\\}\\]\\]"; private static final String JSPWIKI_IMAGE_LINK = "[{Image src='$2' link='$1'}]"; private static final String CREOLE_IMAGE_LINK_DESC = "\\[\\[(.*?)\\|\\{\\{(.*?)\\|(.*?)\\}\\}\\]\\]"; private static final String JSPWIKI_IMAGE_LINK_DESC = "[{Image src='$2' link='$1' caption='$3'}]"; private static final String PREFORMATTED_PROTECTED = "\\Q{{{\\E.*?\\Q}}}\\E"; //private static final String CREOLE_LINEBREAKS = "([^\\s\\\\])(\r\n|\r|\n)+(?=[^\\s\\*#])"; //private static final String JSPWIKI_LINEBREAKS = "$1\\\\\\\\$2"; private static final String CREOLE_TABLE = "(\n|\r|\r\n|^)(\\|[^\n\r]*)\\|(\\t| )*(\n|\r|\r\n|$)"; private static final String CREOLE_PLUGIN = "\\<\\<((?s:.)*?)\\>\\>"; private static final String JSPWIKI_PLUGIN = "[{$1}]"; private static final String WWW_URL = "(\\[\\[)\\s*(www\\..*?)(\\]\\])"; private static final String HTTP_URL = "$1http://$2$3"; private static final String CREOLE_IMAGE_X = "\\{\\{(.*?)((\\|)(.*?)){0,1}((\\|)(.*?)){0,1}\\}\\}"; private static final String JSPWIKI_IMAGE_X = "[{\u2016 src='$1' caption='$4' \u2015}]"; private static final String CREOLE_LINK_IMAG_X = "\\[\\[(.*?)\\|\\{\\{(.*?)((\\|)(.*?)){0,1}((\\|)(.*?)){0,1}\\}\\}\\]\\]"; private static final String JSPWIKI_LINK_IMAGE_X = "[{\u2016 src='$2' link='$1' caption='$5' \u2015}]"; private static final String JSPWIKI_TABLE = "$1$2$4"; /* TODO Is it possible to use just protect :// ? */ private static final String URL_PROTECTED = "http://|ftp://|https://"; private static final String TABLE_HEADER_PROTECTED = "((\n|\r|\r\n|^)(\\|.*?)(\n|\r|\r\n|$))"; private static final String SIGNATURE = "--~~~"; private static final String SIGNATURE_AND_DATE = "--~~~~"; private static final String DEFAULT_DATEFORMAT = "yyyy-MM-dd"; private static final String ESCAPE_PROTECTED = "~(\\*\\*|~|//|-|#|\\{\\{|}}|\\\\|~\\[~~[|]]|----|=|\\|)"; private static Map<String, String> c_protectionMap = new HashMap<String, String>(); private ArrayList<String> m_hashList = new ArrayList<String>(); /** * I have no idea what this method does. Could someone please tell me? * * @param wikiProps A property set * @param content The content to translate? * @param username The username in the signature? * @return Probably some translated content. */ public String translateSignature(Properties wikiProps, final String content, String username) { String dateFormat = wikiProps.getProperty("creole.dateFormat"); if (dateFormat == null) { dateFormat = DEFAULT_DATEFORMAT; } SimpleDateFormat df = null; try { df = new SimpleDateFormat(dateFormat); } catch (Exception e) { e.printStackTrace(); df = new SimpleDateFormat(DEFAULT_DATEFORMAT); } String result = content; result = protectMarkup(result, PREFORMATTED_PROTECTED, "", ""); result = protectMarkup(result, URL_PROTECTED, "", ""); Calendar cal = Calendar.getInstance(); result = translateElement(result, SIGNATURE_AND_DATE, "-- [[" + username + "]], " + df.format(cal.getTime())); result = translateElement(result, SIGNATURE, "-- [[" + username + "]]"); result = unprotectMarkup(result, false); return result; } /** * Translates Creole markup to JSPWiki markup * * @param wikiProps A set of Wiki Properties * @param content Creole markup * @return Wiki markup */ public String translate(Properties wikiProps, final String content) { boolean blogLineBreaks = false; /* // BROKEN, breaks on different platforms. String tmp = wikiProps.getProperty("creole.blogLineBreaks"); if (tmp != null) { if (tmp.trim().equals("true")) blogLineBreaks = true; } */ String imagePlugin = wikiProps.getProperty("creole.imagePlugin.name"); String result = content; // // Breaks on OSX. It is never a good idea to tamper with the linebreaks. JSPWiki always // stores linebreaks as \r\n, regardless of the platform. //result = result.replace("\r\n", "\n"); //result = result.replace("\r", "\n"); /* Now protect the rest */ result = protectMarkup(result); result = translateLists(result, "*", "-", "Nothing"); result = translateElement(result, CREOLE_BOLD, JSPWIKI_BOLD); result = translateElement(result, CREOLE_ITALIC, JSPWIKI_ITALIC); result = translateElement(result, WWW_URL, HTTP_URL); if (imagePlugin != null && !imagePlugin.equals("")) { result = this.replaceImageArea(wikiProps, result, CREOLE_LINK_IMAG_X, JSPWIKI_LINK_IMAGE_X, 6, imagePlugin); result = this.replaceImageArea(wikiProps, result, CREOLE_IMAGE_X, JSPWIKI_IMAGE_X, 5, imagePlugin); } result = translateElement(result, CREOLE_IMAGE_LINK_DESC, JSPWIKI_IMAGE_LINK_DESC); result = translateElement(result, CREOLE_IMAGE_LINK, JSPWIKI_IMAGE_LINK); result = translateElement(result, CREOLE_LINK, JSPWIKI_LINK); result = translateElement(result, CREOLE_SIMPLELINK, JSPWIKI_SIMPLELINK); result = translateElement(result, CREOLE_HEADER_4, JSPWIKI_HEADER_4); result = translateElement(result, CREOLE_HEADER_3, JSPWIKI_HEADER_3); result = translateElement(result, CREOLE_HEADER_2, JSPWIKI_HEADER_2); result = translateElement(result, CREOLE_HEADER_1, JSPWIKI_HEADER_1); result = translateElement(result, CREOLE_HEADER_0, JSPWIKI_HEADER_0); result = translateElement(result, CREOLE_IMAGE, JSPWIKI_IMAGE); result = translateLists(result, "-", "*", "#"); result = translateElement(result, CREOLE_SIMPLEIMAGE, JSPWIKI_SIMPLEIMAGE); result = translateElement(result, CREOLE_TABLE, JSPWIKI_TABLE); result = replaceArea(result, TABLE_HEADER_PROTECTED, "\\|=([^\\|]*)=|\\|=([^\\|]*)", "||$1$2"); /* if (blogLineBreaks) { result = translateElement(result, CREOLE_LINEBREAKS, JSPWIKI_LINEBREAKS); } */ result = unprotectMarkup(result, true); result = translateVariables(result, blogLineBreaks); //result = result.replace("\n", System.getProperty("line.separator")); return result; } /** Translates lists. */ private static String translateLists(String content, String sourceSymbol, String targetSymbol, String sourceSymbol2)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -