User:Omegatron/monobook.js/reffixer.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.
function fixreftags() {
    var txt = document.editform.wpTextbox1;

	// Full ref:			<ref([^\/]*?)>([^<>]*?)<\/ref>
	// Self-closed:			<ref([^\/]*?)\/>
	// 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/ig, '<ref');
    txt.value = txt.value.replace(/ +\{\{(fact|citation needed|cn|specify)([|}])/ig, '\{\{$1$2');
    
    // If punctuation is doubled on both sides, remove later one
    txt.value = txt.value.replace(/([,.;)?]) *<ref([^\/]*?)>([^<>]*?)<\/ref> *\1/ig, '$1<ref$2>$3</ref>');
    txt.value = txt.value.replace(/([,.;)?]) *<ref([^\/]*?)\/> *\1/ig, '$1<ref$2/>');
    txt.value = txt.value.replace(/([,.;)?]) *\{\{(fact|citation needed|cn|specify)([^}]*?)\}\} *\1/ig, '$1\{\{$2$3\}\}');

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

    // Quotation marks
    txt.value = txt.value.replace(/<ref([^\/]*?)>([^<>]*?)<\/ref>(['"])/ig, '$3<ref$1>$2</ref>');
    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 "<ref/>.", ".<ref/>.", ".<ref/><ref/>.", ".<ref/>.<ref/>."

    // 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', '', '');
    }
});