跳转到内容

User:Date delinker/EngvarB.js

维基百科,自由的百科全书

这是本页的一个历史版本,由Date delinker留言 | 贡献2010年8月12日 (四) 02:16 (replaced protect function)编辑。这可能和当前版本存在着巨大的差异。

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/*************
*** Regex menu framework
*** by [[m:user:Pathoschild]] <http://meta.wikimedia.org/wiki/User:Pathoschild/Scripts/Regex_menu_framework>
***	- adds a sidebar menu of user-defined scripts.
*************/
importScriptURI('http://meta.wikimedia.org/w/index.php?title=User:Pathoschild/Scripts/Regex_menu_framework.js&action=raw&ctype=text/javascript');
 
/* menu links */
// In the function below, add more lines like "regexTool('link text','function_name()')" to add
// links to the sidebar menu. The function name is the function defined in rfmscripts() below.
function rmflinks() {
	regexTool('Custom regex','custom()'); // a default tool which performs regex input in a dynamic form
	regexTool('Edit my regexes ↗','function opennew(url) { window.open(url); }; opennew(wgServer + wgScript + "?title=User:" + wgUserName + "/EngvarB.js&action=edit");');

	regexTool('• Protect words','protectwords()');
	regexTool('• British spelling','BritishEnglish()');
	regexTool('• British (OED) spelling','OxEnglish()');
	regexTool('• British (OED) passive','OxPassive()');

	//Fix formatting of links in the sidebar
	var r = document.getElementById('p-regex')
	if (r){
		r.className += ' portal'
		var d = r.getElementsByTagName('div')
		if (d[0]) d[0].className += ' pBody body'
	}
}
 
/* scripts */
// Below, define the functions linked to from rmflinks() above. These functions can use any JavaScript,
// but there is a set of simplified tools documented at
// http://meta.wikimedia.org/wiki/User:Pathoschild/Script:Regex_menu_framework .

