Jump to content

User:Mesidast/Curly Quotes.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 by [[User:Mesidast]]. For full list of changes see [[User:Mesidast/Curly Quotes]]
// Version 1.1
//
// This script replaces curly quotes (“”‘’) with straight quotes ("') per the guidance at [[MOS:CURLY]].

/**
* TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
* @see https://meta.wikimedia.org/wiki/TemplateScript
* @update-token [[File:Pathoschild/templatescript.js]]
*/

$.ajax("//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js", { dataType:"script", cache:true }).then(function() {
	pathoschild.TemplateScript.add([
		{
			name: "Curly Quotes",
			script: function(editor) {
				var txtBefore = editor.get();
				editor
					.replace(/[“”]/g, '"')
					.replace(/[‘’]/g, "'");
				var filesBefore = txtBefore.match(/\[\[(?:file|image):.+?(?:\||\]\])/gi) || [];
				var filesAfter = editor.get().match(/\[\[(?:file|image):.+?(?:\||\]\])/gi) || [];
				if ((new Set(filesBefore)).size == (new Set(filesAfter)).size) {
					for (var i = 0; i < filesBefore.length; i++) {
						if (filesBefore[i] !== filesAfter[i]) {
							editor.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 (txtBefore !== editor.get()) {
					editor
						.options({ minor: true })
						.appendEditSummary("Replaced curly quotes per [[MOS:CURLY]] using [[User:Mesidast/Curly Quotes|a script]]")
						.clickDiff();
				} else {
					mw.notify("No changes made | Curly Quotes");
				}
			}
		},
	]);
});
// </nowiki>