User:Omegatron/monobook.js/reffixer.js

function fixreftags { var txt = document.editform.wpTextbox1;

// Full ref:			([^<>]*?)<\/ref> // Self-closed:			 // ref-like tags:		\{\{(fact|citation needed|cn|specify)([^}]*?)\}\} // End-of-phrase punct: 	[,.;'")?]       // Also beginning of phrase:    ['"]	// There must be a way to reuse each of these in every situation, but I don't know the code.

// Remove spaces before ref tag txt.value = txt.value.replace(/ +([^<>]*?)<\/ref> *\1/ig, '$1$3 ');   txt.value = txt.value.replace(/([,.;)?]) * *\1/ig, '$1'); txt.value = txt.value.replace(/([,.;)?]) *\{\{(fact|citation needed|cn|specify)([^}]*?)\}\} *\1/ig, '$1\{\{$2$3\}\}');

// Quotation marks txt.value = txt.value.replace(/(['"]) *([^<>]*?)<\/ref>\1/ig, '$1$3 ');   txt.value = txt.value.replace(/(['"]) *\1/ig, '$1'); txt.value = txt.value.replace(/(['"]) *\{\{(fact|citation needed|cn|specify)([^}]*?)\}\}\1/ig, '$1\{\{$2$3\}\}');   // If just one punct after ref, move before    txt.value = txt.value.replace(/([^<>]*?)<\/ref> *([,.;)?])/ig, '$3$2 ');    txt.value = txt.value.replace(/ *([,.;)?])/ig, '$2');    txt.value = txt.value.replace(/\{\{(fact|citation needed|cn|specify)([^}]*?)\}\} *([,.;)?])/ig, '$3\{\{$1$2\}\}');

// Quotation marks txt.value = txt.value.replace(/([^<>]*?)<\/ref>(['"])/ig, '$3$2 ');   txt.value = txt.value.replace(/<ref([^\/]*?)\/>(['"])/ig, '$2<ref$1/>'); txt.value = txt.value.replace(/\{\{(fact|citation needed|cn|specify)([^}]*?)\}\}(['"])/ig, '$3\{\{$1$2\}\}');

// Should eventually handle things like " .", ". .", ". .", ". . ."

// Currently works for these cases, but only if performed multiple times. Should have it loop and stop when nothing has changed? // Add a tag to the summary box var txt = document.editform.wpSummary; var summary = "Fix reference formatting (with regular expression script)"; if (txt.value.indexOf(summary) == -1) { if (txt.value.match(/[^\*\/\s][^\/\s]?\s*$/)) { txt.value += " | "; }		txt.value += summary; }

// Press the diff button to check it   document.editform.wpDiff.click }

$(function {    if(document.forms.editform) {        mw.util.addPortletLink('p-cactions', 'javascript:fixreftags', '¹', 'ca-reffixer', 'Fixes reference tag formatting', , );    } });