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.
// TidyCitations should generally not be run on its own,
// without making a more substantive edit,
// per the human-editor instruction in WP:COSMETICBOT.
//
// SMcCandlish modified version of:
// [[User:Sam Sailor/Scripts/Tidy citations.js]]
// a modified version of [[User:Zyxw/Tidy citations.js]] ([[Special:PermaLink/696805321]])
// which per [[User:Meteor sandwich yum/Tidy citations]]
// which is a modified version of [[User:Waldir/formatcitations.js]].
// SMcCandlish changes:
// * Removed crammed and roomy versions, which are shitey layout.
// * Fixed == null comparison to ===
// * Changed vertical parameters from "| param" to " |param" for consistency with (and ease of conversion to) horizontal citations.
// * Removed dead code (redundant one-case switch inside an if...then that already tested for the same condition).
// * Clearer orientation variable name.

if (autosummary === null) var autosummary = true; //generate a short summary
if (showdiff === null) var showdiff = true; //show diff after pressing button
if (markminor === null) var markminor = false; //mark as minor

function tidy(orientation) {
    var txt = document.editform.wpTextbox1;
    
    // Fill an array with one entry per each recognized citation template
    var originalTemplates = txt.value.match(/\{\{[Cc]it(ation|e [A-Za-z ]+) *\n? *\|[^}]+\}\}/g);
    // Duplicate the array, for editing. We need to keep the original strings for the replacement step
    var tweakedTemplates  = originalTemplates.slice();
	
    for(var i = 0; i < originalTemplates.length; i++) {
		
		// Fill an array with one entry per each parameter for this citation template
        var originalParams = originalTemplates[i].match(/ *\n? *\| *\n? *([a-zA-Z1-9-_]+|[a-zA-Z1-9-_]+ [a-zA-Z1-9-_]+) *= */g);
        // Create array to be filled with the cleaned-up parameters.
        // We need to keep the original strings for the replacement step.
        var tweakedParams = [];
		
		if(orientation == "horizontal") {
            // Remove newlines
            tweakedTemplates[i] = tweakedTemplates[i].replace(/\n/g, "");
            // Normalize spaces around the pipes and equal signs
        	tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\| *([a-zA-Z1-9-_]+|[a-zA-Z1-9-_]+ [a-zA-Z1-9-_]+) *= */g," |$1=");
            // Remove potential extra spaces before template ends
            tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\}\}$/,"}}");
            txt.value = txt.value.replace(originalTemplates[i], tweakedTemplates[i]);                                      
        }
        else if (orientation == "vertical") {
			
            var maxWidth = 0;
            for(var j = 0; j < originalParams.length; j++) {
                // Get rid of the delimiters and spaces, keep only the parameter string
                tweakedParams[j] = originalParams[j].match(/[a-zA-Z1-9-_]+/)[0];
                // Calculate the length of the longest parameter
                maxWidth = (tweakedParams[j].length>maxWidth) ? tweakedParams[j].length : maxWidth;
            }
            maxWidth++; // We need an extra one because Array(n).join(' ') will produce a string with n-1 chars
			
            // Generate the aligned versions of the parameters (with padding before the equal signs)
            for(var k = 0; k < originalParams.length; k++) {
                var numSpaces = maxWidth - tweakedParams[k].length;
                var alignedParam = "\n |" + tweakedParams[k] + new Array(numSpaces).join(" ") + " = ";
                // Replace the original parameters with the tweakes ones
                tweakedTemplates[i] = tweakedTemplates[i].replace (originalParams[k], alignedParam);
       		}
			
            // Also align the }}
            tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\n? *\}\}/g,"\n}}"); 
            // Replace the original templates with the tweaked versions
            txt.value = txt.value.replace(originalTemplates[i], tweakedTemplates[i]); 
        }
	
    if(autosummary) edit_summary();
    if(showdiff) 	diff();
    if(markminor)   document.editform.wpMinoredit.checked = true;
    
    }
}

function edit_summary() {
	var sum = document.editform.wpSummary;
	var summary = ";";
	summary += " [[User:SMcCandlish/TidyCitations.js|TidyCitations]]";
	if (sum.value.indexOf(summary) == -1) {
    	if (sum.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
            	sum.value += "";
    	}
    			sum.value += summary;
	}	
}

function diff() {
	document.editform.wpDiff.click();
}

$(function () {
    if(document.forms.editform) {
        mw.util.addPortletLink('p-tb', 'javascript:tidy("horizontal")', '\{\{Tidy\}\}', 'ca-formatcitations', 'Format citations: tidy whitespace');
        mw.util.addPortletLink('p-tb', 'javascript:tidy("vertical")', '\{\{Tidy\}\} (vertically)', 'ca-formatcitations-vertical', 'Format citations: vertically, tidy whitespace');
    }
});
/*</source>
 
[[Category:Wikipedia scripts]]
*/