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>
	Moves refs and citation needed tags after punctuation.
	This is a fork of [[User:Erutuon/scripts/footnoteCleanup.js]]
*/

// TODO: Delete spaces before references.
// Do this before the punctuation is moved to before the 
// footnotes to avoid spaces going before the punctuation.

var cleanUpFootnotes = function ()
{
	var textbox = $("#wpTextbox1");
	
	if ( !textbox )
		return;
	
	const oldContents = textbox.val();
	var contents = oldContents;
	
	var escaped = [];
	var i = 0;
	
	var replacements = [];
	var count = 0;
	
	var escape = function(text, regexString)
	{
		var regex = new RegExp(regexString, "g");
		text = text.replace(
			regex,
			function(match)
			{
				escaped[i] = match;
				var replacement = "%%" + i + "%%";
				i += 1;
				return replacement;
			}
		);
		return text;
	};
	
	var puncRegex = /((?:%%\d+%%)+)([\.\,\;\:\"]{1,3})/g;
	
	var reorder = function(match, capture1, capture2)
	{
		count += 1;
		var replacement = capture2 + capture1;
		replacements.push(replacement);
		return replacement;
	};
	
	var fixPunctuationPlacement = function(text)
	{
		while ( puncRegex.test(text) )
			text = text.replace(
				/((?:%%\d+%%)+)([\.\,\;\:\"]{1,3})/g,
				reorder
			);
		
		return text;
	};
	
	/*	Escape various things:
		ref tags				*/
	contents = escape(
		contents,
		"<ref[^>]*>[^<]+<\\/ref>"
	);
	contents = escape(
		contents,
		"<ref[^\\/]+\\/>"
	);
	
	// citation needed
	contents = escape(
		contents,
		"\\{\\{(?:[Cc]itation needed|[Cc]n|[Ff]act|[Cc]b|[Cc]tn|[Rr]ef\\?)\\|[^\}]+\\}\\}"
	);
	
	contents = fixPunctuationPlacement(contents);
	
	// footnote templates
	/*	Handles up to one level of nested templates.
		Any more, and there may be problems.			*/
	contents = escape(
		contents,
		"\\{\\{(?:sfn|efn|rfn)\\|(?:[^\\}]*?(?:\\{\\{[^\\}]+\\}\\})?)+\\}\\}"
	);
	
	contents = fixPunctuationPlacement(contents);
	
	/*	Unescape the various things escaped above.
		This has to be done twice, since escaping was done twice.	*/
	contents = contents.replace(
		/%%(\d+)%%/g,
		function(wholematch, number) {
			number = Number(number);
			return escaped[number];
		}
	);
	contents = contents.replace(
		/%%(\d+)%%/g,
		function(wholematch, number) {
			number = Number(number);
			return escaped[number];
		}
	);
	
	var isUnchanged = ( oldContents === contents );
	
	if ( isUnchanged )
	{
		mw.notify("No misplaced footnotes or tagging templates were found.");
	}
	
	textbox.val(contents);
};

const namespaceNumber = mw.config.get("wgNamespaceNumber");

// Add button to use script if user is in main, user or draft space.
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
	if ( namespaceNumber === 0 || namespaceNumber === 2 || namespaceNumber === 118 ) {
		pathoschild.TemplateScript.add([
		{
			name: 'RefPunc',
			script: function(editor) {
				cleanUpFootnotes();
				editor
					.options({ minor: true })
					.appendEditSummary("moved punctuation to before footnotes per [[MOS:REFPUNC]], by [[User:Panamitsu/script/refpunc.js|script]]")
					.clickDiff();
			}
		},
	]);
	}
});




// </nowiki>