用户:Wikibot:修订间差异
小无编辑摘要 |
小无编辑摘要 |
||
第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"> | |||
<source lang="java"> | |||
/* | /* | ||
* Main.java | * Main.java | ||
第94行: | 第92行: | ||
cookie=cookie+"; "+list.get(i); | cookie=cookie+"; "+list.get(i); | ||
} | } | ||
} catch (Exception ex){ | } catch (Exception ex){ | ||
ex.printStackTrace(); | ex.printStackTrace(); | ||
第133行: | 第107行: | ||
}else if (dict.indexOf("?")>0){ | }else if (dict.indexOf("?")>0){ | ||
str=dict.substring(0,dict.indexOf("?")); | str=dict.substring(0,dict.indexOf("?")); | ||
}else if (dict.startsWith("./")){ | }else if (dict.startsWith("./") || dict.startsWith("//")){ | ||
str=dict.substring(2); | str=dict.substring(2); | ||
}else if (dict.startsWith("../")){ | }else if (dict.startsWith("../")){ | ||
第162行: | 第136行: | ||
Pattern pattern= Pattern.compile("\\[UbuntuHelp:(.*?)\\]"); | Pattern pattern= Pattern.compile("\\[UbuntuHelp:(.*?)\\]"); | ||
Matcher matcher=pattern.matcher(html); | 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()) { | while(matcher.find()) { | ||
String line=matcher.group(1); | String line=matcher.group(1); | ||
第255行: | 第239行: | ||
text=text.replaceAll("= (.*?) =","== $1 =="); | text=text.replaceAll("= (.*?) =","== $1 =="); | ||
//转化List | //转化List | ||
text= | text=replaceList2(text); | ||
//[[BR]] -> <BR> | //[[BR]] -> <BR> | ||
text=text.replaceAll("\\[\\[BR\\]\\]","<br>"); | text=text.replaceAll("\\[\\[BR\\]\\]","<br>"); | ||
第262行: | 第246行: | ||
//link convert subscripted - ,, * ,, -> <sub> * </sub> | //link convert subscripted - ,, * ,, -> <sub> * </sub> | ||
text=text.replaceAll(",,(.*?),,","<sub>$1</sub>"); | text=text.replaceAll(",,(.*?),,","<sub>$1</sub>"); | ||
//link convert - ["/*"] -> [[ex: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]]"); | ||
第324行: | 第312行: | ||
if (text.trim().startsWith("#REDIRECT")){ | if (text.trim().startsWith("#REDIRECT")){ | ||
System.out.print(dict + " | System.out.print(" "+text.trim()); | ||
Pattern pattern= Pattern.compile("\\[\\["+ex+":(.*?)\\]\\]"); | |||
Matcher matcher=pattern.matcher(text); | |||
if (matcher.find()){ | |||
//如果仅仅是大小写的重定向,就不用考虑直接忽略。 | |||
if (matcher.group(1).toLowerCase().equals(dict.toLowerCase())){ | |||
System.out.println("\r\n"+dict+" redirect to "+matcher.group(1)+" , 忽略. "); | |||
return ""; | |||
} | |||
} | |||
return text+head+foot; | return text+head+foot; | ||
} | } | ||
第331行: | 第328行: | ||
} | } | ||
return head+text+foot; | return head+text+foot; | ||
} | |||
public String replaceList2(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 (linetrim.startsWith(". ")||linetrim.startsWith("* ")||linetrim.startsWith("1. ") | |||
||linetrim.startsWith("a. ")||linetrim.startsWith("A. ")||linetrim.startsWith("i. ") | |||
||linetrim.startsWith("I. ")){ | |||
if (! start){ | |||
start=true; | |||
block=line; | |||
}else{ | |||
block=block+"\r\n"+line; | |||
} | |||
}else{ | |||
if (start){ | |||
block=formatList(block); | |||
bf.append(block); | |||
start=false; | |||
} | |||
bf.append(lines[i]+"\r\n"); | |||
} | |||
} | |||
if (start){ | |||
block=formatList(block); | |||
bf.append(block); | |||
start=false; | |||
} | |||
return bf.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(); | |||
} | } | ||
第422行: | 第519行: | ||
str=str.replaceAll("<style.*?>",""); | str=str.replaceAll("<style.*?>",""); | ||
str=str.replaceAll("<rowbgcolor.*?>",""); | str=str.replaceAll("<rowbgcolor.*?>",""); | ||
str=str.replaceAll("<tablestyle.*?>",""); | |||
block="{|border=\"1\" cellspacing=\"0\"\r\n"+str; | block="{|border=\"1\" cellspacing=\"0\"\r\n"+str; | ||
}else{ | }else{ | ||
第841行: | 第939行: | ||
// | // | ||
// 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)); | |||
} | |||
} | } | ||
第864行: | 第982行: | ||
} | } | ||
</source> | </source> |
2007年11月30日 (五) 14:51的版本
机器人,自动将 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.BufferedInputStream; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; 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.Security; import java.security.cert.X509Certificate; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Vector; 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 Vector<String>(); List <String> oldDict= new Vector<String>(); String cookie=getCookie(); private String getCookie(){ String cookie = ""; try{ URL httpurl = new URL("http://wiki.ubuntu.org.cn/index.php?title=Special:Userlogin"); HttpURLConnection httpConn = (HttpURLConnection)httpurl.openConnection(); httpConn.addRequestProperty("Cookie",cookie); httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)"); httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpConn.setRequestProperty("Connection", "Keep-Alive"); httpConn.setUseCaches(false); cookie=httpConn.getHeaderField("Set-Cookie"); String data="wpName=wikibot&wpPassword=********&wpRemember=1"; httpurl = new URL("http://wiki.ubuntu.org.cn/index.php?title=Special:Userlogin&action=submitlogin&type=login"); httpConn = (HttpURLConnection)httpurl.openConnection(); httpConn.setRequestMethod("POST"); httpConn.addRequestProperty("Cookie",cookie); httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)"); httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpConn.setRequestProperty("Content-Language","UTF-8" ); httpConn.setRequestProperty("Content-Length", ""+data.getBytes().length); httpConn.setRequestProperty("Connection", "Keep-Alive"); httpConn.setDoOutput(true); httpConn.setDoInput(true); httpConn.setUseCaches(false); DataOutputStream outStream = new DataOutputStream(httpConn.getOutputStream()); outStream.writeBytes(data); outStream.flush(); outStream.close(); List <String> list=httpConn.getHeaderFields().get("Set-Cookie"); for (int i=0;i<list.size();i++){ cookie=cookie+"; "+list.get(i); } } catch (Exception ex){ ex.printStackTrace(); } return cookie; } private void addDict(String dict){ String str=dict.trim(); if (dict.startsWith("/")){ str=dict.substring(1); }else if (dict.indexOf("#")>0){ str=dict.substring(0,dict.indexOf("#")); }else if (dict.indexOf("?")>0){ str=dict.substring(0,dict.indexOf("?")); }else if (dict.startsWith("./") || dict.startsWith("//")){ str=dict.substring(2); }else if (dict.startsWith("../")){ str=dict.substring(3); } if (str.toLowerCase().indexOf("team")>0) return; if (str.trim().length()==0) return; if (str.trim().length()>=256) return; //../CommandLine 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 clearDict(){ addDict.clear(); oldDict.clear(); } 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() { public void checkClientTrusted(X509Certificate[] chain, String authType) {} public void checkServerTrusted(X509Certificate[] chain, String authType) {} public X509Certificate[] getAcceptedIssuers() { return null; } }; private HostnameVerifier hnv = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; public String getUrl(String urladdress,String dict) throws IOException{ URL url=new URL(urladdress); HttpURLConnection 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"); InputStream in=httpConn.getInputStream(); 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"); } return moin2wm(sb.toString(),urladdress,dict); } public String moin2wm(String html,String url,String dict) throws UnsupportedEncodingException{ String text=html; 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(".*TableOfContents.*",""); //标题从二开始 text=text.replaceAll("= (.*?) =","== $1 =="); //转化List text=replaceList2(text); //BR ->
text=text.replaceAll("\\[\\[BR\\]\\]","
"); //link convert superscripted - ^ * ^ -> * text=text.replaceAll("\\^(.*)\\^","$1"); //link convert subscripted - ,, * ,, -> * text=text.replaceAll(",,(.*?),,","$1"); //link convert - ["/*"] -> ex:dict/* text=text.replaceAll("\\[//(.*?)\"\\]",""+ex+":"+dict+"/$1"); text=text.replaceAll("\\[/(.*?)\"\\]",""+ex+":"+dict+"/$1"); //link convert - [" * "] -> UbuntuHelp: * text=text.replaceAll("\\[\"(.*?)\"\\]",""+ex+":$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 text=text.replaceAll(" wiki:(\\S*)"," UbuntuWiki:$1"); text=text.replaceAll("\r\nwiki:(\\S*)","\r\nUbuntuWiki:$1"); //link convert - [wiki:cat * ] -> * text=text.replaceAll("\\[wiki:(.*?)\\ (.*?)\\]","$2"); //link convert - [wiki:cat * ] -> * 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]"); //link convert - __ * __ -> * text=text.replaceAll("__(.*?)__","$1"); //link convert - {{{ * }}} ->*
text=text.replaceAll("\\{\\{\\{(.*?)\\}\\}\\}","$1
");
//link convert - \r\n {{{ * -> \r\n
* text=text.replaceAll("\r\n([ \\.]*?)\\{\\{\\{(.*)","\r\n<pre><nowiki>$2"); //link convert - {{{ * -> <pre><nowiki> * text=text.replaceAll("\\{\\{\\{(.*)","\r\n<pre><nowiki>$1"); //link convert - * }}} -> * <\pre><\nowiki> text=text.replaceAll("(.*?)\\}\\}\\}","$1
");
//CategoryHomepage =>; text=text.replaceAll("Category(\\S*)",""); text=text.replaceAll("\r\n( *)","\r\n"); text=replaceUrl(text,turl,dict); text=tableConv(text); //xxx:http => http: text=text.replaceAll("\\[\\[(.*?):http(.*?)\\]\\]","http$2"); //xxx:ftp => ftp: text=text.replaceAll("\\[\\[(.*?):ftp(.*?)\\]\\]","ftp$2"); //[[1]] => UbuntuWiki: text=text.replaceAll("\\[\\[2]\\]","UbuntuWiki:$1"); //[[3]] => UbuntuHelp: text=text.replaceAll("\\[\\[4]\\]","UbuntuHelp:$1"); //[[5]] => UbuntuWiki: text=text.replaceAll("\\[\\[6]\\]","UbuntuWiki:$1"); //[[7]] => UbuntuHelp: text=text.replaceAll("\\[\\[8]\\]","UbuntuHelp:$1"); String foot="\r\n"; if (text.trim().startsWith("#REDIRECT")){ System.out.print(" "+text.trim()); Pattern pattern= Pattern.compile("\\[\\["+ex+":(.*?)\\]\\]"); Matcher matcher=pattern.matcher(text); if (matcher.find()){ //如果仅仅是大小写的重定向,就不用考虑直接忽略。 if (matcher.group(1).toLowerCase().equals(dict.toLowerCase())){ System.out.println("\r\n"+dict+" redirect to "+matcher.group(1)+" , 忽略. "); return ""; } } return text+head+foot; } if (text.trim().length()<10){ return ""; } return head+text+foot; } public String replaceList2(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 (linetrim.startsWith(". ")||linetrim.startsWith("* ")||linetrim.startsWith("1. ") ||linetrim.startsWith("a. ")||linetrim.startsWith("A. ")||linetrim.startsWith("i. ") ||linetrim.startsWith("I. ")){ if (! start){ start=true; block=line; }else{ block=block+"\r\n"+line; } }else{ if (start){ block=formatList(block); bf.append(block); start=false; } bf.append(lines[i]+"\r\n"); } } if (start){ block=formatList(block); bf.append(block); start=false; } return bf.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 replaceList(String text){ text=text.replaceAll("\r\n \\. (.*?)","\r\n* $1"); text=text.replaceAll("\r\n \\. (.*?)","\r\n** $1"); text=text.replaceAll("\r\n \\. (.*?)","\r\n*** $1"); text=text.replaceAll("\r\n \\. (.*?)","\r\n**** $1"); text=text.replaceAll("\r\n \\. (.*?)","\r\n***** $1"); text=text.replaceAll("\r\n \\. (.*?)","\r\n****** $1"); text=text.replaceAll("\r\n \\*(.*?)","\r\n*$1"); text=text.replaceAll("\r\n \\*(.*?)","\r\n**$1"); text=text.replaceAll("\r\n \\*(.*?)","\r\n***$1"); text=text.replaceAll("\r\n \\*(.*?)","\r\n****$1"); text=text.replaceAll("\r\n \\*(.*?)","\r\n*****$1"); text=text.replaceAll("\r\n \\*(.*?)","\r\n******$1");
// text=text.replaceAll("\r\n \\. (.*?)","\r\n#: $1"); // text=text.replaceAll("\r\n \\. (.*?)","\r\n##: $1"); // text=text.replaceAll("\r\n \\. (.*?)","\r\n###: $1"); // text=text.replaceAll("\r\n \\. (.*?)","\r\n####: $1"); // text=text.replaceAll("\r\n \\. (.*?)","\r\n#####: $1"); // text=text.replaceAll("\r\n \\. (.*?)","\r\n######: $1"); // text=text.replaceAll("\r\n \\*(.*?)","\r\n#$1"); // text=text.replaceAll("\r\n \\*(.*?)","\r\n##$1"); // text=text.replaceAll("\r\n \\*(.*?)","\r\n###$1"); // text=text.replaceAll("\r\n \\*(.*?)","\r\n####$1"); // text=text.replaceAll("\r\n \\*(.*?)","\r\n#####$1"); // text=text.replaceAll("\r\n \\*(.*?)","\r\n######$1");
for (int i=1;i<20;i++){
// text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n#$1"); // text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n##$1"); // text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n###$1"); // text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n####$1"); // text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n#####$1"); // text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n######$1");
text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n*$1"); text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n**$1"); text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n***$1"); text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n****$1"); text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n*****$1"); text=text.replaceAll("\r\n "+i+"\\.(.*?)","\r\n******$1"); } return text; } public String replaceUrl(String text,String baseurl,String dict) throws UnsupportedEncodingException{ //attachment:IconsPage/info.png -> while (true){ Pattern pattern= Pattern.compile("attachment:(.*?)/(\\S*)"); 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:(\\S*)"); 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 // |} 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 (! start){ start=true; String str=line.substring(1,line.length()-2); str=str.replaceAll("<bgcolor.*?>",""); str=str.replaceAll("<style.*?>",""); str=str.replaceAll("<rowbgcolor.*?>",""); str=str.replaceAll("<tablestyle.*?>",""); block="{|border=\"1\" cellspacing=\"0\"\r\n"+str; }else{ String str=line.substring(1,line.length()-2); str=str.replaceAll("<bgcolor.*?>",""); str=str.replaceAll("<style.*?>",""); str=str.replaceAll("<rowbgcolor.*?>",""); 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 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.print(" is short:"+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"; } try{ url=new URL("http://wiki.ubuntu.org.cn/"+ex+":"+dict+"?action=raw"); conn = url.openConnection(); conn.setReadTimeout(60000); conn.setRequestProperty("Cookie", cookie); in=conn.getInputStream(); read=new BufferedReader(new InputStreamReader(in)); sb=new StringBuffer(); while (true){ String line=read.readLine(); if (line==null) break; sb.append(line+"\r\n"); } if (sb.toString().trim().length()==html.trim().length()) { System.out.print(" no changes"); return; } }catch(Exception ex0){ //nothing } url=new URL("http://wiki.ubuntu.org.cn/"+ex+":"+dict+"?action=edit"); conn = url.openConnection(); conn.setReadTimeout(60000); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Cookie", cookie); conn.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)"); conn.setRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); in=conn.getInputStream(); read=new BufferedReader(new InputStreamReader(in)); sb=new StringBuffer(); while (true){ String line=read.readLine(); if (line==null) break; sb.append(line+"\r\n"); } int start=sb.indexOf("<form id=\"editform\"");
int end=sb.indexOf("