function protectwords(){
	var table = [
		'(al)(uminum)',
		'(ar)(tifact)',
		'(ana|cata|hydro|para)(lyz)',
		'(ly)(zing)',
		'(di)(arrhea)',
		'(de|of)(fense)',
		'(fu)(ror)',
		'(gr)(ay)',
		'(je)(welry)',
		'(ma)(neuver)', 
		'(pe)(diatric)',
		'(pe)(dophil)',
		'(sk)(eptic)',
		'(in)(quiry)',
		'(va)(por)',
		'(on)(stage)',
		'(break|clean|lock|pick)(up)',
		'(he)(mo)',
		'(or)(thopedic)',
		'(ar)(cheo)',
		'(pa)(leonto)',
		'(en)(ology)',
		'(es)(ophag)',
		'(es)(trogen)',
		'(es)(thetic)',
		'(ho)(meopath)',
		'(me)(dieval)',
		'(ri)(gor)',
		'(an|leuk|septic|tox)(emi)',
		'(su)(lfur)',
		'(lik|liv|rat|sal|siz|shak)(able)',
		'(clam|glam|harb|neighb|rum|savi?)(or)',
		'(behavi|col|fl?av|hon|hum|lab)(or)',
		'(mo)(ld)',
		'(calib|fib|goit|lust|mit|nit|reconnoit|sab|saltpet|spect|theat|tit)(er)',
		'(cent|epicent|recent)(er)',
		'(centi|kilo|milli|\\d\\s)(meter)',
		'(centi|milli|deci|pico|hecto)(liter)',
		'(dema|peda|mono|syna)(gog)',
		'(ana|cata|dia|epi|homo|mono|pro)(log)',
		'(iz)(e[drs]?|ations?|ing)',
		'(iz)(e[drs]?|ations?|ing)', //repeating to catch second instances within protected strings (quotations in particular)
		'(el)(e[dr]|est|ing)',
		'(bev|jew|lev|mod|rev|trav)(el)',
		'([a-z]{2,8}[^egl\\b])(el)',
		'(counci|dia)(le[dr]|ling|lor)',
		'(app|enthr|riv|sign|tot)(ale[dr]|aling)',
	];

	for (i=0; i<table.length; i++) {
		var re = new RegExp('(<.+?>[^<]+?)' + table[i] + '([^<]+?<\\/.+?>)', 'g')
		regex(re, '$1$2♫$3$4')

		var re = new RegExp('(\\[Category:[^\\]]*?)' + table[i] + '([^\\]]*?\\])', 'g')
		regex(re, '$1$2♫$3$4')

		var re = new RegExp('(\\[(?:file|image):[^\\.]{0,20})' + table[i] + '([^\\.]{0,20}\\.)', 'g')
		regex(re, '$1$2♫$3$4')

		var re = new RegExp('({[^{]{0,6}quot[^\\|]{1,7}\\s?\\|[^}]*?)' + table[i] + '([^}]*?})', 'g')
		regex(re, '$1$2♫$3$4')
	}
}
function Simple(){
 var box=document.editform.wpTextbox1;
 var txt = box.value;

//protectwords()

  txt=txt.replace(/([^\w\d\-\/])aluminum/g, '$1aluminium');
  txt=txt.replace(/([^\w\d\-\/])artifact/g, '$1artefact');
  txt=txt.replace(/([^\w\d\-\/])(ana|cata|hydro|para)lyz(e|ing)/g, '$1$2lys$3');
  txt=txt.replace(/([^\w\d\-\/])diarrhea/g, '$1diarrhoea');
  txt=txt.replace(/([^\w\d\-\/])(de|of)fense/g, '$1$2fence');
  txt=txt.replace(/([^\w\d\-\/])furor([^\w\d\-\/])/g, '$1furore$2');
  txt=txt.replace(/([^\w\d\-\/])(light-|dark-|\s)gray/g, '$1$2grey');
//  txt=txt.replace(/([^\w\d\-\/])judgment/g, '$1judgement');
  txt=txt.replace(/([^\w\d\-\/])jewelry/g, '$1jewellery');
  txt=txt.replace(/([^\w\d\-\/])maneuver(ab|ed|ing)/g, '$1manoeuvr$2');
  txt=txt.replace(/([^\w\d\-\/])maneuver/g, '$1manoeuvre');
  txt=txt.replace(/([^\w\d\-\/])ped(iatric|ophil)/g, '$1paed$2');
  txt=txt.replace(/([^\w\d\-\/])skeptic/g, '$1sceptic');
  txt=txt.replace(/([^\w\d\-\/])in(quir(?:e|ing))/g, '$1en$2');
  txt=txt.replace(/([^\w\d\-\/])vapor(s?[^\w\d\-\/])/g, '$1vapour$2');

//composite words
//  txt=txt.replace(/([^\w\d\-\/])onstage/g, '$1on-stage');
  txt=txt.replace(/([^\w\d\-\/])(break|lock|pick)up/g, '$1$2-up');

 //latin words
  txt=txt.replace(/([^\w\d\-\/])gyneco/g, '$1gynaeco');
  txt=txt.replace(/([^\w\d\-\/])hemo(globin|ly|phil)/g, '$1haemo$2');
  txt=txt.replace(/([^\w\d\-\/])orthopedic/g, '$1orthopaedic');
  txt=txt.replace(/([^\w\d\-\/])archeo/g, '$1archaeo');
  txt=txt.replace(/([^\w\d\-\/])paleonto/g, '$1palaeonto');
  txt=txt.replace(/([^\w\d\-\/])enology/g, '$1oenology');
  txt=txt.replace(/([^\w\d\-\/])esophag/g, '$1oesophag');
  txt=txt.replace(/([^\w\d\-\/])estrogen/g, '$1oestrogen');
  txt=txt.replace(/([^\w\d\-\/])esthetic/g, '$1aesthetic');
  txt=txt.replace(/([^\w\d\-\/])homeopath/g, '$1homoeopath');
//  txt=txt.replace(/([^\w\d\-\/])medieval([^\w\d\-\/])/g, '$1mediaeval$2');
  txt=txt.replace(/([^\w\d\-\/])rigor([^\w\d\-\/])/g, '$1rigour');
  txt=txt.replace(/([^\w\d\-\/])(an|leuk|septic|tox)emi(a|c)/g, '$1$2aemi$3'); 

 //'f' words
  txt=txt.replace(/([^\w\d\-\/])sulfur/g, '$1sulphur');

// non-redundant e
  txt=txt.replace(/([^\w\d\-\/])(lik|liv|rat|sal|siz|shak)(able)/g, '$1$2e$3'); 

//'~our' words
  txt=txt.replace(/([^\w\d\-\/])(clam|glam|harb|neighb|rum|savi?)or(ed|ful|ing|less|ly|s|)([^\w\d\-\/])/g, '$1$2our$3$4');
  txt=txt.replace(/([^\w\d\-\/])neighborhood/g, '$1neighbourhood');
  txt=txt.replace(/([^\w\d\-\/])behavior(al|s?[^\w\d\-\/])/g, '$1behaviour$2');
  txt=txt.replace(/([^\w\d\-\/])(fav|hon)or(abl[ey]|ed|ing|s|)([^\w\d\-\/])/g, '$1$2our$3$4');
  txt=txt.replace(/([^\w\d\-\/])labor(e[dr]|ing|s?\W)/g, '$1labour$2');
  txt=txt.replace(/([^\w\d\-\/\|])(flav|hum)or(ed|ful|fully|ings?|less|lessly|s\W)/g, '$1$2our$3');
  txt=txt.replace(/([^\w\d\-\/\|])color(ed|ful|fully|ings?|less|lessly)/g, '$1colour$2');
//  txt=txt.replace(/([^\w\d\-\/\|])colors([^\W\s]|[\s]*[^=])/g, '$1colours$2');
  txt=txt.replace(/( [\w\D]*\-)(col|flav|hum)ored/g, '$1$2oured');

  txt=txt.replace(/([^\w\d\-\/])mold(ed|ing|s?\W)/g, '$1mould$2');

//'~re~' words
  txt=txt.replace(/([^\w\d\-\/=])(calib|fib|goit|lust|mit|nit|reconnoit|sab|saltpet|spect|theat|tit)er(ed|ing)/g, '$1$2r$3'); 
  txt=txt.replace(/([^\w\d\-\/])(calib|fib|goit|lust|mit|nit|reconnoit|sab|saltpet|spect|theat|tit)er(s?\W)/g, '$1$2re$3');
  txt=txt.replace(/([^\w\d\-\/=])(cent|epicent|recent)er(ed|ing)/g, '$1$2r$3'); 
  txt=txt.replace(/([^\w\d\-\/=])(cent|epicent|recent)ers/g, '$1$2res'); 
  txt=txt.replace(/([^\w\d\-\/])(centi|kilo|milli|-|\d\s)meter/g, '$1$2metre');
  txt=txt.replace(/([^\w\d\-\/])(centi|milli|deci|pico|hecto|\b)liter(s?\b)/g, '$1$2litre$3');
  txt=txt.replace(/([^\w\d\-\/])(dema|peda|mono|syna)gog(s?\W)/g, '$1$2gogue$3');
  txt=txt.replace(/([^\w\d\\\/\-])(ana|cata|dia|epi|homo|mono|pro)log(s?[^\w\d\-\/])/g, '$1$2logue$3');
//  txt=txt.replace(/([^\w\d\-\/])(\W[a-z]{3,12}[^s\b])iz(e[drs]?|ations?|ing)/g, '$1$2is$3');
//  txt=txt.replace(/([^\w\d\-\/])(empha)siz(e|ing)/g, '$1$2sis$3'); //rem synthesiser conversion - equally common

//'~l' words where the 'l' doubles as past and present participle
  txt=txt.replace(/([^\w\d\-\/])(nov|pan)el(ist)/g, '$1$2ell$3'); 
  txt=txt.replace(/([^\w\d\-\/])(bev|jew|lev|mod|rev|trav)el(e[dr]|ing)/g, '$1$2ell$3'); 
  txt=txt.replace(/([^\w\d\-\/])([a-z]{2,8}[^egl ])el(e[dr]|est|ing)([^\w\d\-\/])/g, '$1$2ell$3$4');
  txt=txt.replace(/([^\w\d\-\/])(counci|dia)l(e[dr]|ing|ors?)([^\w\d\-\/])/gi, '$1$2ll$3$4');
  txt=txt.replace(/([^\w\d\-\/])(app|enthr|riv|sign|tot)al(e[dr]|ing)/g, '$1$2all$3'); 
  txt=txt.replace(/(\s[\w]*)ll(ful|ment)/g, '$1l$2');

//to add
// protestors

//reconvert special cases
  txt=txt.replace(/(nobelpr|pics)ise/g, '$1ize');  

// unprotectwords()
box.value = txt;
}
function OxEnglish() {
  unprotectwords();
  protectwords();
  Simple();
  re_zwords();
  unprotectwords();
  insert_EngvarOx()
  edit_summary();
}
function OxPassive() {
  unprotectwords();
  protectwords();
  Simple();
//  re_zwords();
  unprotectwords();
  insert_EngvarB()
  edit_summary();
}
function BritishEnglish() {
  unprotectwords();
  protectwords();
  Simple();
  zwords();
  unprotectwords();
  insert_EngvarB()
  edit_summary();
}
function Simpleplus() {
  unprotectwords();
  Simple();
  zwords();
  unprotectwords();
  insert_EngvarB()
  edit_summary();
}
function zwords(){
 var box=document.editform.wpTextbox1;
 var txt = box.value;

txt=txt.replace(/([^\w\d\-][a-z]{3,12}[^s ])iz(e[drs]?|ation|ing)(\W)/g, '$1is$2$3');
txt=txt.replace(/([^\w\d\-][\w]{3,12}[^s ])iz(e[drs]?|ations|ing)(\W)/gi, '$1is$2$3');  //also capitalised
txt=txt.replace(/([^\w\d\-\/])(empha)siz(e|ing)/g, '$1$2sis$3'); //rem synthesiser conversion - equally common 
box.value = txt;
}

