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.

if (autosummary == null) var autosummary = true; //generate a short summary

if (showdiff == null) var showdiff = false; //show diff after pressing button

if (markminor == null) var markminor = true; //mark as minor



function tidy(str) {

    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 = originalTemplatesi].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(str == "horizontal" || str == "crammed" || str == "roomy") {

            // Remove newlines

            tweakedTemplatesi = tweakedTemplatesi].replace(/\n/g, "");

            // Normalize spaces around the pipes and equal signs

			switch(str) {

				case "horizontal":

					tweakedTemplatesi = tweakedTemplatesi].replace(/ *\| *([a-zA-Z1-9]+|[a-zA-Z1-9]+ [a-zA-Z1-9]+) *= */g," |$1=");

					break;

				case "crammed":

            		tweakedTemplatesi = tweakedTemplatesi].replace(/ *\| *([a-zA-Z1-9]+) *= */g,"|$1=");

            		break;

            	case "roomy":

            		tweakedTemplatesi = tweakedTemplatesi].replace(/ *\| *([a-zA-Z1-9]+) *= */g," | $1 = ");

            		break;

            }

            // Remove potential extra spaces before template ends

            tweakedTemplatesi = tweakedTemplatesi].replace(/ *\}\}$/,"}}");

            txt.value = txt.value.replace(originalTemplatesi], tweakedTemplatesi]);                                      

        }

        else if (str == "vertical") {

			

            var maxWidth = 0;

            for(var j = 0; j < originalParams.length; j++) {

                // Get rid of the delimiters and spaces, keep only the parameter string

                tweakedParamsj = originalParamsj].match(/[a-zA-Z1-9]+/)[0];

                // Calculate the length of the longest parameter

                maxWidth = (tweakedParamsj].length>maxWidth) ? tweakedParamsj].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 - tweakedParamsk].length;

                var alignedParam = "\n  | " + tweakedParamsk + new Array(numSpaces).join(" ") + " = ";

                // Replace the original parameters with the tweakes ones

                tweakedTemplatesi = tweakedTemplatesi].replace (originalParamsk], alignedParam);

       		}

			

            // Also align the }}

            tweakedTemplatesi = tweakedTemplatesi].replace(/ *\n? *\}\}/g,"\n}}"); 

            // Replace the original templates with the tweaked versions

            txt.value = txt.value.replace(originalTemplatesi], tweakedTemplatesi]); 

        }

	

    if(autosummary) edit_summary();

    if(showdiff) 	diff();

    if(markminor)   document.editform.wpMinoredit.checked = true;

    

    }

}



function edit_summary() {

	var sum = document.editform.wpSummary;

	var summary = "Harmonize whitespace in citation templates";

	summary += " (using [[User:Meteor sandwich yum/Tidy citations.js|a script]])";

	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', 'Formats citations & tidy whitespace (vertically)');

        mw.util.addPortletLink('p-tb', 'javascript:tidy("crammed")', '\{\{Crammed\}\}', 'ca-formatcitations-crammed', 'Formats citations without any whitespace whatsoever');

        mw.util.addPortletLink('p-tb', 'javascript:tidy("roomy")', '\{\{Roomy\}\}', 'ca-formatcitations-roomy', 'Formats citations with a lot of whitespace');

    }

});

//for dummy edits add more or less to end of this line