// importScript('User:Sl/wiki/hks2u.js');
document.write('<script type="text/javascript" src="'
+ 'http://zh-yue.wikipedia.org/w/index.php?title=User:Sl/wiki/hks2u.js'
+ '&action=raw&ctype=text/javascript&dontcountme=s"></script>');
hD="0123456789ABCDEF";
function h2d(h) {return parseInt(h,16);}
function d2h(d) {
var h = hD.substr(d&15,1);
while(d>15) {d>>=4;h=hD.substr(d&15,1)+h;}
switch(h.length) {
case 1: h = "000" + h; break;
case 2: h = "00" + h; break;
case 3: h = "0" + h; break;
}
return h;
}
function supplane(num) {
if(num >= 65536) num -= 65536;
var num1 = Math.floor(num / 1024) + 55296;
var num2 = num % 1024 + 56320;
return String.fromCharCode(num1) + String.fromCharCode(num2);
}
function converte2u() {
var content = document.getElementById("wpTextbox1").value;
var newStr = "";
if(content.length == 0) {
newStr = default_text;
}
else {
content = content.replace(/&#x([\dA-Fa-f]+);/g, function(w){return "\037\036" + h2d(w.substr(3,w.length-2)) + "\037"});
content = content.replace(/&#(\d+);/g, "\037\036$1\037");
var col_array = content.split("\037");
for (var i = 0; i < col_array.length; i++) {
if(col_array[i] == 0) continue;
if(col_array[i].substr(0,1) == "\036") {
if(col_array[i].substr(1,col_array[i].length-1) >= 65536) {
newStr += supplane(col_array[i].substr(1,col_array[i].length-1));
}
else {
newStr += String.fromCharCode(col_array[i].substr(1,col_array[i].length-1));
}
}
else newStr += col_array[i];
}
}
document.getElementById("wpTextbox1").value = newStr;
}
function converth2u() {
var content = document.getElementById("wpTextbox1").value;
var newStr = "";
if(content.length == 0) {
newStr = default_text;
}
else for(var i = 0; i < content.length; i++) {
var unicode = content.charCodeAt(i);
var unicode2 = table2u[unicode];
if(unicode2 > 65535) {
newStr += supplane(unicode2);
}
else {
newStr += ( unicode2 ) ? String.fromCharCode(unicode2) : String.fromCharCode(unicode);
}
}
document.getElementById("wpTextbox1").value = newStr;
}
function imagetag2file() {
var oldContent = document.getElementById("wpTextbox1").value;
var newContent = oldContent.replace(/<\/br>/g, "<br />");
newContent = newContent.replace(/\{\{\s*Template:/gi, "{{");
newContent = newContent.replace(/\[\[《([^》]+)》\]\]/g, "《[[$1]]》");
newContent = newContent.replace(/\{\{\s*Template:/gi, "{{");
newContent = newContent.replace(/<noinclude>[\r\n]{2,}/gi, "<noinclude>\r\n");
newContent = newContent.replace(/\[\[\s*(:?)\s*Image\s*:/gi, "[[$1File:");
newContent = newContent.replace(/\[\[\s*(:?)\s*图像\s*:/g, "[[$1File:");
newContent = newContent.replace(/\[\[\s*(:?)([\-a-z]*:?)\s*category\s*:/gi, "[[$1$2Category:");
newContent = newContent.replace(/\[\[\s*(:?[\-a-z]+:)\s*/gi, "[[$1");
newContent = newContent.replace(/([\r\n][\*#:]+)\s*/g, "$1");
newContent = newContent.replace(/\s*<br\s*\/?\s*>/gi, "<br />");
newContent = newContent.replace(/(\S?)[ \t ]+[\r\n\cM\0]/g, "$1\r\n");
newContent = newContent.replace(/[\r\n]{3,}/g, "\r\n\r\n");
newContent = newContent.replace(/(\={2,})[\r\n]{2,}/g, "$1\r\n");
document.getElementById("wpTextbox1").value = newContent;
}
addOnloadHook(function() {
var choice = document.createDocumentFragment();
var choiceCheck = document.createElement('input');
var choiceLabel = document.createElement('label');
choiceCheck.setAttribute('type', 'checkbox');
choiceCheck.setAttribute('value', '1');
choiceCheck.setAttribute('id', 'dontConvertImage');
choiceLabel.setAttribute('for', 'dontConvertImage');
choiceLabel.setAttribute('title', '不要把文件前綴轉換為 [[File:');
choiceLabel.appendChild(document.createTextNode('不轉換為 [[File:'));
choice.appendChild(choiceCheck);
choice.appendChild(document.createTextNode(' '));
choice.appendChild(choiceLabel);
choice.appendChild(document.createTextNode('\n'));
$('wpMinoredit').parentNode.insertBefore(choice, $('wpMinoredit'));
addHandler($('editform'), 'submit', function(e) {
if ($('dontConvertImage').checked) return true;
imagetag2file();
converth2u();
return true;
});
});