function re_zwords(){
//converts s-words into Oxford z-words
 var txt=document.editform.wpTextbox1;
 var txt = box.value;
 
txt=txt.replace(/([^\w\d\-][a-z]{5,12})is(ations?)(\W)/g, '$1iz$2$3');
txt=txt.replace(/([^\w\d\-][a-z]{0,12}(?:[aeiou][^aeiou]|al))is(e[drs]?|ing)(\W)/g, '$1iz$2$3')
txt=txt.replace(/(empha|synthe)sis(e|ing)/g, '$1siz$2'); 
box.value = txt;
 
}

function unprotectwords(){
 var box=document.editform.wpTextbox1;
 var txt = box.value;

 txt=txt.replace(/(\w)xXx(\w)/g, '$1$2');
 box.value = txt;
}

function insert_EngvarB(){
 var box=document.editform.wpTextbox1;
 var txt = box.value;
// Add a template to the article identifying English variant
  txt=txt.replace(/{{(EngvarOx)}}/gi, '{{EngvarB}}');
if (txt.search(/{{EngvarB}}/gi) == -1)
{
box.value='{{EngvarB}}\r\n'+txt;
}
}

function insert_EngvarOx(){
 var box=document.editform.wpTextbox1;
 var txt = box.value;
// Add a template to the article identifying English variant
if (txt.search(/{{EngvarOx}}/gi) == -1)
{
box.value='{{EngvarOx}}\r\n'+txt;
}
}

