User:Incnis Mrsi/replace.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Must be loaded after Advisor.js

ct.rules.push(function (s) {
	var re = / - /g; // lame dash
	var a = ct.getAllMatches(re, s);
	for (var i = 0; i < a.length; i++) {
		var m = a[i];
		a[i] = {
				start: m.start,
				end: m.end-1,
				replacement: '\u00A0\u2013', // a non-breaking space and the dash
				name: 'lame dash',
				description: 'Replace “-” with ndash',
				help: ''
		};
	}
	return a;
});

ct.rules.push(function (s) {
	var re = /\[\[Delete key\|DEL\]\]/g;
	var a = ct.getAllMatches(re, s);
	for (var i = 0; i < a.length; i++) {
		var m = a[i];
		a[i] = {
				start: m.start,
				end: m.end,
				replacement: '[[Delete character|DEL]]',
				name: '[DEL]',
				description: 'the correct link for DEL',
				help: ''
		};
	}
	return a;
});

ct.rules.push(function (s) {
	var re = /\[\[broken bar\|\u00A6\]\]/g;
	var a = ct.getAllMatches(re, s);
	for (var i = 0; i < a.length; i++) {
		var m = a[i];
		a[i] = {
				start: m.start,
				end: m.end,
				replacement: '[[\u00A6]]',
				name: '[\u00A6]',
				description: 'simpler link for [\u00A6]',
				help: ''
		};
	}
	return a;
});

ct.rules.push(function (s) {
	var re = /\[\[[Pp]lus and minus signs\|-\]\]/g;
	var a = ct.getAllMatches(re, s);
	for (var i = 0; i < a.length; i++) {
		var m = a[i];
		a[i] = {
				start: m.start,
				end: m.end,
				replacement: '[[Hyphen-minus|-]]',
				name: '[-]',
				description: 'correct link for [-]',
				help: ''
		};
	}
	return a;
});

ct.rules.push(function (s) {
	var re = /''[A-Za-zΑ-Ωα-ω]''(?!')(?![^\u007B]*\u007D\u007D)/g;
	var a = ct.getAllMatches(re, s);
	for (var i = 0; i < a.length; i++) {
		var m = a[i];
		a[i] = {
				start: m.start,
				end: m.end,
				replacement: '{{mvar|'+s.charAt(m.start+2)+'}}',
				name: 'mvar|'+s.charAt(m.start+2),
				description: "replacement of ''q'' with {{mvar|q}}",
				help: ''
		};
	}
	return a;
});

ct.rules.push(function (s) {
	var re = /[0-9]{1,3} [JFMASOND][a-z]{2}/g;
	var a = ct.getAllMatches(re, s);
//	if (a.length == 0) { alert("no dates to NBSPificate!"); }
	var o = [];
	var j = 0;
	var mo;
	for (var i = 0; i < a.length; i++) {
		var m = a[i];
//		alert(s.substring(m.start,m.end));
		if ( (
		      ( m.end - m.start == 5)
		||    ( m.end - m.start == 6) && ( s.substr(m.start,2) <= '31' )
		)
		&& (
		      ( (mo = s.substr(m.end-3,3)) == 'Jan' )
		||    ( mo == 'Feb' )
		||    ( mo == 'Mar' )
		||    ( mo == 'Apr' )
		||    ( mo == 'May' )
		||    ( mo == 'Jun' )
		||    ( mo == 'Jul' )
		||    ( mo == 'Aug' )
		||    ( mo == 'Sep' )
		||    ( mo == 'Oct' )
		||    ( mo == 'Nov' )
		||    ( mo == 'Dec' )
		) ) {
			o[j] = {
				start: m.end-4,
				end: m.end-3,
				replacement: '\u00A0',
				name: s.substring(m.start,m.end),
				description: 'NBSPification of dates',
				help: ''
			};
			j += 1;
		}
	}
	return o;
});
// 31 January

ct.rules.push(function (s) {
	var re = /[JFMASOND][a-z]+ [0-9]{1,3}/g;
	var a = ct.getAllMatches(re, s);
//	if (a.length == 0) { alert("no dates to NBSPificate!"); }
	var o = [];
	var j = 0;
	var mo;
	for (var i = 0; i < a.length; i++) {
		var m = a[i];
//		alert(s.substring(m.start,m.end));
		if (
			( ( s.charAt(m.end-3) < '0' ) || ( s.charAt(m.end-3) > '9' ) )
		&&	( s.substr(m.end-2,2) <= '31' )
		&& (
		      ( (mo = s.substr(m.start,3)) == 'Jan' )
		||    ( mo == 'Feb' )
		||    ( mo == 'Mar' )
		||    ( mo == 'Apr' )
		||    ( mo == 'May' )
		||    ( mo == 'Jun' )
		||    ( mo == 'Jul' )
		||    ( mo == 'Aug' )
		||    ( mo == 'Sep' )
		||    ( mo == 'Oct' )
		||    ( mo == 'Nov' )
		||    ( mo == 'Dec' )
		) ) {
			if (s.charAt(mo = m.end-2) != ' ') { mo -= 1; } 
			o[j] = {
				start: mo,
				end: mo+1,
				replacement: '\u00A0',
				name: s.substring(m.start,m.end),
				description: 'NBSPification of dates',
				help: ''
			};
			j += 1;
		}
	}
	return o;
});
// January 31

ct.rules.push(function (s) {
	var re = /[12][0-9]{3}-[12][0-9]{3}/g;
	var a = ct.getAllMatches(re, s);
	var o = [];
	var j = 0;
	for (var i = 0; i < a.length; i++) {
		var m = a[i];
		if (
			( s.substr(m.start,4) <= '2013' )
		&&	( s.substr(m.start+5,4) <= '2073' )
		) {
			o[j] = {
				start: m.start+4,
				end: m.end-4,
				replacement: '\u2013',
				name: s.substr(m.start,5),
				description: 'year\u2013year',
				help: ''
			}
			j += 1;
		}
	}
	return o;
});
// 1973-2013

// temporary?
ct.rules.push(function (s) {
	var re = /\u007B\u007B[Mm]ath\u007C[^\u007C]+?\u007D\u007D/g;
	var a = ct.getAllMatches(re, s);
	var o = [];
	var j = 0;
	for (var i = 0; i < a.length; i++) {
		var m = a[i];
		if ( 
			( s.substring(m.start+7,m.end-2).search(/=/) >= 0 )
		&&	( s.substr(m.start+7,2) != '1=' )
		) {
			o[j] = {
				start: m.start,
				end: m.start+7,
				replacement: '{{math|1=',
				name: 'math|\u03B6=',
				description: "avoiding problems with “=” in {{math}}",
				help: ''
			}
			j += 1;
		};
	}
	return o;
});