User:Date delinker/EngvarB.js

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

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。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.
*************/
mw.loader.load('https://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(mw.config.get('wgServer') + mw.config.get('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(){
	Ohc_ENGVARprotectall()
	var table = [
		'(al)(uminum)',
		'(ar)(tifact)',
		'(ana|cata|hydro|para)(lyz)',
		'(ly)(zing)',
		'(di)(arrhea)',
		'(de|of)(fense)',
		'(pr)(actic[ei])',
		'(fu)(ror)',
		'(gr)(ay)',
		'(je)(welry)',
		'(ma)(neuver)', 
		'(pe)(diatric)',
		'(pe)(dophil)',
		'(sk)(eptic)',
		'(in)(quiry)',
		'(va)(por)',
		'(on)(stage)',
		'(break|drop|clean|line|lock|pick)(out|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)(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?|ably|ing)',
		'(iz)(e[drs]?|ations?|ably|ing)', //repeating to catch second instances within protected strings (quotations in particular)
		'(bev|jew|lev|mod|pan|rev|trav)(el\\w)',
		'([a-z]{2,8}\\wel)(e[dr]|est|ing)',
		'(counci|dia)(le[dr]|ling|lor)',
		'(app|enthr|riv|sign|tot)(ale[dr]|aling)',
	];

	for (i=0; i<table.length; i++) {
		var expr = [
			['(<.+?>[^<]+?)',				'([^<]+?<\\/.+?>)'],
			['(\\[Category:[^\\]]*?)',			'([^\\]]*?\\])'],
			['(\\[(?:[Ff]ile|[Ii]mage):[^\\.]{0,20})',		'([^\\.]{0,20}\\.)'],
			['(\\[[^\\|\\]]*?)',			'([^\\|\\]]*?\\|)'],
			['({[^{]{0,6}(?:[Qq]uot[^\\|]{1,7}\\s?|[Qq])\\|[^}]*?)', '([^}]*?})'], 
		];
		for (j=0; j<expr.length; j++) {
			var re = new RegExp(expr[j][0] + table[i] + expr[j][1], 'g')
			regex(re, '$1$2♫$3$4')
		}
	}
}
function Ohc_ENGVARprotectall(){
	//protect from function re_z-words
	regex(/(enterpri|promi|surpri)(se[drs]?|ing)/g, '$1♫$2'); 
	regex(/(nobelpri|imagesi|picsi)(ze)/g, '$1♫$2');  
//	regex(/(foo)(bar)/g, '$1♫$2');
//	regex(/(foo)(bar)/g, '$1♫$2');
}

function Simple(){
 var box=document.editform.wpTextbox1;
 var txt = box.value;
//protectwords()

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

//composite words
//	txt=txt.replace(/([ \|\[])onstage/g, '$1on-stage');
	txt=txt.replace(/([ \|\[])(break|drop|clean|line|lock|pick)(out|up)/g, '$1$2-$3');


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

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

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

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


//'~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(/([ \|\[])(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(/([ \|\[])(centi|kilo|milli|-|\d\s)meter/g, '$1$2metre');
	txt=txt.replace(/([ \|\[])(centi|milli|deci|pico|hecto|\b)liter(s?\b)/g, '$1$2litre$3');
	txt=txt.replace(/([ \|\[])(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?\b)/g, '$1$2logue$3');
//	txt=txt.replace(/([ \|\[])(\W[a-z]{3,12}[^s\b])iz(e[drs]?|ations?|ing)/g, '$1$2is$3');
//	txt=txt.replace(/([ \|\[])(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(/([ \|\[])(pan)el(ist)/g, '$1$2ell$3'); 
	txt=txt.replace(/([ \|\[])(bev|jew|lev|mod|rev|trav)el(e[dr]|ing)/g, '$1$2ell$3'); 
	txt=txt.replace(/([ \|\[])([a-z]{2,8}[^egl ])el(e[dr]|est|ing)(\b)/g, '$1$2ell$3$4');
	txt=txt.replace(/([ \|\[])(counci|dia)l(e[dr]|ing|ors?)(\b)/gi, '$1$2ll$3$4');
	txt=txt.replace(/([ \|\[])(app|enthr|riv|sign|tot)al(e[dr]|ing)/g, '$1$2all$3'); 
	txt=txt.replace(/(\s[\w]*)ll(ful|ment)/g, '$1l$2');

	txt=txt.replace(/(movie theat(er|re))/g, 'cinema'); 
	txt=txt.replace(/(airplane|aeroplane)/g, 'aircraft'); 
//to add
// protestors

//reconvert special cases

// 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|ably|ing)(\W)/g, '$1is$2$3');
//txt=txt.replace(/([^\w\d\-][\w]{3,12}[^s ])iz(e[drs]?|ations|ably|ing)(\W)/gi, '$1is$2$3');  //also capitalised
txt=txt.replace(/([ \|\[])(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)?♫(\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();
}
$(function () {
 if(document.forms.editform) {

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