function edit_summary(){
// Add a tag to the summary box
 var form=document.editform.wpSummary;
 var txt = form.value;

    var summary = "rendering [[WP:ENGVAR|consistent British spelling]] by [[User:Ohconfucius/EngvarB|script]]";
	if (txt.indexOf(summary) == -1) {
		if (txt.match(/[^\*\/\s][^\/\s]?\s*$/)) {
			txt += " | ";
		}
		txt += summary;
	}
//   document.forms.editform.wpMinoredit.checked = true;
   form.value=txt;
//   document.editform.wpDiff.click();
}
addOnloadHook(function () {
 if(document.forms.editform) {

   addPortletLink('p-tb', 'javascript:BritishEnglish()', 'BRITISH English', 'British spelling ', 'converts to Brit English', '', '');
   addPortletLink('p-tb', 'javascript:OxEnglish()', 'OXFORD English', 'OED spelling ', 'converts to Oxford English', '', '');
   addPortletLink('p-tb', 'javascript:OxPassive()', 'OXFORD (passive)', 'British spelling ', 'converts to wishy-washy English', '', '');
   addPortletLink('p-tb', 'javascript:protectwords()', 'Protect Words', 't-mdy', 'month day', '', '');
//  addPortletLink('p-tb', 'javascript:Simpleplus()', 'Speedy convert to British', 'British spelling ', 'converts everything to Brit English', '', '');
  }
});