Moinmoin to Mediawiki:修订间差异
小无编辑摘要 |
小无编辑摘要 |
||
第573行: | 第573行: | ||
</source> | </source> | ||
2007年5月15日 (二) 09:40的版本
写了一个转化程序,本程序的作用是将 moinmoin convert to mediawiki 。
<source lang="java"> /*
* Main.java * * Created on 2007年5月12日, 下午1:31 by Oneleaf * * 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.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; 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="NEED_REPLACE_OWNER_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("./")){ 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; //../CommandLine if (oldDict.contains(str)) return; if (addDict.contains(str)) return; addDict.add(str); } private void delDict(String dict){ addDict.remove(dict); oldDict.add(dict); } 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); } } } /** 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); InputStream in=url.openConnection().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: | :}}Moinmoin to Mediawiki}}/af | • {{#if: "+ex+":"+dict+"|Afrikaans| [[::Moinmoin to Mediawiki/af|Afrikaans]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/ar | • {{#if: "+ex+":"+dict+"|العربية| [[::Moinmoin to Mediawiki/ar|العربية]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/az | • {{#if: "+ex+":"+dict+"|azərbaycanca| [[::Moinmoin to Mediawiki/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/bcc | • {{#if: "+ex+":"+dict+"|جهلسری بلوچی| [[::Moinmoin to Mediawiki/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/bg | • {{#if: "+ex+":"+dict+"|български| [[::Moinmoin to Mediawiki/bg|български]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/br | • {{#if: "+ex+":"+dict+"|brezhoneg| [[::Moinmoin to Mediawiki/br|brezhoneg]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/ca | • {{#if: "+ex+":"+dict+"|català| [[::Moinmoin to Mediawiki/ca|català]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/cs | • {{#if: "+ex+":"+dict+"|čeština| [[::Moinmoin to Mediawiki/cs|čeština]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/de | • {{#if: "+ex+":"+dict+"|Deutsch| [[::Moinmoin to Mediawiki/de|Deutsch]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/el | • {{#if: "+ex+":"+dict+"|Ελληνικά| [[::Moinmoin to Mediawiki/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/es | • {{#if: "+ex+":"+dict+"|español| [[::Moinmoin to Mediawiki/es|español]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/fa | • {{#if: "+ex+":"+dict+"|فارسی| [[::Moinmoin to Mediawiki/fa|فارسی]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/fi | • {{#if: "+ex+":"+dict+"|suomi| [[::Moinmoin to Mediawiki/fi|suomi]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/fr | • {{#if: "+ex+":"+dict+"|français| [[::Moinmoin to Mediawiki/fr|français]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/gu | • {{#if: "+ex+":"+dict+"|ગુજરાતી| [[::Moinmoin to Mediawiki/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/he | • {{#if: "+ex+":"+dict+"|עברית| [[::Moinmoin to Mediawiki/he|עברית]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/hu | • {{#if: "+ex+":"+dict+"|magyar| [[::Moinmoin to Mediawiki/hu|magyar]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/id | • {{#if: "+ex+":"+dict+"|Bahasa Indonesia| [[::Moinmoin to Mediawiki/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/it | • {{#if: "+ex+":"+dict+"|italiano| [[::Moinmoin to Mediawiki/it|italiano]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/ja | • {{#if: "+ex+":"+dict+"|日本語| [[::Moinmoin to Mediawiki/ja|日本語]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/ko | • {{#if: "+ex+":"+dict+"|한국어| [[::Moinmoin to Mediawiki/ko|한국어]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/ksh | • {{#if: "+ex+":"+dict+"|Ripoarisch| [[::Moinmoin to Mediawiki/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/mr | • {{#if: "+ex+":"+dict+"|मराठी| [[::Moinmoin to Mediawiki/mr|मराठी]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/ms | • {{#if: "+ex+":"+dict+"|Bahasa Melayu| [[::Moinmoin to Mediawiki/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/nl | • {{#if: "+ex+":"+dict+"|Nederlands| [[::Moinmoin to Mediawiki/nl|Nederlands]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/no | • {{#if: "+ex+":"+dict+"|norsk| [[::Moinmoin to Mediawiki/no|norsk]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/oc | • {{#if: "+ex+":"+dict+"|occitan| [[::Moinmoin to Mediawiki/oc|occitan]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/pl | • {{#if: "+ex+":"+dict+"|polski| [[::Moinmoin to Mediawiki/pl|polski]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/pt | • {{#if: "+ex+":"+dict+"|português| [[::Moinmoin to Mediawiki/pt|português]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/ro | • {{#if: "+ex+":"+dict+"|română| [[::Moinmoin to Mediawiki/ro|română]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/ru | • {{#if: "+ex+":"+dict+"|русский| [[::Moinmoin to Mediawiki/ru|русский]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/si | • {{#if: "+ex+":"+dict+"|සිංහල| [[::Moinmoin to Mediawiki/si|සිංහල]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/sq | • {{#if: "+ex+":"+dict+"|shqip| [[::Moinmoin to Mediawiki/sq|shqip]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/sr | • {{#if: "+ex+":"+dict+"|српски / srpski| [[::Moinmoin to Mediawiki/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/sv | • {{#if: "+ex+":"+dict+"|svenska| [[::Moinmoin to Mediawiki/sv|svenska]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/th | • {{#if: "+ex+":"+dict+"|ไทย| [[::Moinmoin to Mediawiki/th|ไทย]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/tr | • {{#if: "+ex+":"+dict+"|Türkçe| [[::Moinmoin to Mediawiki/tr|Türkçe]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/vi | • {{#if: "+ex+":"+dict+"|Tiếng Việt| [[::Moinmoin to Mediawiki/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/yue | • {{#if: "+ex+":"+dict+"|粵語| [[::Moinmoin to Mediawiki/yue|粵語]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/zh | • {{#if: "+ex+":"+dict+"|中文| [[::Moinmoin to Mediawiki/zh|中文]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/zh-hans | • {{#if: "+ex+":"+dict+"|中文(简体)| [[::Moinmoin to Mediawiki/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: "+ex+":"+dict+" | "+ex+":"+dict+" | {{#if: | :}}Moinmoin to Mediawiki}}/zh-hant | • {{#if: "+ex+":"+dict+"|中文(繁體)| [[::Moinmoin to Mediawiki/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:"+ex+":"+dict+"|:Moinmoin to Mediawiki|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :Moinmoin to Mediawiki/zh | | {{#ifexist: Moinmoin to Mediawiki/zh | | {{#ifeq: {{#titleparts:Moinmoin to Mediawiki|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:Moinmoin to Mediawiki|1|-1|}} | zh | | }} \r\n";
//删除注释 text=text.replaceAll("\r\n#(.*)",""); text=text.replaceAll("^#(.*?)\r\n",""); //删除主题 text=text.replaceAll(".*TableOfContents.*",""); //标题从二开始 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 - [" * "] -> 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: * 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 - {{{ * ->
* text=text.replaceAll("\\{\\{\\{(.*)","<pre><nowiki>$1"); //link convert - * }}} -> * <\pre><\nowiki> text=text.replaceAll("(.*?)\\}\\}\\}","$1
");
//CategoryHomepage =>; text=text.replaceAll("Category(\\S*)",""); text=replaceUrl(text,turl,dict); text=tableConv(text); String foot="\r\n"; if (text.trim().length()<10){ return ""; } return head+text+foot; } 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"); 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"); } 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; block="{|border=\"1\" cellspacing=\"0\"\r\n"+line.substring(1,line.length()-2); }else{ block=block+"\r\n|-\r\n"+line.substring(1,line.length()-2); } }else{ if (start){ block=block+"\r\n|}\r\n"; bf.append(block); start=false; } bf.append(lines[i]+"\r\n"); } } 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.println(dict+" is short."); 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.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.println(dict+" no changes."); return; } }catch(Exception ex0){ //nothing } url=new URL("http://wiki.ubuntu.org.cn/"+ex+":"+dict+"?action=edit"); conn = url.openConnection(); 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"); } int start=sb.indexOf("<form id=\"editform\"");
int end=sb.indexOf("