User:Argenti Aertheri/AutoEdSupplement.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Argenti Aertheri/AutoEdSupplement. |
//<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>