用户:Wikibot:修订间差异
小无编辑摘要 |
无编辑摘要 |
||
(未显示2个用户的5个中间版本) | |||
第1行: | 第1行: | ||
机器人,自动将 http://help.ubuntu.com 和 http://wiki.ubuntu.com 由 monimoni 格式转换到 mediawiki 格式,并自动更新和发布的小程序。 | 机器人,自动将 http://help.ubuntu.com 和 http://wiki.ubuntu.com 由 monimoni 格式转换到 mediawiki 格式,并自动更新和发布的小程序。 由java写成。 | ||
由java写成。 | |||
<br> <source lang="java"> | |||
< | |||
<java | |||
/* | /* | ||
* Main.java | * Main.java | ||
第13行: | 第10行: | ||
* and open the template in the editor. | * and open the template in the editor. | ||
*/ | */ | ||
package wiki; | package wiki; | ||
import java.io.BufferedReader; | import java.io.BufferedReader; | ||
import java.io.File; | import java.io.File; | ||
import java.io.FileInputStream; | import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | |||
import java.io.FileOutputStream; | import java.io.FileOutputStream; | ||
import java.io.IOException; | import java.io.IOException; | ||
import java.io.InputStream; | import java.io.InputStream; | ||
import java.io.InputStreamReader; | import java.io.InputStreamReader; | ||
import java.io.OutputStream; | import java.io.OutputStream; | ||
import java.io.UnsupportedEncodingException; | import java.io.UnsupportedEncodingException; | ||
import java.net.HttpURLConnection; | import java.net.HttpURLConnection; | ||
第36行: | 第28行: | ||
import java.net.URLEncoder; | import java.net.URLEncoder; | ||
import java.security.GeneralSecurityException; | import java.security.GeneralSecurityException; | ||
import java.security.cert.X509Certificate; | import java.security.cert.X509Certificate; | ||
import java.util.ArrayList; | |||
import java.util.Date; | |||
import java.util.HashMap; | import java.util.HashMap; | ||
import java.util.Iterator; | import java.util.Iterator; | ||
import java.util.List; | import java.util.List; | ||
import java.util.Map; | import java.util.Map; | ||
import java.util. | import java.util.logging.Level; | ||
import java.util.logging.Logger; | |||
import java.util.regex.Matcher; | import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | import java.util.regex.Pattern; | ||
第56行: | 第50行: | ||
*/ | */ | ||
public class Main { | public class Main { | ||
List <String> addDict= new | |||
List <String> oldDict= new | List<String> addDict = new ArrayList<String>(); | ||
List<String> oldDict = new ArrayList<String>(); | |||
String cookie = "NEED INPUT"; | |||
int readOneDictPayTime = 2500; | |||
public String getHtmlFromStream(InputStream in) throws IOException { | |||
StringBuffer html = new StringBuffer(); | |||
int no = 0; | |||
while ((no = in.read()) > -1) { | |||
html.append((char) no); | |||
} | } | ||
return | in.close(); | ||
return new String(html.toString().getBytes("iso8859-1"), "utf-8"); | |||
} | } | ||
private void addDict(String dict){ | private void addDict(String dict) { | ||
String str=dict.trim(); | String str = dict.trim(); | ||
if (dict.startsWith("/")){ | if (dict.startsWith("/")) { | ||
str=dict.substring(1); | str = dict.substring(1); | ||
} | } | ||
str=dict.substring(0,dict.indexOf("#")); | if (dict.indexOf("#") > 0) { | ||
} | str = dict.substring(0, dict.indexOf("#")); | ||
str=dict.substring(0,dict.indexOf("?")); | } | ||
} | if (dict.indexOf("?") > 0) { | ||
str=dict.substring(2); | str = dict.substring(0, dict.indexOf("?")); | ||
} | } | ||
str=dict.substring(3); | if (dict.startsWith("./") || dict.startsWith("//")) { | ||
str = dict.substring(2); | |||
} | |||
if (dict.startsWith("../")) { | |||
str = dict.substring(3); | |||
} | |||
if (dict.endsWith(".") || dict.endsWith(")") || dict.endsWith("]") || | |||
dict.endsWith("}") || dict.endsWith(";") || dict.endsWith("+") || | |||
dict.endsWith("'") || dict.endsWith("\"") || dict.endsWith("/") || | |||
dict.endsWith(":") || dict.endsWith(",") || dict.endsWith(">")) { | |||
str = dict.substring(0, str.length() - 1); | |||
} | |||
// if (str.toLowerCase().indexOf("team")>0) return; | |||
if (str.trim().length() == 0) { | |||
return; | |||
} | } | ||
if (str.trim().length() >= 256) { | |||
return; | |||
if (str.trim().length()>=256) return; | |||
//../CommandLine | //../CommandLine | ||
//Community => community | |||
if (oldDict.contains(str)) return; | } | ||
if (addDict.contains(str)) return; | if ("Community".equals(str)) { | ||
return; | |||
} | |||
if (oldDict.contains(str)) { | |||
return; | |||
} | |||
if (addDict.contains(str)) { | |||
return; | |||
} | |||
addDict.add(str); | addDict.add(str); | ||
} | } | ||
private void delDict(int dictindex){ | private void delDict(int dictindex) { | ||
oldDict.add(addDict.get(dictindex)); | oldDict.add(addDict.get(dictindex)); | ||
addDict.remove(dictindex); | addDict.remove(dictindex); | ||
} | } | ||
private void clearDict(){ | private void delDict(String dict) { | ||
oldDict.add(dict); | |||
addDict.remove(dict); | |||
} | |||
private void clearDict() { | |||
addDict.clear(); | addDict.clear(); | ||
oldDict.clear(); | oldDict.clear(); | ||
} | } | ||
private void getDicts(String html){ | private boolean findDict(String dict) { | ||
Pattern pattern= Pattern.compile("\\[UbuntuHelp:(.*?)\\]"); | for (String d : addDict) { | ||
Matcher matcher=pattern.matcher(html); | if (d.equals(dict)) { | ||
while(matcher.find()) { | return true; | ||
String line=matcher.group(1); | } | ||
if (line.indexOf("|")>0){ | } | ||
addDict(line.substring(0,line.indexOf("|"))); | for (String d : oldDict) { | ||
}else{ | if (d.equals(dict)) { | ||
return true; | |||
} | |||
} | |||
return false; | |||
} | |||
private void getDicts(String html) { | |||
Pattern pattern = Pattern.compile("\\[UbuntuHelp:(.*?)\\]"); | |||
Matcher matcher = pattern.matcher(html); | |||
while (matcher.find()) { | |||
String line = matcher.group(1); | |||
if (line.indexOf("|") > 0) { | |||
addDict(line.substring(0, line.indexOf("|"))); | |||
} else { | |||
addDict(line); | |||
} | |||
} | |||
pattern = Pattern.compile("\\[UbuntuWiki:(.*?)\\]"); | |||
matcher = pattern.matcher(html); | |||
while (matcher.find()) { | |||
String line = matcher.group(1); | |||
if (line.indexOf("|") > 0) { | |||
addDict(line.substring(0, line.indexOf("|"))); | |||
} else { | |||
addDict(line); | addDict(line); | ||
} | } | ||
} | } | ||
} | } | ||
/** Creates a new instance of Main */ | /** Creates a new instance of Main */ | ||
public Main() { | public Main() { | ||
第178行: | 第168行: | ||
try { | try { | ||
sslContext = SSLContext.getInstance("TLS"); | sslContext = SSLContext.getInstance("TLS"); | ||
X509TrustManager[] xtmArray = new X509TrustManager[] { xtm }; | X509TrustManager[] xtmArray = new X509TrustManager[]{xtm}; | ||
sslContext.init(null, xtmArray, new java.security.SecureRandom()); | sslContext.init(null, xtmArray, new java.security.SecureRandom()); | ||
} catch(GeneralSecurityException gse) { | } catch (GeneralSecurityException gse) { | ||
} | } | ||
if(sslContext != null) { | if (sslContext != null) { | ||
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); | HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); | ||
} | } | ||
HttpsURLConnection.setDefaultHostnameVerifier(hnv); | HttpsURLConnection.setDefaultHostnameVerifier(hnv); | ||
} | } | ||
private X509TrustManager xtm = new X509TrustManager() { | private X509TrustManager xtm = new X509TrustManager() { | ||
public void checkClientTrusted(X509Certificate[] chain, String authType) {} | |||
public void checkServerTrusted(X509Certificate[] chain, String authType) {} | @Override | ||
public void checkClientTrusted(X509Certificate[] chain, String authType) { | |||
} | |||
@Override | |||
public void checkServerTrusted(X509Certificate[] chain, String authType) { | |||
} | |||
@Override | |||
public X509Certificate[] getAcceptedIssuers() { | public X509Certificate[] getAcceptedIssuers() { | ||
return null; | return null; | ||
} | } | ||
}; | }; | ||
private HostnameVerifier hnv = new HostnameVerifier() { | private HostnameVerifier hnv = new HostnameVerifier() { | ||
@Override | |||
public boolean verify(String hostname, SSLSession session) { | public boolean verify(String hostname, SSLSession session) { | ||
return true; | return true; | ||
} | } | ||
}; | }; | ||
public String getUrl(String urladdress,String dict) throws IOException{ | public String getUrl(String urladdress, String dict) throws IOException, InterruptedException { | ||
URL url=new URL(urladdress); | URL url = new URL(urladdress); | ||
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); | HttpURLConnection httpConn; | ||
int SleepTime = 1; | |||
while (true) { | |||
httpConn = (HttpURLConnection) url.openConnection(); | |||
httpConn.setReadTimeout(60000); | |||
httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)"); | |||
httpConn.setRequestProperty("Content-Language", "UTF-8"); | |||
httpConn.setRequestProperty("Connection", "Keep-Alive"); | |||
String | if (httpConn.getResponseCode() > 500) { | ||
System.out.println("read " + httpConn.getResponseCode() + " from " + urladdress + " sleep " + SleepTime + " minute."); | |||
readOneDictPayTime = readOneDictPayTime + 100; | |||
System.out.println("adjust readOneDictPayTime to " + readOneDictPayTime); | |||
if (readOneDictPayTime > 10000) { | |||
readOneDictPayTime = 2500; | |||
} | |||
Thread.sleep(60000 * SleepTime); | |||
SleepTime = SleepTime + 1; | |||
} else if (httpConn.getResponseCode() > 400) { | |||
return ""; | |||
} else { | |||
break; | |||
} | |||
} | |||
InputStream in = httpConn.getInputStream(); | |||
try { | |||
String html = getHtmlFromStream(in); | |||
return moin2wm(html, urladdress, dict); | |||
} finally { | |||
in.close(); | |||
} | } | ||
} | } | ||
public String moin2wm(String html,String url,String dict) throws UnsupportedEncodingException{ | public String moin2wm(String html, String url, String dict) throws UnsupportedEncodingException { | ||
String text=html | String text = html; | ||
if (text.indexOf("This page does not exist yet.") > 0) { | |||
return ""; | |||
if ( | |||
} | } | ||
String ex = "UbuntuHelp"; | |||
String head="{{From|"+url.substring(0,url.indexOf("?"))+"}}\r\n{{Languages|"+ex+":"+dict+"}}\r\n"; | String turl = "https://help.ubuntu.com/community/"; | ||
if (url.startsWith("https://wiki")) { | |||
ex = "UbuntuWiki"; | |||
turl = "https://wiki.ubuntu.com/"; | |||
} | |||
String head = "{{From|" + url.substring(0, url.indexOf("?")) + "}}\r\n{{Languages|" + ex + ":" + dict + "}}\r\n"; | |||
//删除注释 | //删除注释 | ||
text=text.replaceAll("\r\n##(.*)",""); | text = text.replaceAll("\r\n##(.*)", ""); | ||
text=text.replaceAll("\r\n#format(.*)",""); | text = text.replaceAll("\r\n#format(.*)", ""); | ||
text=text.replaceAll("\r\n#language(.*)",""); | text = text.replaceAll("\r\n#language(.*)", ""); | ||
text=text.replaceAll("\r\n#pragma(.*)",""); | text = text.replaceAll("\r\n#pragma(.*)", ""); | ||
text=text.replaceAll("\r\n#acl(.*)",""); | text = text.replaceAll("\r\n#acl(.*)", ""); | ||
text=text.replaceAll("^##(.*)\r\n",""); | text = text.replaceAll("^##(.*)\r\n", ""); | ||
text=text.replaceAll("^#format(.*)\r\n",""); | text = text.replaceAll("^#format(.*)\r\n", ""); | ||
text=text.replaceAll("^#language(.*)\r\n",""); | text = text.replaceAll("^#language(.*)\r\n", ""); | ||
text=text.replaceAll("^#pragma(.*)\r\n",""); | text = text.replaceAll("^#pragma(.*)\r\n", ""); | ||
text=text.replaceAll("^#acl(.*)\r\n",""); | text = text.replaceAll("^#acl(.*)\r\n", ""); | ||
//替换#REDIRECT PDFPrinting => #REDIRECT [[PDFPrinting]] | //替换#REDIRECT PDFPrinting => #REDIRECT [[PDFPrinting]] | ||
text=text.replaceAll("#REDIRECT (\\S*)","#REDIRECT "+"[["+ex+":$1]]"); | text = text.replaceAll("#REDIRECT (\\S*)", "#REDIRECT " + "[[" + ex + ":$1]]"); | ||
text=text.replaceAll("#redirect (\\S*)","#REDIRECT "+"[["+ex+":$1]]"); | text = text.replaceAll("#redirect (\\S*)", "#REDIRECT " + "[[" + ex + ":$1]]"); | ||
//#refresh 0 https://wiki.ubuntu.com/ASUS_A3H_5010_Laptop_with_Ubuntu | //#refresh 0 https://wiki.ubuntu.com/ASUS_A3H_5010_Laptop_with_Ubuntu | ||
text=text.replaceAll("#REFRESH (.*?) (\\S*)","#REDIRECT "+"[["+ex+":$2]]"); | text = text.replaceAll("#REFRESH (.*?) (\\S*)", "#REDIRECT " + "[[" + ex + ":$2]]"); | ||
text=text.replaceAll("#refresh (.*?) (\\S*)","#REDIRECT "+"[["+ex+":$2]]"); | text = text.replaceAll("#refresh (.*?) (\\S*)", "#REDIRECT " + "[[" + ex + ":$2]]"); | ||
//删除不要的下拉列表 | |||
text = text.replaceAll("\\[\\[Navigation\\(.*?\\)\\]\\]", ""); | |||
text = text.replaceAll("Navigation\\(.*?\\)", ""); | |||
//删除主题 | //删除主题 | ||
text=text.replaceAll(".*TableOfContents.*",""); | text = text.replaceAll(".*TableOfContents.*", ""); | ||
//分离{{{ }}} | |||
Pattern pattern = Pattern.compile("\\{\\{\\{(.*?)\\}\\}\\}"); | |||
Matcher matcher = pattern.matcher(text); | |||
List<String> codes = new ArrayList(); | |||
while (matcher.find()) { | |||
String code = matcher.group(1); | |||
codes.add("<code><nowiki>" + code + "</nowiki></code>"); | |||
int index = text.indexOf(matcher.group(0)); | |||
int len = matcher.group(0).length(); | |||
text = text.substring(0, index) + "@@code" + String.valueOf(codes.size()) + "@@" + text.substring(index + len); | |||
} | |||
pattern = Pattern.compile("\\{\\{\\{(.*?)\\}\\}\\}", Pattern.DOTALL); | |||
String html2 = text; | |||
matcher = pattern.matcher(html2); | |||
while (matcher.find()) { | |||
String code = matcher.group(1); | |||
codes.add("<pre><nowiki>" + code + "</nowiki></pre>"); | |||
int index = text.indexOf("{{{" + matcher.group(1) + "}}}"); | |||
int len = matcher.group(1).length() + 6; | |||
text = text.substring(0, index) + "@@code" + String.valueOf(codes.size()) + "@@" + text.substring(index + len); | |||
} | |||
//转化表格 | |||
text = tableConv(text); | |||
//标题从二开始 | //标题从二开始 | ||
text=text.replaceAll("= (.*?) =","== $1 =="); | text = text.replaceAll("= (.*?) =", "== $1 =="); | ||
//转化List | //转化List | ||
text=replaceList(text); | text = replaceList(text); | ||
//[[BR]] -> <BR> | //[[BR]] -> <BR> | ||
text=text.replaceAll("\\[\\[BR\\]\\]","<br>"); | text = text.replaceAll("\\[\\[BR\\]\\]", "<br>"); | ||
//link convert superscripted - ^ * ^ -> <sup> * </sup> | //link convert superscripted - ^ * ^ -> <sup> * </sup> | ||
text=text.replaceAll("\\^(.*)\\^","<sup>$1</sup>"); | text = text.replaceAll("\\^(.*)\\^", "<sup>$1</sup>"); | ||
//link convert subscripted - ,, * ,, -> <sub> * </sub> | //link convert subscripted - ,, * ,, -> <sub> * </sub> | ||
text=text.replaceAll(",,(.*?),,","<sub>$1</sub>"); | text = text.replaceAll(",,(.*?),,", "<sub>$1</sub>"); | ||
//link convert - ["/*"] -> [dict/*] | |||
text = text.replaceAll("\\[\\[\"//(.*?)\\|(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/$1|$2]]"); | |||
text = text.replaceAll("\\[\\[\"/(.*?)\\|(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/$1|$2]]"); | |||
text = text.replaceAll("\\[\\[\"\\./(.*?)\\|(.*?)\"\\]\\]", "[[" + ex + ":$1|$2]]"); | |||
text = text.replaceAll("\\[\\[\"\\.\\./(.*?)\\|(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/../$1|$2]]"); | |||
text = text.replaceAll("\\[\\[\"//(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/$1|$1]]"); | |||
text = text.replaceAll("\\[\\[\"/(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/$1|$1]]"); | |||
text = text.replaceAll("\\[\\[\"\\./(.*?)\"\\]\\]", "[[" + ex + ":$1|$1]]"); | |||
text = text.replaceAll("\\[\\[\"\\.\\./(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/../$1|$1]]"); | |||
//link convert - [[/*]] -> [[dict/*]] | |||
//[[/Introduction|Introduction]] | |||
text = text.replaceAll("\\[\\[//(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":" + dict + "/$1|$2]]"); | |||
text = text.replaceAll("\\[\\[/(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":" + dict + "/$1|$2]]"); | |||
text = text.replaceAll("\\[\\[\\./(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":$1|$2]]"); | |||
text = text.replaceAll("\\[\\[\\.\\./(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":" + dict + "/../$1|$2]]"); | |||
text = text.replaceAll("\\[\\[//(.*?)\\]\\]", "[[" + ex + ":" + dict + "/$1|$1]]"); | |||
text = text.replaceAll("\\[\\[/(.*?)\\]\\]", "[[" + ex + ":" + dict + "/$1|$1]]"); | |||
text = text.replaceAll("\\[\\[\\./(.*?)\\]\\]", "[[" + ex + ":$1|$1]]"); | |||
text = text.replaceAll("\\[\\[\\.\\./(.*?)\\]\\]", "[[" + ex + ":" + dict + "/../$1|$1]]"); | |||
//link convert - [" * "] -> [[ UbuntuHelp: * ]] | //link convert - [" * "] -> [[ UbuntuHelp: * ]] | ||
text=text.replaceAll("\\[\"(.*?)\"\\]","[["+ex+":$1]]"); | text = text.replaceAll("\\[\"(.*?)\"\\]", "[[" + ex + ":$1|$1]]"); | ||
//link convert - [# * ] -> [[ * ]] | //link convert - [# * ] -> [[ * ]] | ||
text=text.replaceAll("\\[#(.*?)\\]","[[$1]]"); | text = text.replaceAll("\\[#(.*?)\\]", "[[$1]]"); | ||
//link convert - [: / * : * ] -> [[ UbuntuHelp:dict\ * | * ]] | //link convert - [: / * : * ] -> [[ UbuntuHelp:dict\ * | * ]] | ||
text=text.replaceAll("\\[:/(.*?):(.*?)\\]","[["+ex+":"+dict+"/$1|$2]]"); | text = text.replaceAll("\\[:/(.*?):(.*?)\\]", "[[" + ex + ":" + dict + "/$1|$2]]"); | ||
//link convert - [: * : * ] -> [[ UbuntuHelp: * | * ]] | //link convert - [: * : * ] -> [[ UbuntuHelp: * | * ]] | ||
text=text.replaceAll("\\[:(.*?):(.*?)\\]","[["+ex+":$1|$2]]"); | text = text.replaceAll("\\[:(.*?):(.*?)\\]", "[[" + ex + ":$1|$2]]"); | ||
//link convert - [: / * ] -> [[ UbuntuHelp: dict * ]] | //link convert - [: / * ] -> [[ UbuntuHelp: dict * ]] | ||
text=text.replaceAll("\\[:/(.*?)\\]","[["+ex+":"+dict+"/$1]]"); | text = text.replaceAll("\\[:/(.*?)\\]", "[[" + ex + ":" + dict + "/$1]]"); | ||
//link convert - [: * ] -> [[ UbuntuHelp: * ]] | //link convert - [: * ] -> [[ UbuntuHelp: * ]] | ||
text=text.replaceAll("\\[:(.*?)\\]","[["+ex+":$1]]"); | text = text.replaceAll("\\[:(.*?)\\]", "[[" + ex + ":$1]]"); | ||
//link convert - wiki:cat -> [[UbuntuWiki:cat]] | //link convert - wiki:cat -> [[UbuntuWiki:cat]] | ||
text=text.replaceAll(" wiki:(\\S*)"," [[UbuntuWiki:$1]]"); | //link convert - wiki:Ubuntu/cat -> [[UbuntuWiki:cat]] | ||
text=text.replaceAll("\r\nwiki:(\\S*)","\r\n[[UbuntuWiki:$1]]"); | text = text.replaceAll(" wiki:Ubuntu/(\\S*)", " [[UbuntuWiki:$1]]"); | ||
text = text.replaceAll("\r\nwiki:Ubuntu/(\\S*)", "\r\n[[UbuntuWiki:$1]]"); | |||
text = text.replaceAll(" wiki:(\\S*)", " [[UbuntuWiki:$1]]"); | |||
text = text.replaceAll("\r\nwiki:(\\S*)", "\r\n[[UbuntuWiki:$1]]"); | |||
//link convert - [wiki:cat * ] -> [[UbuntuWiki:cat| * ]] | //link convert - [wiki:cat * ] -> [[UbuntuWiki:cat| * ]] | ||
text=text.replaceAll("\\[wiki:(.*?)\\ (.*?)\\]","[[UbuntuWiki:$1|$2]]"); | text = text.replaceAll("\\[wiki:Ubuntu/(.*?)\\ (.*?)\\]", "[[UbuntuWiki:$1|$2]]"); | ||
text = text.replaceAll("\\[wiki:(.*?)\\ (.*?)\\]", "[[UbuntuWiki:$1|$2]]"); | |||
//link convert - [wiki:cat * ] -> [[UbuntuWiki:cat| * ]] | //link convert - [wiki:cat * ] -> [[UbuntuWiki:cat| * ]] | ||
text=text.replaceAll("\\[wiki:(.*?)\\]","[[UbuntuWiki:$1]]"); | text = text.replaceAll("\\[wiki:Ubuntu/(.*?)\\]", "[[UbuntuWiki:$1]]"); | ||
text = text.replaceAll("\\[wiki:(.*?)\\]", "[[UbuntuWiki:$1]]"); | |||
//link convert - [UbuntuWiki:\*] -> [UbuntuWiki:dict\*] | //link convert - [UbuntuWiki:\*] -> [UbuntuWiki:dict\*] | ||
text=text.replaceAll("\\[UbuntuWiki:\\\\(.*?)\\]","[UbuntuWiki:"+dict+"\\$1]"); | text = text.replaceAll("\\[UbuntuWiki:\\\\(.*?)\\]", "[UbuntuWiki:" + dict + "\\$1]"); | ||
//link convert - [UbuntuHelp:\*] -> [UbuntuHelp:dict\*] | //link convert - [UbuntuHelp:\*] -> [UbuntuHelp:dict\*] | ||
text=text.replaceAll("\\[UbuntuHelp:\\\\(.*?)\\]","[UbuntuHelp:"+dict+"\\$1]"); | text = text.replaceAll("\\[UbuntuHelp:\\\\(.*?)\\]", "[UbuntuHelp:" + dict + "\\$1]"); | ||
//Self:/ddd=dict/ Self:ddd=dict | |||
text = text.replaceAll(":Self:(.*?)//", ":" + dict + "/$1"); | |||
text = text.replaceAll(":Self:(.*?)/", ":" + dict + "/$1"); | |||
text = text.replaceAll(":Self:(.*?)", ":$1"); | |||
text = text.replaceAll(":self:(.*?)//", ":" + dict + "/$1"); | |||
text = text.replaceAll(":self:(.*?)/", ":" + dict + "/$1"); | |||
text = text.replaceAll(":self:(.*?)", ":$1"); | |||
//link convert - __ * __ -> <u> * </u> | //link convert - __ * __ -> <u> * </u> | ||
text=text.replaceAll("__(.*?)__","<u>$1</u>"); | text = text.replaceAll("__(.*?)__", "<u>$1</u>"); | ||
//CategoryHomepage =>[[category:"+ex+"]]; | //CategoryHomepage =>[[category:"+ex+"]]; | ||
text=text.replaceAll("Category(\\S*)","[[category:Category$1]]"); | text = text.replaceAll("Category(\\S*)", "[[category:Category$1]]"); | ||
text=text.replaceAll("\r\n( *)","\r\n"); | text = text.replaceAll("\r\n( *)", "\r\n"); | ||
text= | text = attachmentUrl(text, turl, dict); | ||
//[[xxx:http]] => [[http:]] | //[[xxx:http]] => [[http:]] | ||
text=text.replaceAll("\\[\\[(.*?):http(.*?)\\]\\]","[[http$2]]"); | text = text.replaceAll("\\[\\[(.*?):http(.*?)\\]\\]", "[[http$2]]"); | ||
//[[xxx:ftp]] => [[ftp:]] | //[[xxx:ftp]] => [[ftp:]] | ||
text=text.replaceAll("\\[\\[(.*?):ftp(.*?)\\]\\]","[[ftp$2]]"); | text = text.replaceAll("\\[\\[(.*?):ftp(.*?)\\]\\]", "[[ftp$2]]"); | ||
//[[Ubuntu:HardwareSupport]] =>[[UbuntuWiki:HardwareSupport]] | |||
text = text.replaceAll("\\[\\[Ubuntu:(.*?)\\]\\]", "[[UbuntuWiki:$1]]"); | |||
//[[https://wiki.ubuntu.com/*]] => [[UbuntuWiki:]] | //[[https://wiki.ubuntu.com/*]] => [[UbuntuWiki:]] | ||
text=text.replaceAll("\\[\\[https://wiki\\.ubuntu\\.com/(.*?)\\]\\]","[[UbuntuWiki:$1]]"); | text = text.replaceAll("\\[\\[https://wiki\\.ubuntu\\.com/(.*?) (.*?)\\]\\]", "[[UbuntuWiki:$1|$2]]"); | ||
text = text.replaceAll("\\[\\[https://wiki\\.ubuntu\\.com/(.*?)\\]\\]", "[[UbuntuWiki:$1]]"); | |||
//[[https://help.ubuntu.com/community/*]] => [[UbuntuHelp:]] | //[[https://help.ubuntu.com/community/*]] => [[UbuntuHelp:]] | ||
text=text.replaceAll("\\[\\[https://help\\.ubuntu\\.com/community/(.*?)\\]\\]","[[UbuntuHelp:$1]]"); | text = text.replaceAll("\\[\\[https://help\\.ubuntu\\.com/community/(.*?) (.*?)\\]\\]", "[[UbuntuHelp:$1|$2]]"); | ||
text = text.replaceAll("\\[\\[https://help\\.ubuntu\\.com/community/(.*?)\\]\\]", "[[UbuntuHelp:$1]]"); | |||
//[[http://wiki.ubuntu.com/*]] => [[UbuntuWiki:]] | //[[http://wiki.ubuntu.com/*]] => [[UbuntuWiki:]] | ||
text=text.replaceAll("\\[\\[http://wiki\\.ubuntu\\.com/(.*?)\\]\\]","[[UbuntuWiki:$1]]"); | text = text.replaceAll("\\[\\[http://wiki\\.ubuntu\\.com/(.*?) (.*?)\\]\\]", "[[UbuntuWiki:$1|$2]]"); | ||
text = text.replaceAll("\\[\\[http://wiki\\.ubuntu\\.com/(.*?)\\]\\]", "[[UbuntuWiki:$1]]"); | |||
//[[http://help.ubuntu.com/community/*]] => [[UbuntuHelp:]] | //[[http://help.ubuntu.com/community/*]] => [[UbuntuHelp:]] | ||
text=text.replaceAll("\\[\\[http://help\\.ubuntu\\.com/community/(.*?)\\]\\]","[[UbuntuHelp:$1]]"); | text = text.replaceAll("\\[\\[http://help\\.ubuntu\\.com/community/(.*?) (.*?)\\]\\]", "[[UbuntuHelp:$1|$2]]"); | ||
text = text.replaceAll("\\[\\[http://help\\.ubuntu\\.com/community/(.*?)\\]\\]", "[[UbuntuHelp:$1]]"); | |||
text = text.replaceAll("\\[http://wiki\\.ubuntu\\.com/(.*?) (.*?)\\]", "[[UbuntuWiki:$1|$2]]"); | |||
text = text.replaceAll("\\[https://wiki\\.ubuntu\\.com/(.*?) (.*?)\\]", "[[UbuntuWiki:$1|$2]]"); | |||
text = text.replaceAll("\\[https://help\\.ubuntu\\.com/community/(.*?) (.*?)\\]", "[[UbuntuHelp:$1|$2]]"); | |||
text = text.replaceAll("\\[http://help\\.ubuntu\\.com/community/(.*?) (.*?)\\]", "[[UbuntuHelp:$1|$2]]"); | |||
text = text.replaceAll("\\[http://wiki\\.ubuntu\\.com/(.*?)\\]", "[[UbuntuWiki:$1]]"); | |||
text = text.replaceAll("\\[https://wiki\\.ubuntu\\.com/(.*?)\\]", "[[UbuntuWiki:$1]]"); | |||
text = text.replaceAll("\\[https://help\\.ubuntu\\.com/community/(.*?)\\]", "[[UbuntuHelp:$1]]"); | |||
text = text.replaceAll("\\[http://help\\.ubuntu\\.com/community/(.*?)\\]", "[[UbuntuHelp:$1]]"); | |||
text = text.replaceAll("\\[\\[(.*?)/(.*?)/\\.\\./(.*?)\\]\\]", "[[$1/$3]]"); | |||
text = text.replaceAll("\\[\\[(.*?)/(.*?)/\\.\\./(.*?)\\]\\]", "[[$1/$3]]"); | |||
text = text.replaceAll("\\[\\[(.*?)/\\./(.*?)\\]\\]", "[[$1/$2]]"); | |||
//[[http://redhat.com|RedHat]] => [http://redhat.com RedHat] | |||
text = text.replaceAll("\\[\\[(http://.*?)\\|(.*?)\\]\\]", "[$1 $2]"); | |||
//[[https://redhat.com|RedHat]] => [https://redhat.com RedHat] | |||
text = text.replaceAll("\\[\\[(https://.*?)\\|(.*?)\\]\\]", "[$1 $2]"); | |||
//[[ftp://redhat.com|RedHat]] => [http://redhat.com RedHat] | |||
text = text.replaceAll("\\[\\[(ftp://.*?)\\|(.*?)\\]\\]", "[$1 $2]"); | |||
//[[http://redhat.com]] => [http://redhat.com] | |||
text = text.replaceAll("\\[\\[(http://.*?)\\]\\]", "[$1]"); | |||
//[[https://redhat.com]] => [https://redhat.com] | |||
text = text.replaceAll("\\[\\[(https://.*?)\\]\\]", "[$1]"); | |||
//[[ftp://redhat.com]] => [http://redhat.com] | |||
text = text.replaceAll("\\[\\[(ftp://.*?)\\]\\]", "[$1]"); | |||
//[[RedHat|redhat]] => [[UbuntuHelp:RedHat|readhat]] | |||
text = text.replaceAll("\\[\\[(?!UbuntuWiki:|UbuntuHelp:|https|http|ftp|category)(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":$1|$2]]"); | |||
//[[RedHat]] => [[UbuntuHelp:RedHat|ReadHat]] | |||
text = text.replaceAll("\\[\\[(?!UbuntuWiki:|UbuntuHelp:|https|http|ftp|category)(.*?)\\]\\]", "[[" + ex + ":$1|$1]]"); | |||
//转化% | |||
text = formatdict(text); | |||
//转化单词 | |||
text = format2(text, ex, dict); | |||
//需要还原$code$ | |||
for (int i = 0; i < codes.size(); i++) { | |||
String str = codes.get(i); | |||
String s = "@@code" + String.valueOf(i + 1) + "@@"; | |||
int m = text.indexOf(s); | |||
int n = s.length(); | |||
//text = text.substring(0, m) + str + text.substring(m + n); | |||
text = text.replace(s, str); | |||
} | } | ||
if (text.trim().length()<10){ | |||
String foot = "\r\n[[category:" + ex + "]]"; | |||
if (text.trim().startsWith("#REDIRECT")) { | |||
// System.out.print(" "+text.trim()); | |||
pattern = Pattern.compile("\\[\\[" + ex + ":(.*?)\\]\\]"); | |||
matcher = pattern.matcher(text); | |||
if (matcher.find()) { | |||
//如果仅仅是大小写的重定向,就不用考虑直接忽略。 | |||
if (matcher.group(1).toLowerCase().equals(dict.toLowerCase())) { | |||
System.out.println(dict + " redirect to " + matcher.group(1) + " , ignore. "); | |||
return ""; | |||
} | |||
} | |||
return text + head + foot; | |||
} | |||
if (text.trim().length() < 10) { | |||
return ""; | return ""; | ||
} | } | ||
return head+text+foot; | return head + text + foot; | ||
} | |||
public String formatdict(String text) { | |||
Pattern pattern = Pattern.compile("\\[\\[(.*?)\\]\\]"); | |||
Matcher matcher = pattern.matcher(text); | |||
String html = text; | |||
while (matcher.find()) { | |||
String dict = matcher.group(1); | |||
if (dict.indexOf("%") > 0) { | |||
int index = html.indexOf("[[" + dict + "]]"); | |||
int len = dict.length() + 4; | |||
html = html.substring(0, index) + "[[" + getDict(dict) + "]]" + html.substring(index + len); | |||
} | |||
} | |||
return html; | |||
} | |||
public String getDict(String dict) { | |||
String str = ""; | |||
str = dict.replaceAll("%..", "_"); | |||
str = str.replaceAll("%.", ""); | |||
return str; | |||
} | |||
//检查有没有至少两个大写单词存在,如果存在检查是不是词汇,如果是,转为链接。 | |||
public boolean arrayhas(char[] c, char b) { | |||
for (int i = 0; i < c.length; i++) { | |||
if (c[i] == b) { | |||
return true; | |||
} | |||
} | |||
return false; | |||
} | |||
public String replaceBySpace(String text, String match) { | |||
Pattern pattern = Pattern.compile(match); | |||
Matcher matcher = pattern.matcher(text); | |||
String html = text; | |||
while (matcher.find()) { | |||
String str = matcher.group(0); | |||
int index = text.indexOf(str); | |||
int length = str.length(); | |||
String space = String.format("%-" + length + "s", ""); | |||
html = html.substring(0, index) + space + html.substring(index + length); | |||
} | |||
return html; | |||
} | |||
public String format2(String html, String ex, String dict) { | |||
String result = html; | |||
int n_count = 0; | |||
int m_count = 0; | |||
char[] o = {':', '%', '?', '&', '=', '\\', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'}; | |||
char[] m = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; | |||
char[] n = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; | |||
String temp = html; | |||
//删除现有的所有的链接 | |||
temp = replaceBySpace(temp, "\\{\\{.*?\\}\\}"); | |||
temp = replaceBySpace(temp, "=====.*?====="); | |||
temp = replaceBySpace(temp, "====.*?===="); | |||
temp = replaceBySpace(temp, "===.*?==="); | |||
temp = replaceBySpace(temp, "==.*?=="); | |||
temp = replaceBySpace(temp, "=.*?="); | |||
temp = replaceBySpace(temp, "\\[\\[.*?\\]\\]"); | |||
temp = replaceBySpace(temp, "\\[.*?\\]"); | |||
temp = replaceBySpace(temp, "http:.*? "); | |||
temp = replaceBySpace(temp, "https:.*? "); | |||
temp = replaceBySpace(temp, "<pre>.*?</pre>"); | |||
temp = replaceBySpace(temp, "<code>.*?</code>"); | |||
String word = ""; | |||
for (int i = 0; i < temp.length(); i++) { | |||
char c = temp.charAt(i); | |||
if (arrayhas(m, c)) { | |||
word = word + String.valueOf(c); | |||
m_count++; | |||
} else if (arrayhas(n, c)) { | |||
word = word + String.valueOf(c); | |||
n_count++; | |||
} else if (arrayhas(o, c)) { | |||
word = word + String.valueOf(c); | |||
} else { | |||
//单词截取完成 | |||
if (n_count > 1 && m_count > 0) { | |||
if (!word.equals("IconsPage") && !word.equals("AttachFile") && | |||
word.indexOf("\\") < 0 && word.indexOf("/") < 0 && findDict(word) && !word.equals(dict)) { | |||
try { | |||
int index = temp.indexOf(word); | |||
int len = word.length(); | |||
String replace = "[[" + ex + ":" + word + "|" + word + "]]"; | |||
result = result.substring(0, index) + replace + result.substring(index + len); | |||
temp = temp.substring(0, index) + String.format("%-" + replace.length() + "s", "") + temp.substring(index + len); | |||
} catch (Exception e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
} | |||
n_count = 0; | |||
m_count = 0; | |||
word = ""; | |||
} | |||
} | |||
return result; | |||
} | |||
public String urlEncode(String dict) { | |||
String str = dict; | |||
str = str.replaceAll(" ", "%20"); | |||
if (str.indexOf("#") > 0) { | |||
str = str.substring(0, str.indexOf("#")); | |||
} | |||
if (str.indexOf("?") > 0) { | |||
str = str.substring(0, str.indexOf("?")); | |||
} | |||
return str; | |||
} | |||
public boolean checkListStart(String line) { | |||
String str = line; | |||
if (str.length() < 4) { | |||
return false; | |||
} | |||
if (str.charAt(0) == '.' && str.charAt(0) == ' ') { | |||
return true; | |||
} | |||
if (str.charAt(0) == '*' && str.charAt(0) == ' ') { | |||
return true; | |||
} | |||
char[] o = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; | |||
char[] m = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; | |||
char[] n = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; | |||
if (arrayhas(o, str.charAt(0)) || arrayhas(m, str.charAt(0)) || arrayhas(n, str.charAt(0))) { | |||
if (str.charAt(1) == '.' && str.charAt(2) == ' ') { | |||
return true; | |||
} | |||
} | |||
return false; | |||
} | |||
public String replaceList(String text) { | |||
String[] lines = text.split("\r\n"); | |||
String block = ""; | |||
StringBuffer bf = new StringBuffer(); | |||
boolean start = false; | |||
for (int i = 0; i < lines.length; i++) { | |||
String line = lines[i]; | |||
String linetrim = line.trim(); | |||
if (linetrim.equals("")) { | |||
continue; | |||
} | |||
if (checkListStart(linetrim)) { | |||
if (!start) { | |||
start = true; | |||
block = line; | |||
} else { | |||
block = block + "\r\n" + line; | |||
} | |||
} else { | |||
if (start) { | |||
block = formatList2(block); | |||
bf.append(block + "\r\n"); | |||
start = false; | |||
} | |||
bf.append(lines[i] + "\r\n"); | |||
} | |||
} | |||
if (start) { | |||
block = formatList2(block); | |||
bf.append(block + "\r\n"); | |||
start = false; | |||
} | |||
return bf.toString(); | |||
} | |||
public static int formatList_getSpaceCount(String s) { | |||
int num = 0; | |||
for (int i = 0; i < s.length(); i++) { | |||
if (s.charAt(i) == ' ') { | |||
num++; | |||
} else { | |||
break; | |||
} | |||
} | |||
return num; | |||
} | |||
public static String formatList_clear(String mstr) { | |||
String str = mstr.trim(); | |||
if (str.startsWith(".") || str.startsWith("*")) { | |||
return str.substring(1).trim(); | |||
} else { | |||
return str.substring(2).trim(); | |||
} | |||
} | } | ||
public String | public String formatList2(String text) { | ||
String[] mlist = text.split("\r\n"); | |||
// if (mlist.length<2) return text; | |||
while (true) { | |||
boolean done = true; | |||
for (String line : mlist) { | |||
if (formatList_getSpaceCount(line) > 0) { | |||
done = false; | |||
break; | |||
} | |||
} | |||
if (done) { | |||
break; | |||
} | |||
int lay = 0; | |||
String lType = "ol"; | |||
for (int i = 0; i < mlist.length; i++) { | |||
int space = formatList_getSpaceCount(mlist[i]); | |||
if (space > 0 && lay == 0) { | |||
String str = mlist[i].trim(); | |||
if (str.startsWith("*")) { | |||
lType = "ul"; | |||
} | |||
mlist[i] = "<" + lType + "><li>" + formatList_clear(str); | |||
// | lay = space; | ||
if (i == mlist.length - 1) { | |||
mlist[i] = mlist[i] + "</li></" + lType + ">"; | |||
} | |||
} else if (space > 0 && lay == space) { | |||
mlist[i] = "</li><li>" + formatList_clear(mlist[i]); | |||
// | if (i == mlist.length - 1) { | ||
mlist[i] = mlist[i] + "</li></" + lType + ">"; | |||
// | } | ||
} else if (lay > 0 && space == 0) { | |||
mlist[i] = "</li></" + lType + ">" + mlist[i]; | |||
break; | |||
} else { | |||
continue; | |||
} | |||
} | |||
} | |||
StringBuffer sb = new StringBuffer(); | |||
for (String line : mlist) { | |||
sb.append(line + "\r\n"); | |||
} | } | ||
return text; | return sb.toString(); | ||
} | |||
public String formatList(String text) { | |||
/* | |||
1. one | |||
1. two | |||
1. one | |||
* bullet 1 | |||
* bullet 2 | |||
1. two | |||
1. three | |||
* bullet | |||
1. one | |||
*最好是转为*=> <ul><li></li></ul> #=> <ol><li></li></ol> | |||
*/ | |||
//获得步进长度 | |||
text = text.replaceAll(" 1\\. ", " # "); | |||
text = text.replaceAll(" a\\. ", " # "); | |||
text = text.replaceAll(" A\\. ", " # "); | |||
text = text.replaceAll(" i\\. ", " # "); | |||
text = text.replaceAll(" I\\. ", " # "); | |||
text = text.replaceAll(" \\. ", " # "); | |||
String step = ""; | |||
for (int i = 0; i < text.length(); i++) { | |||
if (text.charAt(i) == ' ') { | |||
step = step + " "; | |||
} else { | |||
break; | |||
} | |||
} | |||
//修改步长为" " | |||
if (step.length() > 0) { | |||
text = text.replaceAll(step, " "); | |||
} | |||
String[] lines = text.split("\r\n"); | |||
for (int i = 0; i < lines.length; i++) { | |||
if (lines[i].charAt(0) != ' ') { | |||
break; | |||
} | |||
lines[i] = lines[i].substring(1); | |||
} | |||
StringBuffer bf = new StringBuffer(); | |||
bf.append(lines[0] + "\r\n"); | |||
for (int i = 1; i < lines.length; i++) { | |||
if (lines[i].startsWith(" ")) { | |||
char[] s = lines[i].toCharArray(); | |||
for (int j = 0; j < s.length; j++) { | |||
if (s[j] == ' ') { | |||
if (j >= lines[i - 1].length()) { | |||
s[j] = 0; | |||
} | |||
if (lines[i - 1].charAt(j) != '*' && lines[i - 1].charAt(j) != '#') { | |||
s[j] = 0; | |||
} else { | |||
s[j] = lines[i - 1].charAt(j); | |||
} | |||
} else { | |||
break; | |||
} | |||
} | |||
String l = ""; | |||
for (int j = 0; j < s.length; j++) { | |||
if (s[j] == 0) { | |||
continue; | |||
} | |||
l = l + s[j]; | |||
} | |||
lines[i] = l; | |||
} | |||
bf.append(lines[i] + "\r\n"); | |||
} | |||
return bf.toString(); | |||
} | } | ||
public String | public String attachmentUrl(String text, String baseurl, String dict) throws UnsupportedEncodingException { | ||
//attachment:IconsPage/info.png -> https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png | //attachment:IconsPage/info.png -> https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png | ||
while (true){ | while (true) { | ||
Pattern pattern= Pattern.compile("attachment:(.*?)/(\\ | Pattern pattern = Pattern.compile("\\{\\{attachment:(.*?)/(.*?)\\}\\}"); | ||
Matcher matcher=pattern.matcher(text); | Matcher matcher = pattern.matcher(text); | ||
String replace; | String replace; | ||
if (matcher.find()) { | if (matcher.find()) { | ||
replace=baseurl+matcher.group(1)+"?action=AttachFile&do=get&target="+URLEncoder.encode(matcher.group(2), "UTF-8"); | replace = baseurl + matcher.group(1) + "?action=AttachFile&do=get&target=" + URLEncoder.encode(matcher.group(2), "UTF-8"); | ||
text=text.substring(0,matcher.start(0))+replace+text.substring(matcher.end(0)); | text = text.substring(0, matcher.start(0)) + replace + text.substring(matcher.end(0)); | ||
continue; | continue; | ||
} | } | ||
pattern= Pattern.compile("attachment:(\\ | pattern = Pattern.compile("\\{\\{attachment:(.*?)\\}\\}"); | ||
matcher=pattern.matcher(text); | matcher = pattern.matcher(text); | ||
if(matcher.find()) { | if (matcher.find()) { | ||
replace=baseurl+dict+"?action=AttachFile&do=get&target="+URLEncoder.encode(matcher.group(1), "UTF-8"); | replace = baseurl + dict + "?action=AttachFile&do=get&target=" + URLEncoder.encode(matcher.group(1), "UTF-8"); | ||
text=text.substring(0,matcher.start(0))+replace+text.substring(matcher.end(0)); | text = text.substring(0, matcher.start(0)) + replace + text.substring(matcher.end(0)); | ||
continue; | continue; | ||
} | } | ||
break; | break; | ||
} | } | ||
return text; | return text; | ||
} | } | ||
public String tableConv(String html){ | public String tableConv(String html) { | ||
//||a||b||c|| -> {| | //||a||b||c|| -> {| | ||
//||d||e||f|| |a||b||c | //||d||e||f|| |a||b||c | ||
第409行: | 第818行: | ||
// |d||e||f | // |d||e||f | ||
// |} | // |} | ||
// System.out.println(html); | |||
String[] lines=html.split("\r\n"); | |||
String[] lines = html.split("\r\n"); | |||
String block = ""; | String block = ""; | ||
StringBuffer bf= new StringBuffer(); | StringBuffer bf = new StringBuffer(); | ||
boolean start=false; | boolean start = false; | ||
for (int i=0;i<lines.length;i++){ | for (int i = 0; i < lines.length; i++) { | ||
String line=lines[i].trim(); | String line = lines[i].trim(); | ||
if (line.startsWith("||")){ | if (line.startsWith("||")) { | ||
if ( | if (line.length() < 4) { | ||
continue; //line like ||(.*?)|| | |||
} | |||
String str = line.substring(1, line.length() - 2); | |||
Pattern pattern = Pattern.compile("<(.*?)>"); | |||
Matcher matcher = pattern.matcher(line); | |||
while (matcher.find()) { | |||
String x = matcher.group(1).trim(); | |||
String replace = ""; | |||
str=str. | if (x.startsWith("-")) { | ||
if (x.indexOf(" ") > 0) { | |||
block=block+"\r\n|-\r\n"+str; | replace = "colspan=" + x.substring(1, x.indexOf(" ")) + "|"; | ||
} else { | |||
replace = "colspan=" + x.substring(1) + "|"; | |||
} | |||
} else if (x.startsWith("|")) { | |||
if (x.indexOf(" ") > 0) { | |||
replace = "rowspan=" + x.substring(1, x.indexOf(" ")) + "|"; | |||
} else { | |||
replace = "rowspan=" + x.substring(1) + "|"; | |||
} | |||
} | |||
// System.out.println("match:"+matcher.group(1)+" to:"+replace); | |||
int m = str.indexOf("<" + matcher.group(1) + ">"); | |||
int n = ("<" + matcher.group(1) + ">").length(); | |||
if (m == -1 || (m + n) > str.length()) { | |||
continue; | |||
} | |||
str = str.substring(0, m) + replace + str.substring(m + n); | |||
// str=str.replaceFirst("<"+matcher.group(1)+">",replace); | |||
} | |||
if (!start) { | |||
start = true; | |||
block = "{|border=\"1\" cellspacing=\"0\"\r\n" + str; | |||
} else { | |||
block = block + "\r\n|-\r\n" + str; | |||
} | } | ||
}else{ | } else { | ||
if (start){ | if (start) { | ||
block=block+"\r\n|}\r\n"; | block = block + "\r\n|}\r\n"; | ||
bf.append(block); | bf.append(block); | ||
start=false; | start = false; | ||
} | } | ||
bf.append(lines[i]+"\r\n"); | bf.append(lines[i] + "\r\n"); | ||
} | } | ||
} | } | ||
if (start){ | if (start) { | ||
block=block+"\r\n|}\r\n"; | block = block + "\r\n|}\r\n"; | ||
bf.append(block); | bf.append(block); | ||
start=false; | start = false; | ||
} | } | ||
return bf.toString(); | return bf.toString(); | ||
} | } | ||
public String getWikiHtml(String url) throws MalformedURLException, IOException { | |||
URL httpurl = new URL(url); | |||
public void putText(String dict,String html,String surl) throws MalformedURLException, IOException{ | HttpURLConnection httpConn = (HttpURLConnection) httpurl.openConnection(); | ||
httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042523 Ubuntu/9.04 (jaunty) Firefox/3.0.10"); | |||
httpConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); | |||
httpConn.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5"); | |||
httpConn.setRequestProperty("Accept-Charset", "gb2312,utf-8;q=0.7,*;q=0.7"); | |||
httpConn.setRequestProperty("Referer", "http://wiki.ubuntu.org.cn/"); | |||
httpConn.setRequestProperty("Cookie", cookie); | |||
httpConn.setUseCaches(false); | |||
httpConn.setDoInput(true); | |||
InputStream in = httpConn.getInputStream(); | |||
String wikihtml = getHtmlFromStream(in); | |||
return wikihtml; | |||
} | |||
public void putText(String dict, String html, String surl) throws MalformedURLException, IOException { | |||
URL url; | URL url; | ||
URLConnection conn; | URLConnection conn; | ||
第457行: | 第906行: | ||
BufferedReader read; | BufferedReader read; | ||
StringBuffer sb; | StringBuffer sb; | ||
if (html.length()<5){ | if (html.length() < 5) { | ||
System.out. | System.out.println(dict + " is short , ignore. " + html); | ||
return; | return; | ||
} | } | ||
第471行: | 第920行: | ||
// } | // } | ||
// } | // } | ||
String ex="UbuntuHelp"; | String ex = "UbuntuHelp"; | ||
if (surl.startsWith("https://wiki")){ | if (surl.startsWith("https://wiki")) { | ||
ex="UbuntuWiki"; | ex = "UbuntuWiki"; | ||
} | |||
String sdict = dict; | |||
if (sdict.indexOf("%") > 0) { | |||
sdict = getDict(dict); | |||
} | } | ||
try{ | |||
url=new URL("http://wiki.ubuntu.org.cn/"+ex+":"+ | try { | ||
url = new URL("http://wiki.ubuntu.org.cn/" + ex + ":" + sdict + "?action=raw"); | |||
conn = url.openConnection(); | conn = url.openConnection(); | ||
conn.setReadTimeout(60000); | conn.setReadTimeout(60000); | ||
conn.setRequestProperty("Cookie", cookie); | conn.setRequestProperty("Cookie", cookie); | ||
in=conn.getInputStream(); | in = conn.getInputStream(); | ||
String wikihtml = getHtmlFromStream(in); | |||
int wikilen = (wikihtml.replaceAll("\r", "")).replaceAll("\n", "").length(); | |||
int htmllen = (html.replaceAll("\r", "")).replaceAll("\n", "").length(); | |||
if (wikilen == htmllen) { | |||
System.out.println(dict + " no changes."); | |||
System.out. | |||
return; | return; | ||
} else { | |||
System.out.println(dict + " length: " + html.length() + " oldlength: " + wikihtml.length()); | |||
} | } | ||
}catch(Exception ex0){ | } catch (Exception ex0) { | ||
} | } | ||
String tempdict = sdict.replaceAll("%23", ""); | |||
String address = "http://wiki.ubuntu.org.cn/" + ex + ":" + tempdict + "?action=edit"; | |||
String wikihtml = null; | |||
String from = null; | |||
Boolean flag = false; | |||
for (int i = 0; i < 5; i++) { | |||
wikihtml = getWikiHtml(address); | |||
int start = wikihtml.indexOf("<form id=\"editform\""); | |||
int end = wikihtml.indexOf("<div class=\"printfooter\">"); | |||
try { | |||
from = wikihtml.substring(start, end); | |||
flag = true; | |||
break; | |||
} catch (Exception ex0) { | |||
//cookie = getCookie(); | |||
if (!wikihtml.equals("")) { | |||
System.out.println(wikihtml); | |||
} else { | |||
System.out.println(dict + " read error!"); | |||
} | |||
try { | |||
Thread.sleep(60000); | |||
} catch (InterruptedException ex1) { | |||
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1); | |||
} | |||
} | |||
} | } | ||
if (!flag) { | |||
System.out.println("read " + address + " error,please set cookie!"); | |||
System.exit(0); | |||
System.out.println( | |||
System. | |||
} | } | ||
Map<String,String> map=getPostDate(from); | Map<String, String> map = getPostDate(from); | ||
Iterator<String> iterator=map.keySet().iterator(); | Iterator<String> iterator = map.keySet().iterator(); | ||
url = new URL("http://wiki.ubuntu.org.cn/index.php?title="+ex+":"+URLEncoder.encode( | url = new URL("http://wiki.ubuntu.org.cn/index.php?title=" + ex + ":" + URLEncoder.encode(sdict, "UTF-8") + "&action=submit"); | ||
HttpURLConnection conn2 = (HttpURLConnection) url.openConnection(); | HttpURLConnection conn2 = (HttpURLConnection) url.openConnection(); | ||
String boundary="---------------------------167593640336579986891120154"; | String boundary = "---------------------------167593640336579986891120154"; | ||
conn2.setReadTimeout(60000); | conn2.setReadTimeout(60000); | ||
conn2.setDoOutput(true); | conn2.setDoOutput(true); | ||
conn2.setAllowUserInteraction(false); | conn2.setAllowUserInteraction(false); | ||
conn2.setRequestMethod("POST"); | conn2.setRequestMethod("POST"); | ||
conn2.setRequestProperty("Cookie", cookie); | conn2.setRequestProperty("Cookie", cookie); | ||
conn2.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary); | conn2.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); | ||
conn2.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20061201 Firefox/2.0.0.3 (Ubuntu-feisty)"); | conn2.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20061201 Firefox/2.0.0.3 (Ubuntu-feisty)"); | ||
conn2.setRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); | conn2.setRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); | ||
conn2.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5"); | conn2.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5"); | ||
conn2.setRequestProperty("Referer", "http://wiki.ubuntu.org.cn/index.php?title=UbuntuHelp:"+URLEncoder.encode(dict, "UTF-8")+"&action=edit"); | conn2.setRequestProperty("Referer", "http://wiki.ubuntu.org.cn/index.php?title=UbuntuHelp:" + URLEncoder.encode(dict, "UTF-8") + "&action=edit"); | ||
conn2.setRequestProperty("Accept-Charset", "UTF-8,*"); | conn2.setRequestProperty("Accept-Charset", "UTF-8,*"); | ||
conn2.setRequestProperty("Connection", "Keep-Alive"); | conn2.setRequestProperty("Connection", "Keep-Alive"); | ||
StringBuffer content=new StringBuffer(); | StringBuffer content = new StringBuffer(); | ||
while (iterator.hasNext()){ | while (iterator.hasNext()) { | ||
String name=iterator.next(); | String name = iterator.next(); | ||
if (name.equals("wpPreview")) | if (name.equals("wpPreview")) { | ||
continue; | |||
} | |||
if (name.equals("wpDiff")) { | |||
continue; | |||
if (name.equals(" | |||
} | } | ||
content.append("--"+boundary+"\r\n"); | if (name.equals("wpWatchthis")) { | ||
content.append("Content-Disposition: form-data; name=\""+name+"\"\r\n\r\n"); | continue; | ||
content.append(value+"\r\n"); | } | ||
String value = ""; | |||
if (name.equals("wpTextbox1")) { | |||
value = html; | |||
} else { | |||
value = map.get(name); | |||
} | |||
content.append("--" + boundary + "\r\n"); | |||
content.append("Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n"); | |||
content.append(value + "\r\n"); | |||
} | } | ||
content.append("--"+boundary+"--\r\n\r\n"); | content.append("--" + boundary + "--\r\n\r\n"); | ||
byte[] data=content.toString().getBytes(); | byte[] data = content.toString().getBytes(); | ||
conn2.setRequestProperty("Content-Length", String.valueOf(data.length)); | conn2.setRequestProperty("Content-Length", String.valueOf(data.length)); | ||
OutputStream out = conn2.getOutputStream(); | OutputStream out = conn2.getOutputStream(); | ||
out.write(data); | out.write(data); | ||
out.flush(); | out.flush(); | ||
// Get the response | // Get the response | ||
try{ | try { | ||
String wikihtml2 = getHtmlFromStream(conn2.getInputStream()); | |||
// System.out.println(wikihtml2); | |||
} catch (Exception ex0) { | |||
System.out.println(dict + " add. but has a error:" + ex0.getMessage()); | |||
}catch(Exception ex0){ | |||
System.out.println(" add. but has a error:"+ex0.getMessage()); | |||
out.close(); | out.close(); | ||
return; | return; | ||
} | } | ||
out.close(); | out.close(); | ||
System.out.println(dict + " put end ."); | |||
} | delDict(dict); | ||
} | |||
private Map<String,String> getPostDate(String from){ | @SuppressWarnings("empty-statement") | ||
Map<String,String> map=new HashMap<String,String>(); | private Map<String, String> getPostDate(String from) { | ||
Pattern pattern= Pattern.compile("<input(.*?)/>"); | Map<String, String> map = new HashMap<String, String>(); | ||
Matcher matcher=pattern.matcher(from); | Pattern pattern = Pattern.compile("<input(.*?)/>"); | ||
Pattern pname=Pattern.compile("name=[\"|'](.*?)[\"|']"); | Matcher matcher = pattern.matcher(from); | ||
Pattern pvalue=Pattern.compile("value=[\"|'](.*?)[\"|']"); | Pattern pname = Pattern.compile("name=[\"|'](.*?)[\"|']"); | ||
Pattern pvalue = Pattern.compile("value=[\"|'](.*?)[\"|']"); | |||
while(matcher.find()) { | |||
String name,value; | while (matcher.find()) { | ||
String input=matcher.group(1); | String name, value; | ||
Matcher mname=pname.matcher(input); | String input = matcher.group(1); | ||
Matcher mvalue=pvalue.matcher(input); | Matcher mname = pname.matcher(input); | ||
if (mname.find()){ | Matcher mvalue = pvalue.matcher(input); | ||
name=mname.group(1); | if (mname.find()) { | ||
}else{ | name = mname.group(1); | ||
} else { | |||
continue; | continue; | ||
}; | } | ||
if (mvalue.find()){ | ; | ||
value=mvalue.group(1); | if (mvalue.find()) { | ||
}else{ | value = mvalue.group(1); | ||
if (input.indexOf("checkbox")>0){ | } else { | ||
value="0"; | if (input.indexOf("checkbox") > 0) { | ||
}else{ | value = "0"; | ||
value=""; | } else { | ||
value = ""; | |||
} | } | ||
}; | } | ||
map.put(name,value); | ; | ||
map.put(name, value); | |||
} | } | ||
// int start=from.indexOf("cols='80' style=\"width:100%\" >"); | // int start=from.indexOf("cols='80' style=\"width:100%\" >"); | ||
// int end=from.indexOf("</textarea>"); | // int end=from.indexOf("</textarea>"); | ||
// | // | ||
// map.put("wpTextbox1",from.substring(start,end)); | // map.put("wpTextbox1",from.substring(start,end)); | ||
map.put("wpTextbox1",""); | map.put("wpTextbox1", ""); | ||
return map; | return map; | ||
} | } | ||
public void helpstart() throws IOException{ | private void saveProgress(String file, String dict) throws FileNotFoundException, IOException { | ||
FileOutputStream out = new FileOutputStream(file, true); | |||
out.write(dict.getBytes()); | |||
out.write("\r\n".getBytes()); | |||
out.flush(); | |||
out.close(); | |||
} | |||
private void readProgress(String file) throws FileNotFoundException, IOException { | |||
if (!new File(file).exists()) { | |||
return; | |||
} | |||
FileInputStream in = new FileInputStream(file); | |||
String lines = getHtmlFromStream(in); | |||
String[] dicts = lines.split("\r\n"); | |||
for (String dict : dicts) { | |||
oldDict.add(dict); | |||
} | |||
} | |||
public void helpstart() throws IOException, InterruptedException { | |||
clearDict(); | clearDict(); | ||
// String dict="community/"; | // String dict="community/"; | ||
第635行: | 第1,116行: | ||
// getDicts(out); | // getDicts(out); | ||
// putText(dict,out,"https://help.ubuntu.com/community/"); | // putText(dict,out,"https://help.ubuntu.com/community/"); | ||
InputStream in=null; | InputStream in = null; | ||
File saveFile=new File(" | File saveFile = new File("help.ubuntu.com.index.html"); | ||
if (saveFile.exists()){ | if (saveFile.exists()) { | ||
in=new FileInputStream(saveFile); | in = new FileInputStream(saveFile); | ||
}else{ | } else { | ||
URL url=new URL("https://help.ubuntu.com/community/TitleIndex"); | URL url = new URL("https://help.ubuntu.com/community/TitleIndex"); | ||
in=url.openConnection().getInputStream( | in = url.openConnection().getInputStream(); | ||
} | } | ||
String html = getHtmlFromStream(in); | |||
if (!saveFile.exists()){ | if (!saveFile.exists()) { | ||
FileOutputStream out=new FileOutputStream(saveFile); | FileOutputStream out = new FileOutputStream(saveFile); | ||
out.write( | out.write(html.getBytes()); | ||
out.flush(); | out.flush(); | ||
out.close(); | out.close(); | ||
} | } | ||
// URL url=new URL("https://help.ubuntu.com/community/TitleIndex"); | // URL url=new URL("https://help.ubuntu.com/community/TitleIndex"); | ||
// InputStream in=url.openConnection().getInputStream(); | // InputStream in=url.openConnection().getInputStream(); | ||
第668行: | 第1,142行: | ||
// sb.append(line+"\r\n"); | // sb.append(line+"\r\n"); | ||
// } | // } | ||
html = html.substring(html.indexOf("<a href=\"/community/TitleIndex?allpages=1\">") + 50, html.indexOf("<div id=\"footer\">")); | |||
Pattern pattern= Pattern.compile("<a href=\"/community/(.*?)\">"); | Pattern pattern = Pattern.compile("<a href=\"/community/(.*?)\">"); | ||
Matcher matcher=pattern.matcher(html); | Matcher matcher = pattern.matcher(html); | ||
while(matcher.find()) { | while (matcher.find()) { | ||
String input=matcher.group(1); | String input = matcher.group(1); | ||
addDict(input); | addDict(input); | ||
} | } | ||
String dict; | String dict; | ||
String out; | for (String d : addDict) { | ||
System.out.println(d); | |||
System.out.println("一共需要转换 "+String.valueOf(addDict.size())+" 篇文章。"); | } | ||
// String out; | |||
System.out.println("一共需要转换 " + String.valueOf(addDict.size()) + " 篇文章。"); | |||
readProgress("helpProgress.txt"); | |||
ThreadPool pool = new ThreadPool(); | |||
for (int i = 0; i < addDict.size(); i++) { | |||
final String dict1 = addDict.get(i); | |||
if (oldDict.contains(dict1)) { | |||
System.out.println(dict1 + " already read."); | |||
continue; | |||
} | |||
Runnable run = new Runnable() { | |||
public void run() { | |||
long starttime = new Date().getTime(); | |||
String html = ""; | |||
try { | |||
System.out.println(dict1 + " read ..."); | |||
html = getUrl("https://help.ubuntu.com/community/" + urlEncode(dict1) + "?action=raw", dict1); | |||
System.out.println(dict1 + " read end."); | |||
} catch (Exception ex) { | |||
ex.printStackTrace(); | |||
return; | |||
} | |||
try { | try { | ||
System.out. | // System.out.println(dict1 + " conver ..."); | ||
// getDicts(html); | |||
System.out. | System.out.println(dict1 + " put ..."); | ||
}catch(Exception | putText(dict1, html, "https://help.ubuntu.com/community/" + dict1); | ||
saveProgress("helpProgress.txt", dict1); | |||
long needwaitTime = readOneDictPayTime - (new Date().getTime() - starttime); | |||
if (needwaitTime > 0) { | |||
System.out.println("sleep " + needwaitTime); | |||
Thread.sleep(needwaitTime); | |||
} | |||
// System.out.println(dict1+" put end."); | |||
} catch (Exception ex) { | |||
System.out.println(dict1 + " error:" + ex.getMessage()); | |||
} | } | ||
} | } | ||
}; | |||
pool.addTask(run); | |||
} | } | ||
pool.start(); | |||
// while (addDict.size()>0){ | |||
// dict=addDict.get(0); | |||
// System.out.print(String.valueOf(addDict.size())+" "+dict); | |||
// try{ | |||
//// if (addDict.size()>1377 && dict.indexOf("%")<0 ) continue; | |||
// try { | |||
// System.out.print(" read"); | |||
// out=getUrl("https://help.ubuntu.com/community/"+urlEncode(dict)+"?action=raw",dict); | |||
// System.out.print(" ."); | |||
// }catch(Exception ex){ | |||
// try { | |||
// System.out.print(" read again"); | |||
// out=getUrl("https://help.ubuntu.com/community/"+urlEncode(dict)+"?action=raw",dict); | |||
// System.out.print(" ."); | |||
// }catch(Exception ex2){ | |||
// continue; | |||
// } | |||
// } | |||
// try { | |||
// System.out.print(" get dict"); | |||
// getDicts(out); | |||
// System.out.print(" . put"); | |||
// putText(dict,out,"https://help.ubuntu.com/community/"+dict); | |||
// System.out.print(" .\r\n"); | |||
// }catch(Exception ex){ | |||
// System.out.println(dict+" error:"+ex.getMessage()); | |||
// continue; | |||
// } | |||
// }finally{ | |||
// delDict(0); | |||
// } | |||
// } | |||
String out = getUrl("https://help.ubuntu.com/community?action=raw", "community"); | |||
putText("community", out, "https://help.ubuntu.com/community/"); | |||
} | } | ||
public void wikistart() throws MalformedURLException, IOException { | |||
public void wikistart() throws MalformedURLException, IOException{ | |||
clearDict(); | clearDict(); | ||
InputStream in=null; | InputStream in = null; | ||
File saveFile=new File(" | File saveFile = new File("wiki.ubuntu.com.index.html"); | ||
if (saveFile.exists()){ | if (saveFile.exists()) { | ||
in=new FileInputStream(saveFile); | in = new FileInputStream(saveFile); | ||
}else{ | } else { | ||
URL url=new URL("https://wiki.ubuntu.com/TitleIndex"); | URL url = new URL("https://wiki.ubuntu.com/TitleIndex"); | ||
in=url.openConnection().getInputStream(); | in = url.openConnection().getInputStream(); | ||
} | } | ||
BufferedReader read=new BufferedReader(new InputStreamReader(in)); | BufferedReader read = new BufferedReader(new InputStreamReader(in)); | ||
StringBuffer sb=new StringBuffer(); | StringBuffer sb = new StringBuffer(); | ||
while (true){ | while (true) { | ||
String line=read.readLine(); | String line = read.readLine(); | ||
if (line==null) break; | if (line == null) { | ||
sb.append(line+"\r\n"); | break; | ||
} | |||
sb.append(line + "\r\n"); | |||
} | } | ||
read.close(); | read.close(); | ||
if (!saveFile.exists()){ | if (!saveFile.exists()) { | ||
FileOutputStream out=new FileOutputStream(saveFile); | FileOutputStream out = new FileOutputStream(saveFile); | ||
out.write(sb.toString().getBytes()); | out.write(sb.toString().getBytes()); | ||
out.flush(); | out.flush(); | ||
out.close(); | out.close(); | ||
} | } | ||
String html=sb.substring(sb.indexOf("<a | String html = sb.substring(sb.indexOf("<a href=\"/TitleIndex?allpages=1\">Include system pages</a>"), sb.indexOf("<div id=\"footer\">")); | ||
Pattern pattern= Pattern.compile("<a href=\"/(.*?)\">"); | Pattern pattern = Pattern.compile("<a href=\"/(.*?)\">"); | ||
Matcher matcher=pattern.matcher(html); | Matcher matcher = pattern.matcher(html); | ||
while(matcher.find()) { | while (matcher.find()) { | ||
String input=matcher.group(1); | String input = matcher.group(1); | ||
if (input.indexOf("?") > 0) { | |||
continue; | |||
} | |||
if (input.indexOf("ubuntu.com") > 0) { | |||
continue; | |||
} | |||
if (input.equals("Jan_Mate")) { | |||
continue; | |||
} | |||
addDict(input); | addDict(input); | ||
System.out.println(input); | System.out.println(input); | ||
第758行: | 第1,291行: | ||
String dict; | String dict; | ||
String out; | String out; | ||
System.out.println("一共需要转换 "+String.valueOf(addDict.size())+" 篇文章。"); | System.out.println("一共需要转换 " + String.valueOf(addDict.size()) + " 篇文章。"); | ||
readProgress("wikiProgress.txt"); | |||
while (addDict.size()>0){ | |||
ThreadPool pool = new ThreadPool(); | |||
for (int i = 0; i < addDict.size(); i++) { | |||
final String dict1 = addDict.get(i); | |||
// if (addDict.size()>12285) continue; | if (oldDict.contains(dict1)) { | ||
System.out.println(dict1 + " already read."); | |||
continue; | |||
} | |||
Runnable run = new Runnable() { | |||
public void run() { | |||
long starttime = new Date().getTime(); | |||
String html = ""; | |||
try { | |||
System.out.println(dict1 + " read ..."); | |||
html = getUrl("https://wiki.ubuntu.com/" + urlEncode(dict1) + "?action=raw", dict1); | |||
System.out.println(dict1 + " read end ."); | |||
} catch (Exception ex) { | |||
ex.printStackTrace(); | |||
return; | |||
} | |||
try { | |||
// System.out.println(dict1 + " conver ..."); | |||
// getDicts(html); | |||
System.out.println(dict1 + " put ..."); | |||
putText(dict1, html, "https://wiki.ubuntu.com/" + dict1); | |||
saveProgress("wikiProgress.txt", dict1); | |||
long needwaitTime = readOneDictPayTime - (new Date().getTime() - starttime); | |||
if (needwaitTime > 0) { | |||
System.out.println("sleep " + needwaitTime); | |||
Thread.sleep(needwaitTime); | |||
} | |||
// System.out.println(dict1+" put end ."); | |||
} catch (Exception ex) { | |||
System.out.println(dict1 + " error:" + ex.getMessage()); | |||
ex.printStackTrace(); | |||
} | |||
} | |||
}; | |||
pool.addTask(run); | |||
} | |||
pool.start(); | |||
// while (addDict.size()>0){ | |||
// dict=addDict.get(0); | |||
// System.out.print(String.valueOf(addDict.size())+" "+dict); | |||
// try{ | |||
//// if (addDict.size()>12285) continue; | |||
// try { | |||
// System.out.print(" read"); | |||
// out=getUrl("https://wiki.ubuntu.com/"+urlEncode(dict)+"?action=raw",dict); | |||
// System.out.print(" ."); | |||
// }catch(Exception ex){ | |||
// ex.printStackTrace(); | |||
// try { | |||
// System.out.print(" read again"); | |||
// out=getUrl("https://wiki.ubuntu.com/"+urlEncode(dict)+"?action=raw",dict); | |||
// System.out.print(" ."); | |||
// }catch(Exception ex2){ | |||
// ex2.printStackTrace(); | |||
// continue; | |||
// } | |||
// } | |||
// try { | |||
// System.out.print(" get dict"); | |||
// getDicts(out); | |||
// System.out.print(" . put"); | |||
// putText(dict,out,"https://wiki.ubuntu.com/"+dict); | |||
// System.out.print(" .\r\n"); | |||
// }catch(Exception ex){ | |||
// System.out.println(dict+" error:"+ex.getMessage()); | |||
// } | |||
// }finally{ | |||
// delDict(0); | |||
// } | |||
// } | |||
} | |||
public void laptopTestingTeam() throws FileNotFoundException, IOException { | |||
InputStream in = null; | |||
File saveFile = new File("/home/wangpian/Desktop/index.php"); | |||
in = new FileInputStream(saveFile); | |||
BufferedReader read = new BufferedReader(new InputStreamReader(in)); | |||
StringBuffer sb = new StringBuffer(); | |||
while (true) { | |||
String line = read.readLine(); | |||
if (line == null) { | |||
break; | |||
} | |||
sb.append(line + "\r\n"); | |||
} | |||
read.close(); | |||
in.close(); | |||
Pattern pattern = Pattern.compile("\\[\\[UbuntuWiki:LaptopTestingTeam/(.*?)\\]\\]"); | |||
Matcher matcher = pattern.matcher(sb.toString()); | |||
while (matcher.find()) { | |||
String input = "LaptopTestingTeam/" + matcher.group(1); | |||
addDict(input); | |||
System.out.println(input); | |||
} | |||
String out = ""; | |||
while (addDict.size() > 0) { | |||
String dict = addDict.get(0); | |||
System.out.print(String.valueOf(addDict.size()) + " " + dict); | |||
try { | |||
try { | try { | ||
System.out.print(" read"); | System.out.print(" read"); | ||
out=getUrl("https://wiki.ubuntu.com/"+dict+"?action=raw",dict); | out = getUrl("https://wiki.ubuntu.com/" + urlEncode(dict) + "?action=raw", dict); | ||
System.out.print(" ."); | System.out.print(" ."); | ||
}catch(Exception ex){ | } catch (Exception ex) { | ||
ex.printStackTrace(); | ex.printStackTrace(); | ||
try { | try { | ||
System.out.print(" read again"); | System.out.print(" read again"); | ||
out=getUrl("https://wiki.ubuntu.com/"+dict+"?action=raw",dict); | out = getUrl("https://wiki.ubuntu.com/" + urlEncode(dict) + "?action=raw", dict); | ||
System.out.print(" ."); | System.out.print(" ."); | ||
}catch(Exception ex2){ | } catch (Exception ex2) { | ||
ex2.printStackTrace(); | ex2.printStackTrace(); | ||
continue; | continue; | ||
第784行: | 第1,424行: | ||
getDicts(out); | getDicts(out); | ||
System.out.print(" . put"); | System.out.print(" . put"); | ||
putText(dict,out,"https://wiki.ubuntu.com/"+dict); | putText(dict, out, "https://wiki.ubuntu.com/" + dict); | ||
System.out.print(" .\r\n"); | System.out.print(" .\r\n"); | ||
}catch(Exception ex){ | } catch (Exception ex) { | ||
System.out.println(dict+" error:"+ex.getMessage()); | System.out.println(dict + " error:" + ex.getMessage()); | ||
} | } | ||
}finally{ | } finally { | ||
delDict(0); | delDict(0); | ||
} | } | ||
} | } | ||
} | } | ||
public void test() throws IOException{ | public void test() throws IOException, InterruptedException { | ||
// String sss="\r\ndd attachment:IconsPage/info.png ClamAV can only\r\n"; | // String sss="\r\ndd attachment:IconsPage/info.png ClamAV can only\r\n"; | ||
// sss=sss.replaceAll("attachment:(.*?)/(.*?) ","https://help.ubuntu.com/community/$1?action=AttachFile&do=get&target=$2 "); | // sss=sss.replaceAll("attachment:(.*?)/(.*?) ","https://help.ubuntu.com/community/$1?action=AttachFile&do=get&target=$2 "); | ||
第817行: | 第1,457行: | ||
// System.out.println(text); | // System.out.println(text); | ||
// text=text.replaceAll("\r\n([ \\.]*?)\\{\\{\\{(.*)","<pre><nowiki>$2"); | // text=text.replaceAll("\r\n([ \\.]*?)\\{\\{\\{(.*)","<pre><nowiki>$2"); | ||
//link convert - {{{ * -> <pre><nowiki> * | //link convert - {{{ * -> <pre><nowiki> * | ||
// text=text.replaceAll("\\{\\{\\{(.*)","\r\n<pre><nowiki>$1"); | // text=text.replaceAll("\\{\\{\\{(.*)","\r\n<pre><nowiki>$1"); | ||
第825行: | 第1,465行: | ||
// System.out.println(text); | // System.out.println(text); | ||
// getCookie(); | // getCookie(); | ||
// String url="http://bingniu.3322.org/mywiki/OpenLDAPAdminGuide/SecurityConsideration"; | // String url="http://bingniu.3322.org/mywiki/OpenLDAPAdminGuide/SecurityConsideration"; | ||
// String out=getUrl(url+"?action=raw","UbuntuManual"); | // String out=getUrl(url+"?action=raw","UbuntuManual"); | ||
// System.out.println(out); | // System.out.println(out); | ||
// BufferedReader read=new BufferedReader(new FileReader("/home/wangpian/a1.txt")); | // BufferedReader read=new BufferedReader(new FileReader("/home/wangpian/a1.txt")); | ||
// String s=""; | // String s=""; | ||
第842行: | 第1,482行: | ||
// | // | ||
// System.out.println(s.replaceAll("\\[\\[UbuntuHelp(.*?)\\|(.*?)\\]\\]","$2")); | // System.out.println(s.replaceAll("\\[\\[UbuntuHelp(.*?)\\|(.*?)\\]\\]","$2")); | ||
// String html="== Design ==\r\n"+ | |||
// "* Choosing the \"About UMC\" menu item should open the About window, visually centered on the screen. It should behave properly with respect to startup notification and session saving. The window should be closable using either Alt-F4 or the close button.\r\n"+ | |||
// "\r\n"+ | |||
// " * The About window should include:\r\n"+ | |||
// " * UMC logo and version number (most required!)\r\n"+ | |||
// " * computer model, if available\r\n"+ | |||
// " * CPU speed and amount of RAM on the system\r\n"+ | |||
// " * a \"Hardware Details\" button that opens the Device Manager\r\n"+ | |||
// " * Linux kernel version number\r\n"+ | |||
// " * KDE/Gnome version number.\r\n"+ | |||
// " * This \"About Box\" could also include a button \"Generate System Report\". As newbies often are asked to add system details like their sources.list, lspci, lsmod and the like(in launchpad answers or ubuntu forums), they tend to have a hard time gathering these information. Maybe they could select/unselect the appropriate buttons for the config file they want to be exported..?\r\n"+ | |||
// "\r\n"+ | |||
// " * Any of the informative text should be drag-and-droppable as plain text into another program." | |||
// ; | |||
// //System.out.println(replaceList2(html)); | |||
// String text="#REDIRECT [[UbuntuWiki:BelgianTeam/Leuven_2007-01-06]]"; | |||
// Pattern pattern= Pattern.compile("\\[\\[UbuntuWiki:(.*?)\\]\\]"); | |||
// Matcher matcher=pattern.matcher(text); | |||
// if (matcher.find()){ | |||
// System.out.println(matcher.group(1)); | |||
// } | |||
// String dict="community"; | |||
// System.out.println(dict); | |||
// String out=getUrl("https://help.ubuntu.com/"+dict+"?action=raw",dict); | |||
// String out =formatdict("ddrer%34[[24%s]]%343242"); | |||
// System.out.println(out); | |||
// for (int i=0 ;i<255;i++){ | |||
// System.out.print(",'"+String.valueOf((char)i)+"'"); | |||
// } | |||
// String out=getUrl("https://help.ubuntu.com/community/3DChessboard?action=raw","3DChessboard"); | |||
String out = getUrl("https://wiki.ubuntu.com/HardwareSupport?action=raw", "HardwareSupport"); | |||
System.out.println(out); | |||
} | } | ||
/** | /** | ||
* @param args the command line arguments | * @param args the command line arguments | ||
第852行: | 第1,524行: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
// TODO code application logic here | // TODO code application logic here | ||
Main main=new Main(); | Main main = new Main(); | ||
try { | try { | ||
// main.test(); | // main.test(); | ||
// main.helpstart(); | // main.laptopTestingTeam(); | ||
main.helpstart(); | |||
main.wikistart(); | main.wikistart(); | ||
}catch (Exception ex){ | } catch (Exception ex) { | ||
ex.printStackTrace(); | ex.printStackTrace(); | ||
} | } | ||
} | } | ||
} | } | ||
</ | |||
class ThreadPool { | |||
List<Thread> taskList = new ArrayList(); | |||
int count = 1; | |||
void addTask(Thread obj) { | |||
taskList.add(obj); | |||
} | |||
void addTask(Runnable obj) { | |||
Thread thread = new Thread(obj); | |||
taskList.add(thread); | |||
} | |||
@SuppressWarnings("static-access") | |||
void start() { | |||
while (true) { | |||
try { | |||
Thread.sleep(1000); | |||
} catch (InterruptedException ex) { | |||
ex.printStackTrace(); | |||
} | |||
int j = 0; | |||
for (int i = 0; i < taskList.size(); i++) { | |||
if (taskList.get(i).getState() != Thread.State.TERMINATED && taskList.get(i).getState() != Thread.State.NEW) { | |||
j++; | |||
if (j >= count) { | |||
break; | |||
} | |||
} | |||
} | |||
if (j >= count) { | |||
continue; | |||
} | |||
for (int i = 0; i < taskList.size(); i++) { | |||
if (taskList.get(i).getState() == Thread.State.NEW) { | |||
taskList.get(i).start(); | |||
j++; | |||
if (j >= count) { | |||
break; | |||
} | |||
} | |||
} | |||
boolean end = true; | |||
for (int i = 0; i < taskList.size(); i++) { | |||
if (taskList.get(i).getState() != Thread.State.TERMINATED) { | |||
end = false; | |||
break; | |||
} | |||
} | |||
if (end) { | |||
break; | |||
} | |||
} | |||
} | |||
} | |||
</source> |
2011年5月19日 (四) 17:43的最新版本
机器人,自动将 http://help.ubuntu.com 和 http://wiki.ubuntu.com 由 monimoni 格式转换到 mediawiki 格式,并自动更新和发布的小程序。 由java写成。
<source lang="java">
/*
* Main.java * * Created on 2007年5月12日, 下午1:31 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */
package wiki;
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.security.GeneralSecurityException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.X509TrustManager;
/**
* * @author oneleaf */
public class Main {
List<String> addDict = new ArrayList<String>(); List<String> oldDict = new ArrayList<String>(); String cookie = "NEED INPUT"; int readOneDictPayTime = 2500;
public String getHtmlFromStream(InputStream in) throws IOException { StringBuffer html = new StringBuffer(); int no = 0; while ((no = in.read()) > -1) { html.append((char) no); } in.close(); return new String(html.toString().getBytes("iso8859-1"), "utf-8"); }
private void addDict(String dict) { String str = dict.trim();
if (dict.startsWith("/")) { str = dict.substring(1); } if (dict.indexOf("#") > 0) { str = dict.substring(0, dict.indexOf("#")); } if (dict.indexOf("?") > 0) { str = dict.substring(0, dict.indexOf("?")); } if (dict.startsWith("./") || dict.startsWith("//")) { str = dict.substring(2); } if (dict.startsWith("../")) { str = dict.substring(3); } if (dict.endsWith(".") || dict.endsWith(")") || dict.endsWith("]") || dict.endsWith("}") || dict.endsWith(";") || dict.endsWith("+") || dict.endsWith("'") || dict.endsWith("\"") || dict.endsWith("/") || dict.endsWith(":") || dict.endsWith(",") || dict.endsWith(">")) { str = dict.substring(0, str.length() - 1); }
// if (str.toLowerCase().indexOf("team")>0) return;
if (str.trim().length() == 0) { return; } if (str.trim().length() >= 256) { return; //../CommandLine //Community => community } if ("Community".equals(str)) { return; } if (oldDict.contains(str)) { return; } if (addDict.contains(str)) { return; } addDict.add(str); }
private void delDict(int dictindex) { oldDict.add(addDict.get(dictindex)); addDict.remove(dictindex); }
private void delDict(String dict) { oldDict.add(dict); addDict.remove(dict); }
private void clearDict() { addDict.clear(); oldDict.clear(); }
private boolean findDict(String dict) { for (String d : addDict) { if (d.equals(dict)) { return true; } } for (String d : oldDict) { if (d.equals(dict)) { return true; } } return false; }
private void getDicts(String html) { Pattern pattern = Pattern.compile("\\[UbuntuHelp:(.*?)\\]"); Matcher matcher = pattern.matcher(html); while (matcher.find()) { String line = matcher.group(1); if (line.indexOf("|") > 0) { addDict(line.substring(0, line.indexOf("|"))); } else { addDict(line); } } pattern = Pattern.compile("\\[UbuntuWiki:(.*?)\\]"); matcher = pattern.matcher(html); while (matcher.find()) { String line = matcher.group(1); if (line.indexOf("|") > 0) { addDict(line.substring(0, line.indexOf("|"))); } else { addDict(line); } } }
/** Creates a new instance of Main */ public Main() { SSLContext sslContext = null; try { sslContext = SSLContext.getInstance("TLS"); X509TrustManager[] xtmArray = new X509TrustManager[]{xtm}; sslContext.init(null, xtmArray, new java.security.SecureRandom()); } catch (GeneralSecurityException gse) { } if (sslContext != null) { HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); } HttpsURLConnection.setDefaultHostnameVerifier(hnv); } private X509TrustManager xtm = new X509TrustManager() {
@Override public void checkClientTrusted(X509Certificate[] chain, String authType) { }
@Override public void checkServerTrusted(X509Certificate[] chain, String authType) { }
@Override public X509Certificate[] getAcceptedIssuers() { return null; } }; private HostnameVerifier hnv = new HostnameVerifier() {
@Override public boolean verify(String hostname, SSLSession session) { return true; } };
public String getUrl(String urladdress, String dict) throws IOException, InterruptedException { URL url = new URL(urladdress); HttpURLConnection httpConn; int SleepTime = 1; while (true) { httpConn = (HttpURLConnection) url.openConnection(); httpConn.setReadTimeout(60000); httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)"); httpConn.setRequestProperty("Content-Language", "UTF-8"); httpConn.setRequestProperty("Connection", "Keep-Alive");
if (httpConn.getResponseCode() > 500) { System.out.println("read " + httpConn.getResponseCode() + " from " + urladdress + " sleep " + SleepTime + " minute."); readOneDictPayTime = readOneDictPayTime + 100; System.out.println("adjust readOneDictPayTime to " + readOneDictPayTime); if (readOneDictPayTime > 10000) { readOneDictPayTime = 2500; } Thread.sleep(60000 * SleepTime); SleepTime = SleepTime + 1; } else if (httpConn.getResponseCode() > 400) { return ""; } else { break; } }
InputStream in = httpConn.getInputStream(); try { String html = getHtmlFromStream(in); return moin2wm(html, urladdress, dict); } finally { in.close(); } }
public String moin2wm(String html, String url, String dict) throws UnsupportedEncodingException { String text = html; if (text.indexOf("This page does not exist yet.") > 0) { return ""; } String ex = "UbuntuHelp"; String turl = "https://help.ubuntu.com/community/"; if (url.startsWith("https://wiki")) { ex = "UbuntuWiki"; turl = "https://wiki.ubuntu.com/"; }
String head = "
文章出处: |
{{#if: | [" + url.substring(0, url.indexOf("?")) + " {{{2}}}] | " + url.substring(0, url.indexOf("?")) + " }} |
\r\n
点击翻译: |
English {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/af | • {{#if: " + ex + ":" + dict + "|Afrikaans| Afrikaans}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/ar | • {{#if: " + ex + ":" + dict + "|العربية| العربية}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/az | • {{#if: " + ex + ":" + dict + "|azərbaycanca| azərbaycanca}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/bcc | • {{#if: " + ex + ":" + dict + "|جهلسری بلوچی| جهلسری بلوچی}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/bg | • {{#if: " + ex + ":" + dict + "|български| български}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/br | • {{#if: " + ex + ":" + dict + "|brezhoneg| brezhoneg}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/ca | • {{#if: " + ex + ":" + dict + "|català| català}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/cs | • {{#if: " + ex + ":" + dict + "|čeština| čeština}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/de | • {{#if: " + ex + ":" + dict + "|Deutsch| Deutsch}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/el | • {{#if: " + ex + ":" + dict + "|Ελληνικά| Ελληνικά}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/es | • {{#if: " + ex + ":" + dict + "|español| español}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/fa | • {{#if: " + ex + ":" + dict + "|فارسی| فارسی}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/fi | • {{#if: " + ex + ":" + dict + "|suomi| suomi}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/fr | • {{#if: " + ex + ":" + dict + "|français| français}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/gu | • {{#if: " + ex + ":" + dict + "|ગુજરાતી| ગુજરાતી}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/he | • {{#if: " + ex + ":" + dict + "|עברית| עברית}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/hu | • {{#if: " + ex + ":" + dict + "|magyar| magyar}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/id | • {{#if: " + ex + ":" + dict + "|Bahasa Indonesia| Bahasa Indonesia}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/it | • {{#if: " + ex + ":" + dict + "|italiano| italiano}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/ja | • {{#if: " + ex + ":" + dict + "|日本語| 日本語}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/ko | • {{#if: " + ex + ":" + dict + "|한국어| 한국어}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/ksh | • {{#if: " + ex + ":" + dict + "|Ripoarisch| Ripoarisch}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/mr | • {{#if: " + ex + ":" + dict + "|मराठी| मराठी}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/ms | • {{#if: " + ex + ":" + dict + "|Bahasa Melayu| Bahasa Melayu}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/nl | • {{#if: " + ex + ":" + dict + "|Nederlands| Nederlands}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/no | • {{#if: " + ex + ":" + dict + "|norsk| norsk}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/oc | • {{#if: " + ex + ":" + dict + "|occitan| occitan}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/pl | • {{#if: " + ex + ":" + dict + "|polski| polski}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/pt | • {{#if: " + ex + ":" + dict + "|português| português}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/ro | • {{#if: " + ex + ":" + dict + "|română| română}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/ru | • {{#if: " + ex + ":" + dict + "|русский| русский}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/si | • {{#if: " + ex + ":" + dict + "|සිංහල| සිංහල}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/sq | • {{#if: " + ex + ":" + dict + "|shqip| shqip}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/sr | • {{#if: " + ex + ":" + dict + "|српски / srpski| српски / srpski}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/sv | • {{#if: " + ex + ":" + dict + "|svenska| svenska}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/th | • {{#if: " + ex + ":" + dict + "|ไทย| ไทย}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/tr | • {{#if: " + ex + ":" + dict + "|Türkçe| Türkçe}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/vi | • {{#if: " + ex + ":" + dict + "|Tiếng Việt| Tiếng Việt}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/yue | • {{#if: " + ex + ":" + dict + "|粵語| 粵語}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/zh | • {{#if: " + ex + ":" + dict + "|中文| 中文}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/zh-hans | • {{#if: " + ex + ":" + dict + "|中文(简体)| 中文(简体)}}|}} {{#ifexist: {{#if: " + ex + ":" + dict + " | " + ex + ":" + dict + " | {{#if: 用户 | 用户:}}Wikibot}}/zh-hant | • {{#if: " + ex + ":" + dict + "|中文(繁體)| 中文(繁體)}}|}} |
{{#ifeq:" + ex + ":" + dict + "|用户:Wikibot|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: 用户:Wikibot/zh | | {{#ifexist: Wikibot/zh | | {{#ifeq: {{#titleparts:Wikibot|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:Wikibot|1|-1|}} | zh | | }} \r\n";
//删除注释 text = text.replaceAll("\r\n##(.*)", ""); text = text.replaceAll("\r\n#format(.*)", ""); text = text.replaceAll("\r\n#language(.*)", ""); text = text.replaceAll("\r\n#pragma(.*)", ""); text = text.replaceAll("\r\n#acl(.*)", ""); text = text.replaceAll("^##(.*)\r\n", ""); text = text.replaceAll("^#format(.*)\r\n", ""); text = text.replaceAll("^#language(.*)\r\n", ""); text = text.replaceAll("^#pragma(.*)\r\n", ""); text = text.replaceAll("^#acl(.*)\r\n", ""); //替换#REDIRECT PDFPrinting => #REDIRECT PDFPrinting text = text.replaceAll("#REDIRECT (\\S*)", "#REDIRECT " + "" + ex + ":$1"); text = text.replaceAll("#redirect (\\S*)", "#REDIRECT " + "" + ex + ":$1"); //#refresh 0 https://wiki.ubuntu.com/ASUS_A3H_5010_Laptop_with_Ubuntu text = text.replaceAll("#REFRESH (.*?) (\\S*)", "#REDIRECT " + "" + ex + ":$2"); text = text.replaceAll("#refresh (.*?) (\\S*)", "#REDIRECT " + "" + ex + ":$2");
//删除不要的下拉列表 text = text.replaceAll("\\[\\[Navigation\\(.*?\\)\\]\\]", ""); text = text.replaceAll("Navigation\\(.*?\\)", ""); //删除主题 text = text.replaceAll(".*TableOfContents.*", "");
//分离{{{ }}}
Pattern pattern = Pattern.compile("\\{\\{\\{(.*?)\\}\\}\\}");
Matcher matcher = pattern.matcher(text);
List<String> codes = new ArrayList();
while (matcher.find()) {
String code = matcher.group(1);
codes.add("" + code + "
");
int index = text.indexOf(matcher.group(0));
int len = matcher.group(0).length();
text = text.substring(0, index) + "@@code" + String.valueOf(codes.size()) + "@@" + text.substring(index + len);
}
pattern = Pattern.compile("\\{\\{\\{(.*?)\\}\\}\\}", Pattern.DOTALL); String html2 = text; matcher = pattern.matcher(html2); while (matcher.find()) { String code = matcher.group(1);
codes.add("
" + code + "
");
int index = text.indexOf("{{{" + matcher.group(1) + "}}}"); int len = matcher.group(1).length() + 6; text = text.substring(0, index) + "@@code" + String.valueOf(codes.size()) + "@@" + text.substring(index + len); }
//转化表格 text = tableConv(text);
//标题从二开始 text = text.replaceAll("= (.*?) =", "== $1 =="); //转化List text = replaceList(text); //BR ->
text = text.replaceAll("\\[\\[BR\\]\\]", "
"); //link convert superscripted - ^ * ^ -> * text = text.replaceAll("\\^(.*)\\^", "$1"); //link convert subscripted - ,, * ,, -> * text = text.replaceAll(",,(.*?),,", "$1");
//link convert - ["/*"] -> [dict/*] text = text.replaceAll("\\[\\[\"//(.*?)\\|(.*?)\"\\]\\]", "$2"); text = text.replaceAll("\\[\\[\"/(.*?)\\|(.*?)\"\\]\\]", "$2"); text = text.replaceAll("\\[\\[\"\\./(.*?)\\|(.*?)\"\\]\\]", "$2"); text = text.replaceAll("\\[\\[\"\\.\\./(.*?)\\|(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/../$1|$2]]");
text = text.replaceAll("\\[\\[\"//(.*?)\"\\]\\]", "$1"); text = text.replaceAll("\\[\\[\"/(.*?)\"\\]\\]", "$1"); text = text.replaceAll("\\[\\[\"\\./(.*?)\"\\]\\]", "$1"); text = text.replaceAll("\\[\\[\"\\.\\./(.*?)\"\\]\\]", "[[" + ex + ":" + dict + "/../$1|$1]]"); //link convert - /* -> dict/* //Introduction text = text.replaceAll("\\[\\[//(.*?)\\|(.*?)\\]\\]", "$2"); text = text.replaceAll("\\[\\[/(.*?)\\|(.*?)\\]\\]", "$2"); text = text.replaceAll("\\[\\[\\./(.*?)\\|(.*?)\\]\\]", "$2"); text = text.replaceAll("\\[\\[\\.\\./(.*?)\\|(.*?)\\]\\]", "[[" + ex + ":" + dict + "/../$1|$2]]");
text = text.replaceAll("\\[\\[//(.*?)\\]\\]", "$1"); text = text.replaceAll("\\[\\[/(.*?)\\]\\]", "$1"); text = text.replaceAll("\\[\\[\\./(.*?)\\]\\]", "$1"); text = text.replaceAll("\\[\\[\\.\\./(.*?)\\]\\]", "[[" + ex + ":" + dict + "/../$1|$1]]");
//link convert - [" * "] -> UbuntuHelp: * text = text.replaceAll("\\[\"(.*?)\"\\]", "$1");
//link convert - [# * ] -> * text = text.replaceAll("\\[#(.*?)\\]", "$1"); //link convert - [: / * : * ] -> * text = text.replaceAll("\\[:/(.*?):(.*?)\\]", "$2"); //link convert - [: * : * ] -> * text = text.replaceAll("\\[:(.*?):(.*?)\\]", "$2"); //link convert - [: / * ] -> UbuntuHelp: dict * text = text.replaceAll("\\[:/(.*?)\\]", "" + ex + ":" + dict + "/$1"); //link convert - [: * ] -> UbuntuHelp: * text = text.replaceAll("\\[:(.*?)\\]", "" + ex + ":$1"); //link convert - wiki:cat -> UbuntuWiki:cat //link convert - wiki:Ubuntu/cat -> UbuntuWiki:cat text = text.replaceAll(" wiki:Ubuntu/(\\S*)", " UbuntuWiki:$1"); text = text.replaceAll("\r\nwiki:Ubuntu/(\\S*)", "\r\nUbuntuWiki:$1"); text = text.replaceAll(" wiki:(\\S*)", " UbuntuWiki:$1"); text = text.replaceAll("\r\nwiki:(\\S*)", "\r\nUbuntuWiki:$1"); //link convert - [wiki:cat * ] -> * text = text.replaceAll("\\[wiki:Ubuntu/(.*?)\\ (.*?)\\]", "$2"); text = text.replaceAll("\\[wiki:(.*?)\\ (.*?)\\]", "$2"); //link convert - [wiki:cat * ] -> * text = text.replaceAll("\\[wiki:Ubuntu/(.*?)\\]", "UbuntuWiki:$1"); text = text.replaceAll("\\[wiki:(.*?)\\]", "UbuntuWiki:$1"); //link convert - [UbuntuWiki:\*] -> [UbuntuWiki:dict\*] text = text.replaceAll("\\[UbuntuWiki:\\\\(.*?)\\]", "[UbuntuWiki:" + dict + "\\$1]"); //link convert - [UbuntuHelp:\*] -> [UbuntuHelp:dict\*] text = text.replaceAll("\\[UbuntuHelp:\\\\(.*?)\\]", "[UbuntuHelp:" + dict + "\\$1]");
//Self:/ddd=dict/ Self:ddd=dict text = text.replaceAll(":Self:(.*?)//", ":" + dict + "/$1"); text = text.replaceAll(":Self:(.*?)/", ":" + dict + "/$1"); text = text.replaceAll(":Self:(.*?)", ":$1"); text = text.replaceAll(":self:(.*?)//", ":" + dict + "/$1"); text = text.replaceAll(":self:(.*?)/", ":" + dict + "/$1"); text = text.replaceAll(":self:(.*?)", ":$1");
//link convert - __ * __ -> * text = text.replaceAll("__(.*?)__", "$1");
//CategoryHomepage =>; text = text.replaceAll("Category(\\S*)", "");
text = text.replaceAll("\r\n( *)", "\r\n"); text = attachmentUrl(text, turl, dict); //xxx:http => http: text = text.replaceAll("\\[\\[(.*?):http(.*?)\\]\\]", "http$2"); //xxx:ftp => ftp: text = text.replaceAll("\\[\\[(.*?):ftp(.*?)\\]\\]", "ftp$2");
//Ubuntu:HardwareSupport =>UbuntuWiki:HardwareSupport text = text.replaceAll("\\[\\[Ubuntu:(.*?)\\]\\]", "UbuntuWiki:$1"); //[[1]] => UbuntuWiki: text = text.replaceAll("\\[\\(.*?)\\\\]", "$2"); text = text.replaceAll("\\[\\[2]\\]", "UbuntuWiki:$1"); //[[3]] => UbuntuHelp: text = text.replaceAll("\\[\\(.*?)\\\\]", "$2"); text = text.replaceAll("\\[\\[4]\\]", "UbuntuHelp:$1"); //[[5]] => UbuntuWiki: text = text.replaceAll("\\[\\(.*?)\\\\]", "$2"); text = text.replaceAll("\\[\\[6]\\]", "UbuntuWiki:$1"); //[[7]] => UbuntuHelp: text = text.replaceAll("\\[\\(.*?)\\\\]", "$2"); text = text.replaceAll("\\[\\[8]\\]", "UbuntuHelp:$1");
text = text.replaceAll("\\(.*?)\\", "$2"); text = text.replaceAll("\\(.*?)\\", "$2"); text = text.replaceAll("\\(.*?)\\", "$2"); text = text.replaceAll("\\(.*?)\\", "$2"); text = text.replaceAll("\\[9]", "UbuntuWiki:$1"); text = text.replaceAll("\\[10]", "UbuntuWiki:$1"); text = text.replaceAll("\\[11]", "UbuntuHelp:$1"); text = text.replaceAll("\\[12]", "UbuntuHelp:$1");
text = text.replaceAll("\\[\\[(.*?)/(.*?)/\\.\\./(.*?)\\]\\]", "$1/$3"); text = text.replaceAll("\\[\\[(.*?)/(.*?)/\\.\\./(.*?)\\]\\]", "$1/$3"); text = text.replaceAll("\\[\\[(.*?)/\\./(.*?)\\]\\]", "$1/$2");
//[[13]] => RedHat text = text.replaceAll("\\[\\[(http://.*?)\\%7C(.*?)\\]\\]", "[$1 $2]"); //[[14]] => RedHat text = text.replaceAll("\\[\\[(https://.*?)\\%7C(.*?)\\]\\]", "[$1 $2]"); //[[15]] => RedHat text = text.replaceAll("\\[\\[(ftp://.*?)\\%7C(.*?)\\]\\]", "[$1 $2]"); //[[16]] => [17] text = text.replaceAll("\\[\\[(http://.*?)\\]\\]", "[$1]"); //[[18]] => [19] text = text.replaceAll("\\[\\[(https://.*?)\\]\\]", "[$1]"); //[[20]] => [21] text = text.replaceAll("\\[\\[(ftp://.*?)\\]\\]", "[$1]");
//redhat => readhat text = text.replaceAll("\\[\\[(?!UbuntuWiki:|UbuntuHelp:|https|http|ftp|category)(.*?)\\|(.*?)\\]\\]", "$2");
//RedHat => ReadHat text = text.replaceAll("\\[\\[(?!UbuntuWiki:|UbuntuHelp:|https|http|ftp|category)(.*?)\\]\\]", "$1");
//转化% text = formatdict(text);
//转化单词 text = format2(text, ex, dict);
//需要还原$code$ for (int i = 0; i < codes.size(); i++) { String str = codes.get(i); String s = "@@code" + String.valueOf(i + 1) + "@@"; int m = text.indexOf(s); int n = s.length(); //text = text.substring(0, m) + str + text.substring(m + n); text = text.replace(s, str); }
String foot = "\r\n";
if (text.trim().startsWith("#REDIRECT")) {
// System.out.print(" "+text.trim());
pattern = Pattern.compile("\\[\\[" + ex + ":(.*?)\\]\\]"); matcher = pattern.matcher(text); if (matcher.find()) { //如果仅仅是大小写的重定向,就不用考虑直接忽略。 if (matcher.group(1).toLowerCase().equals(dict.toLowerCase())) { System.out.println(dict + " redirect to " + matcher.group(1) + " , ignore. "); return ""; } } return text + head + foot; } if (text.trim().length() < 10) { return ""; } return head + text + foot; }
public String formatdict(String text) { Pattern pattern = Pattern.compile("\\[\\[(.*?)\\]\\]"); Matcher matcher = pattern.matcher(text); String html = text; while (matcher.find()) { String dict = matcher.group(1); if (dict.indexOf("%") > 0) { int index = html.indexOf("" + dict + ""); int len = dict.length() + 4; html = html.substring(0, index) + "" + getDict(dict) + "" + html.substring(index + len); } } return html; }
public String getDict(String dict) { String str = ""; str = dict.replaceAll("%..", "_"); str = str.replaceAll("%.", ""); return str; }
//检查有没有至少两个大写单词存在,如果存在检查是不是词汇,如果是,转为链接。 public boolean arrayhas(char[] c, char b) { for (int i = 0; i < c.length; i++) { if (c[i] == b) { return true; } } return false; }
public String replaceBySpace(String text, String match) { Pattern pattern = Pattern.compile(match); Matcher matcher = pattern.matcher(text); String html = text; while (matcher.find()) { String str = matcher.group(0); int index = text.indexOf(str); int length = str.length(); String space = String.format("%-" + length + "s", ""); html = html.substring(0, index) + space + html.substring(index + length); } return html; }
public String format2(String html, String ex, String dict) { String result = html; int n_count = 0; int m_count = 0; char[] o = {':', '%', '?', '&', '=', '\\', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'}; char[] m = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char[] n = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; String temp = html; //删除现有的所有的链接 temp = replaceBySpace(temp, "\\{\\{.*?\\}\\}"); temp = replaceBySpace(temp, "=====.*?====="); temp = replaceBySpace(temp, "====.*?===="); temp = replaceBySpace(temp, "===.*?==="); temp = replaceBySpace(temp, "==.*?=="); temp = replaceBySpace(temp, "=.*?="); temp = replaceBySpace(temp, "\\[\\[.*?\\]\\]"); temp = replaceBySpace(temp, "\\[.*?\\]"); temp = replaceBySpace(temp, "http:.*? "); temp = replaceBySpace(temp, "https:.*? ");
temp = replaceBySpace(temp, "
.*?
");
temp = replaceBySpace(temp, ".*?
");
String word = "";
for (int i = 0; i < temp.length(); i++) {
char c = temp.charAt(i);
if (arrayhas(m, c)) {
word = word + String.valueOf(c);
m_count++;
} else if (arrayhas(n, c)) {
word = word + String.valueOf(c);
n_count++;
} else if (arrayhas(o, c)) {
word = word + String.valueOf(c);
} else {
//单词截取完成
if (n_count > 1 && m_count > 0) {
if (!word.equals("IconsPage") && !word.equals("AttachFile") &&
word.indexOf("\\") < 0 && word.indexOf("/") < 0 && findDict(word) && !word.equals(dict)) {
try {
int index = temp.indexOf(word);
int len = word.length();
String replace = "" + word + "";
result = result.substring(0, index) + replace + result.substring(index + len);
temp = temp.substring(0, index) + String.format("%-" + replace.length() + "s", "") + temp.substring(index + len);
} catch (Exception e) {
e.printStackTrace();
}
}
}
n_count = 0;
m_count = 0;
word = "";
}
}
return result;
}
public String urlEncode(String dict) { String str = dict; str = str.replaceAll(" ", "%20"); if (str.indexOf("#") > 0) { str = str.substring(0, str.indexOf("#")); } if (str.indexOf("?") > 0) { str = str.substring(0, str.indexOf("?")); } return str; }
public boolean checkListStart(String line) { String str = line; if (str.length() < 4) { return false; } if (str.charAt(0) == '.' && str.charAt(0) == ' ') { return true; } if (str.charAt(0) == '*' && str.charAt(0) == ' ') { return true; } char[] o = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; char[] m = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char[] n = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
if (arrayhas(o, str.charAt(0)) || arrayhas(m, str.charAt(0)) || arrayhas(n, str.charAt(0))) { if (str.charAt(1) == '.' && str.charAt(2) == ' ') { return true; } } return false; }
public String replaceList(String text) { String[] lines = text.split("\r\n"); String block = ""; StringBuffer bf = new StringBuffer(); boolean start = false; for (int i = 0; i < lines.length; i++) { String line = lines[i]; String linetrim = line.trim(); if (linetrim.equals("")) { continue; } if (checkListStart(linetrim)) { if (!start) { start = true; block = line; } else { block = block + "\r\n" + line; } } else { if (start) { block = formatList2(block);
bf.append(block + "\r\n"); start = false; } bf.append(lines[i] + "\r\n"); } } if (start) { block = formatList2(block); bf.append(block + "\r\n"); start = false; } return bf.toString(); }
public static int formatList_getSpaceCount(String s) { int num = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ' ') { num++; } else { break; } } return num; }
public static String formatList_clear(String mstr) { String str = mstr.trim(); if (str.startsWith(".") || str.startsWith("*")) { return str.substring(1).trim(); } else { return str.substring(2).trim(); } }
public String formatList2(String text) { String[] mlist = text.split("\r\n");
// if (mlist.length<2) return text;
while (true) { boolean done = true; for (String line : mlist) { if (formatList_getSpaceCount(line) > 0) { done = false; break; } } if (done) { break; } int lay = 0; String lType = "ol"; for (int i = 0; i < mlist.length; i++) { int space = formatList_getSpaceCount(mlist[i]); if (space > 0 && lay == 0) { String str = mlist[i].trim(); if (str.startsWith("*")) { lType = "ul"; }
mlist[i] = "<" + lType + ">
</" + lType + ">"; } } else if (space > 0 && lay == space) { mlist[i] = "
</" + lType + ">"; } } else if (lay > 0 && space == 0) { mlist[i] = "</" + lType + ">" + mlist[i]; break; } else { continue; } } } StringBuffer sb = new StringBuffer(); for (String line : mlist) { sb.append(line + "\r\n"); } return sb.toString(); } public String formatList(String text) { /* 1. one 1. two 1. one * bullet 1 * bullet 2 1. two 1. three * bullet 1. one *最好是转为*=>
#=>
*/ //获得步进长度 text = text.replaceAll(" 1\\. ", " # "); text = text.replaceAll(" a\\. ", " # "); text = text.replaceAll(" A\\. ", " # "); text = text.replaceAll(" i\\. ", " # "); text = text.replaceAll(" I\\. ", " # "); text = text.replaceAll(" \\. ", " # "); String step = ""; for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == ' ') { step = step + " "; } else { break; } }
//修改步长为" " if (step.length() > 0) { text = text.replaceAll(step, " "); } String[] lines = text.split("\r\n"); for (int i = 0; i < lines.length; i++) { if (lines[i].charAt(0) != ' ') { break; } lines[i] = lines[i].substring(1); }
StringBuffer bf = new StringBuffer(); bf.append(lines[0] + "\r\n"); for (int i = 1; i < lines.length; i++) { if (lines[i].startsWith(" ")) { char[] s = lines[i].toCharArray(); for (int j = 0; j < s.length; j++) { if (s[j] == ' ') { if (j >= lines[i - 1].length()) { s[j] = 0; } if (lines[i - 1].charAt(j) != '*' && lines[i - 1].charAt(j) != '#') { s[j] = 0; } else { s[j] = lines[i - 1].charAt(j); } } else { break; } } String l = ""; for (int j = 0; j < s.length; j++) { if (s[j] == 0) { continue; } l = l + s[j]; } lines[i] = l; } bf.append(lines[i] + "\r\n"); } return bf.toString(); }
public String attachmentUrl(String text, String baseurl, String dict) throws UnsupportedEncodingException { //attachment:IconsPage/info.png -> while (true) { Pattern pattern = Pattern.compile("\\{\\{attachment:(.*?)/(.*?)\\}\\}"); Matcher matcher = pattern.matcher(text); String replace; if (matcher.find()) { replace = baseurl + matcher.group(1) + "?action=AttachFile&do=get&target=" + URLEncoder.encode(matcher.group(2), "UTF-8"); text = text.substring(0, matcher.start(0)) + replace + text.substring(matcher.end(0)); continue; }
pattern = Pattern.compile("\\{\\{attachment:(.*?)\\}\\}"); matcher = pattern.matcher(text); if (matcher.find()) { replace = baseurl + dict + "?action=AttachFile&do=get&target=" + URLEncoder.encode(matcher.group(1), "UTF-8"); text = text.substring(0, matcher.start(0)) + replace + text.substring(matcher.end(0)); continue; }
break; } return text; }
public String tableConv(String html) { //||a||b||c|| -> {| //||d||e||f|| |a||b||c // |- // |d||e||f // |}
// System.out.println(html);
String[] lines = html.split("\r\n"); String block = ""; StringBuffer bf = new StringBuffer(); boolean start = false; for (int i = 0; i < lines.length; i++) { String line = lines[i].trim(); if (line.startsWith("||")) { if (line.length() < 4) { continue; //line like ||(.*?)|| } String str = line.substring(1, line.length() - 2);
Pattern pattern = Pattern.compile("<(.*?)>"); Matcher matcher = pattern.matcher(line); while (matcher.find()) { String x = matcher.group(1).trim(); String replace = ""; if (x.startsWith("-")) { if (x.indexOf(" ") > 0) { replace = "colspan=" + x.substring(1, x.indexOf(" ")) + "|"; } else { replace = "colspan=" + x.substring(1) + "|"; } } else if (x.startsWith("|")) { if (x.indexOf(" ") > 0) { replace = "rowspan=" + x.substring(1, x.indexOf(" ")) + "|"; } else { replace = "rowspan=" + x.substring(1) + "|"; } }
// System.out.println("match:"+matcher.group(1)+" to:"+replace);
int m = str.indexOf("<" + matcher.group(1) + ">"); int n = ("<" + matcher.group(1) + ">").length(); if (m == -1 || (m + n) > str.length()) { continue; } str = str.substring(0, m) + replace + str.substring(m + n);
// str=str.replaceFirst("<"+matcher.group(1)+">",replace);
}
if (!start) { start = true; block = "{|border=\"1\" cellspacing=\"0\"\r\n" + str; } else { block = block + "\r\n|-\r\n" + str; } } else { if (start) { block = block + "\r\n|}\r\n"; bf.append(block); start = false; } bf.append(lines[i] + "\r\n"); } } if (start) { block = block + "\r\n|}\r\n"; bf.append(block); start = false; }
return bf.toString(); }
public String getWikiHtml(String url) throws MalformedURLException, IOException { URL httpurl = new URL(url); HttpURLConnection httpConn = (HttpURLConnection) httpurl.openConnection(); httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042523 Ubuntu/9.04 (jaunty) Firefox/3.0.10"); httpConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); httpConn.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5"); httpConn.setRequestProperty("Accept-Charset", "gb2312,utf-8;q=0.7,*;q=0.7"); httpConn.setRequestProperty("Referer", "http://wiki.ubuntu.org.cn/"); httpConn.setRequestProperty("Cookie", cookie); httpConn.setUseCaches(false); httpConn.setDoInput(true); InputStream in = httpConn.getInputStream(); String wikihtml = getHtmlFromStream(in); return wikihtml; }
public void putText(String dict, String html, String surl) throws MalformedURLException, IOException { URL url; URLConnection conn; InputStream in; BufferedReader read; StringBuffer sb; if (html.length() < 5) { System.out.println(dict + " is short , ignore. " + html); return; }
// if (html.length()<300){ // if (html.toUpperCase().trim().indexOf("REFRESH")>0) { // System.out.println(dict+" is REFRESH."); // return; // } // if (html.toUpperCase().trim().indexOf("REDIRECT")>0) { // System.out.println(dict+" is REDIRECT."); // return; // } // }
String ex = "UbuntuHelp"; if (surl.startsWith("https://wiki")) { ex = "UbuntuWiki"; }
String sdict = dict; if (sdict.indexOf("%") > 0) { sdict = getDict(dict); }
try { url = new URL("http://wiki.ubuntu.org.cn/" + ex + ":" + sdict + "?action=raw"); conn = url.openConnection(); conn.setReadTimeout(60000); conn.setRequestProperty("Cookie", cookie); in = conn.getInputStream(); String wikihtml = getHtmlFromStream(in); int wikilen = (wikihtml.replaceAll("\r", "")).replaceAll("\n", "").length(); int htmllen = (html.replaceAll("\r", "")).replaceAll("\n", "").length(); if (wikilen == htmllen) { System.out.println(dict + " no changes."); return; } else { System.out.println(dict + " length: " + html.length() + " oldlength: " + wikihtml.length()); } } catch (Exception ex0) { }
String tempdict = sdict.replaceAll("%23", ""); String address = "http://wiki.ubuntu.org.cn/" + ex + ":" + tempdict + "?action=edit"; String wikihtml = null; String from = null; Boolean flag = false; for (int i = 0; i < 5; i++) { wikihtml = getWikiHtml(address); int start = wikihtml.indexOf("<form id=\"editform\"");
int end = wikihtml.indexOf("