Jump to content

User:Argenti Aertheri/AutoEdSupplement.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.
//<nowiki>
// Forked from [[User:GorillaWarfare/script/curlies.js]]
// Forked from [[User:Mesidast/Curly Quotes]]
// Version 1.1
function autoEdCurlyRepair(txt) {
	
	let makeCallback = (w, c) => ($0, $1) => {
			//reps.add(c);
			return $1 + ' ' + w;
		};
		
	var txtBefore = txt.value;
	txt = txt.value;
	
	txt = txt.replace(/[“”]/g, '"');
	txt = txt.replace(/[‘’]/g, "'");
	
	txt = txt.replace(/\b([Hh]e|[Ss]he|[Ii]t|[TWtw]hat|[Tt]here)['’]s(?= (?:(?:not|n?ever|n?either|again|all|almost|also|always|just|often|perhaps|quite|rather|sometimes|soon|still|too|(?!(?:ally|apply|bully|comply|fly|imply|multiply|rally|rely|reply|sully|supply|tally) )[a-z]+ly) )*(?:appeared|become|been|begun|belonged|come|consisted|existed|got|gotten|had|happened|lasted|occurred|remained|seemed)\b)/g, makeCallback('has', 's'));
	txt = txt.replace(/\b([Hh]e|[Ss]he|[Ii]t|[TWtw]hat|[Tt]here)['’]s\b/g, makeCallback('is', 's'));
	txt = txt.replace(/\b([Cc])an['’]t\b/g, '$1annot');
	txt = txt.replace(/\b([Ww])on['’]t\b/g, '$1ill not');
	txt = txt.replace(/\b((?![Aa]i)[A-Z]?[a-z]+)n['’]t\b/g, '$1 not');
	txt = txt.replace(/\b([Ww]e|[Hh]e|[Ss]he|[Ii]t|[Tt]hey|[TWtw]hat|[Tt]here)['’]d(?= (?:(?:not|n?ever|n?either|again|all|almost|also|always|just|often|perhaps|quite|rather|sometimes|soon|still|too|(?!(?:ally|apply|bully|comply|fly|imply|multiply|rally|rely|reply|sully|supply|tally) )[a-z]+ly) )*(?:agreed|arisen|awoken|been|begun|blown|bought|broken|brought|built|caught|chosen|dealt|decreed|disagreed|done|drawn|driven|drunk|eaten|fallen|fed|felt|fled|foreseen|forgiven|forgot|forgotten|fought|found|freed|given|gone|got|gotten|grown|guaranteed|had|heard|held|hidden|hurt|kept|known|learnt|led|left|lost|made|meant|met|misheard|misled|mistaken|misunderstood|overheard|overseen|overtaken|paid|rebuilt|rewritten|ridden|risen|said|sat|seen|sent|shot|shown|shrunk|slept|sold|spent|spoken|stolen|stood|struck|stuck|sung|sworn|taken|taught|thought|thrown|told|torn|undergone|understood|undone|woken|won|worn|written|(?![^aeiou]+ed\b|embed\b)[a-z]+[^e]ed)\b)/g, makeCallback('had', 'd'));
	txt = txt.replace(/\b([Ww]e|[Hh]e|[Ss]he|[Ii]t|[Tt]hey|[TWtw]hat|[Tt]here)['’]d\b/g, makeCallback('would', 'd'));
	txt = txt.replace(/([a-z])['’]re\b/g, '$1 are');
	txt = txt.replace(/([a-z])['’]ve\b/g, '$1 have');
	txt = txt.replace(/([a-z])['’]ll\b/g, '$1 will');
	
	var filesBefore = txtBefore.match(/\[\[(?:file|image):.+?(?:\||\]\])/gi) || [];
	var filesAfter = txt.match(/\[\[(?:file|image):.+?(?:\||\]\])/gi) || [];

	var before = txtBefore.match(/(<blockquote([^</]|<\/)+\>[\s\S]+<\/blockquote>)|(\[{1,2}[^\]]+\]{1,2}|<[^>]+>|\{{1,2}[^\}]+\}{1,2}|"[^"]+")/gim) || [];
	var after = txt.match(/(<blockquote([^</]|<\/)+\>[\s\S]+<\/blockquote>)|(\[{1,2}[^\]]+\]{1,2}|<[^>]+>|\{{1,2}[^\}]+\}{1,2}|"[^"]+")/gim) || [];

	if ((new Set(filesBefore)).size == (new Set(filesAfter)).size) {
		for (var i = 0; i < filesBefore.length; i++) {
			if (filesBefore[i] !== filesAfter[i]) {
				txt = txt.replace(filesAfter[i], filesBefore[i]);
			}
		}
	} else {
		mw.notify("CHECK FILE NAMES BEFORE SUBMITTING this edit. Some file names have been changed and could not be restored as they now match another file name | Curly Quotes");
	}
	
	if ((new Set(before)).size == (new Set(after)).size) {
		for (var z = 0; z < before.length; z++) {
			if (before[z] !== after[z]) {
				txt = txt.replace(after[z], before[z]);
			}
		}
	} else {
		mw.notify("CHECK FILE NAMES BEFORE SUBMITTING this edit. Some file names have been changed and could not be restored as they now match another file name | Curly Quotes");
	}
		
	return txt;
};
//</nowiki>