跳转到内容

User:Date delinker/EngvarB.js

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

这是本页的一个历史版本,由Date delinker留言 | 贡献2010年9月17日 (五) 14:50编辑。这可能和当前版本存在着巨大的差异。

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。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','SetEnglish("B")');
//	regexTool('• BRITISH (Oxford) passive','SetEnglish("P")');
	regexTool('• BRITISH (Oxford)','SetEnglish("Ox")');

	//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(){
	protectall()
	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)',
		'(behavi|col|fl?av|hon|hum|lab)(or)', //repeating to catch second instances within protected strings (quotations in particular)
		'(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?|ing)',
		'(iz)(e[drs]?|ations?|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)',
		'(ll)(ful|ment)',
		'(app|enthr|riv|sign|tot)(ale[dr]|aling)',
	];

	for (i=0; i<table.length; i++) {
		var expr = [
			['(<.+?>[^<]+?)',				'([^<]+?<\\/.+?>)'],
			['(\\[Category:[^\\]]*?)',			'([^\\]]*?\\])'],
			['(\\[(?:file|image):[^\\.]{0,20})',		'([^\\.]{0,20}\\.)'],
			['({[^{]{0,6}(?:quot[^\\|]{1,7}\\s?|q)\\|[^}]*?)', '([^}]*?})'], 
		];
		for (j=0; j<expr.length; j++) {
			try {
				var re = new RegExp(expr[j][0] + table[i] + expr[j][1], 'g')
				regex(re, '$1$2♫$3$4')
			}
			catch(err) {
				txt="There was an error on function protectwords(), when changing '"+ expr[j][0] + table[i] + expr[j][1] + "' => '$1$2♫$3$4'.\n\n";
				txt+="Error: " + err.message + "\n\n";
				txt+="Click OK to continue.\n\n";
				alert(txt);
			}
		}
	}
}
function protectall(){
	//protect from function re_z-words
	regex(/(enterpris|promis|surpris)(e[drs]?|ing)/g, '$1♫$2'); 
	regex(/(nobelpri|imagesi|picsi)(ze)/g, '$1♫$2');  
	regex(/(\\|[\s]*enroll?)(ment[\s]*=)/g, '$1♫$2'); // 'enrollment' parameter within Infobox school

//	regex(/(foo)(bar)/g, '$1♫$2');
}
function Simple(){
	//protectwords()

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

		//composite words
//		'onstage':	'$1on-stage',
		'(break|drop|clean|line|lock|pick)(out|up)':	'$1$2-$3',

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

		//'f' words
		'sulfur':	'$1sulphur',

		// non-redundant e
		'(lik|liv|rat|sal|siz|shak)(able)':	'$1$2e$3',

		//'~our' words
		'(clam|glam|harb|neighb|rum|savi?)or(ed|ful|ing|less|ly|s\b)':	'$1$2our$3',
		'neighborhood':	'$1neighbourhood',
		'behavior(al|s?\\b)':	'$1behaviour$2',
		'(fav|hon)or(abl[ey]|ed|i[\\w]*|s\\b)':	'$1$2our$3',
		'labor(e[dr]|ing|s?\\W)':	'$1labour$2',
		'mold(ed|ing|s?\\W)':	'$1mould$2',

		//'~re~' words
		'(calib|fib|goit|lust|mit|nit|reconnoit|sab|saltpet|spect|theat|tit)er(s?\\W)':	'$1$2re$3',
		'(centi|kilo|milli|-|\\d\\s)meter':	'$1$2metre',
		'(centi|milli|deci|pico|hecto|\b)liter(s?\\b)':	'$1$2litre$3',
		'(dema|peda|mono|syna)gog(s?\\W)':	'$1$2gogue$3',
//		'(\\W[a-z]{3,12}[^s\\b])iz(e[drs]?|ations?|ing)':	'$1$2is$3',
//		'(empha)siz(e|ing)':	'$1$2sis$3', //rem synthesiser conversion - equally common

		//'~l' words where the 'l' doubles as past and present participle
		'(pan)el(ist)':	'$1$2ell$3', 
		'(bev|jew|lev|mod|rev|trav)el(e[dr]|ing)':	'$1$2ell$3', 
		'([a-z]{2,8}[^egl ])el(e[dr]|est|ing)(\\b)':	'$1$2ell$3$4',
		'(app|enthr|riv|sign|tot)al(e[dr]|ing)':	'$1$2all$3',
	}

	for (var word in table) {
		var txt="";
		try {
			var re = new RegExp('([ \\|\\[])' + word, 'g')
			regex(re, table[word])
		}
		catch(err) {
			txt="There was an error on function Simple(), when changing '"+ word + "' => '"+ table[word] +"'.\n\n";
			txt+="Error: " + err.message + "\n\n";
			txt+="Click OK to continue.\n\n";
			alert(txt);
		}
	}

	var table = {
		//'~our' words
		'([ \\[])(flav|hum)or(ed|ful|fully|ings?|less|lessly|s\\W)':	'$1$2our$3',
		'([ \\[])color(ed|ful|fully|ings?|less|lessly)':	'$1colour$2',
		'([ \\[])colors([^\\W\\s]|[\\s]*[^=])':	'$1colours$2',

		//'~re~' words
		'([^\\w\\d\\-\\/=])(calib|fib|goit|lust|mit|nit|reconnoit|sab|saltpet|spect|theat|tit)er(ed|ing)':	'$1$2r$3',
		'( [\\w\\D]*\\-)(col|flav|hum)ored':	'$1$2oured',
		'([^\\w\\d\\-\\/=])(cent|epicent|recent)er(ed|ing)':	'$1$2r$3',
		'([^\\w\\d\\-\\/=])(cent|epicent|recent)ers':	'$1$2res',
		'([^\\w\\d\\-\\/])(ana|cata|dia|epi|homo|mono|pro)log(s?\\b)':	'$1$2logue$3',

		//'~l' words where the 'l' doubles as past and present participle
		'([ \\|\\[])(counci|dia)l(e[dr]|ing|ors?)(\\b)':	'$1$2ll$3$4',
		'(\\s[\\w]*)ll(ful|ment)':	'$1l$2',
		'(movie theat(er|re))':	'cinema',
		'(airplanes?|aeroplanes?)':	'aircraft',
	}

	for (var word in table) {
		var txt="";
		try {
			var re = new RegExp(word, 'g')
			regex(re, table[word])			
		}
		catch(err) {
			txt="There was an error on function Simple(), when changing '"+ word + "' => '"+ table[word] +"'.\n\n";
			txt+="Error: " + err.message + "\n\n";
			txt+="Click OK to continue.\n\n";
			alert(txt);
		}
	}

	//to add
	//protestors
	//reconvert special cases
	//unprotectwords()
}

function SetEnglish(v) {
	unprotectwords();
	protectwords();
	Simple();
	switch (v) {
		case 'Ox':
			re_zwords();
			break;
		case 'B':
			zwords();
			break;
		case 'P':
			alert('Feature not implemented');
			break;
	}
	unprotectwords();
	insert_Engvar(v);
	edit_summary();
}

function Simpleplus() {
	unprotectwords();
	Simple();
	zwords();
	unprotectwords();
	insert_Engvar('B')
	edit_summary();
}

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

function unprotectwords(){
	regex(/(\w)♫(\w)/g, '$1$2');
}

function insert_Engvar(v1){
	// Add a template to the article identifying English variant
	var box=document.editform.wpTextbox1;
	var txt = box.value;

	var v2 = ('Ox' == v1)? 'B' : 'Ox';
	var re1 = new RegExp('{{(Engvar' + v1 + ')}}', 'gi')
	var re2 = new RegExp('{{(Engvar' + v2 + ')}}', 'gi')
	txt=txt.replace(re2, '{{Engvar' + v1 + '}}');
	if (txt.search(re1) == -1) box.value='{{Engvar' + v1 + '}}\r\n'+txt;
}


function edit_summary(){
	//Add a tag to the summary box
	setoptions(minor='true',watch='false');
	setreason('rendering [[WP:ENGVAR|consistent British spelling]] by [[User:Ohconfucius/EngvarB|script]]', 'append');
//	doaction('diff');
}