_ _    _ _____  ___   __                       
 __      _(_) | _(_)___ / ( _ ) / /_   ___ ___  _ __ ___  
 \ \ /\ / / | |/ / | |_ \ / _ \| '_ \ / __/ _ \| '_ ` _ \ 
  \ V  V /| |   <| |___) | (_) | (_) | (_| (_) | | | | | |
   \_/\_/ |_|_|\_\_|____/ \___/ \___(_)___\___/|_| |_| |_|

User:Waldyrious/formatcitations.js

In this article, we will explore the fascinating world of User:Waldyrious/formatcitations.js, a topic that has captured the attention of individuals and experts alike. From its beginnings to its current state, User:Waldyrious/formatcitations.js has been the object of study, debate and admiration. Over the years, it has aroused different opinions and emotions, generating endless research and discoveries. Through this article, we will delve into the most relevant aspects of User:Waldyrious/formatcitations.js, offering a detailed and complete vision that allows our readers to better understand this fascinating topic.
function formatCitations(vertical) {
    var txt = document.editform.wpTextbox1;
 
    // Fill an array with one entry per each recognized citation template
    // Note: the regex should be changed to allow parameter values that are themselves templates,
    // e.g. {{cite book | author = John Doe | title = The Book | year = 1234 | ref = {{sfnRef|Doe (1234)}} | isbn = 0-12345-678-9 }}
    // I got as far as this: /(\{\{it(?:ation|(?:ar|e) +)\s*)(\|\s*+=\s*(?:\{\{)?.+(?:\}\})?\s*)+(\}\})/g
    var originalTemplates = txt.value.match(/\{\{it(ation|(ar|e) +) *\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++) {
        if(vertical) {
            // Fill an array with one entry per each parameter for this citation template
            var originalParams = originalTemplates.match(/ *\n? *\| *\n? *+ *= */g);
            // Create array to be filled with the cleaned-up parameters.
            // We need to keep the original strings for the replacement step.
            var tweakedParams = ;

            var maxWidth = 0;
            for(var j = 0; j < originalParams.length; j++){
                // Get rid of the delimiters and spaces, keep only the parameter string
                tweakedParams = originalParams.match(/+/);
                // Calculate the length of the longest parameter
                maxWidth = (tweakedParams.length>maxWidth) ? tweakedParams.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.length;
                var alignedParam = "\n  | " + tweakedParams + new Array(numSpaces).join(" ") + " = ";
                // Replace the original parameters with the tweaked ones
                tweakedTemplates = tweakedTemplates.replace (originalParams, alignedParam);
            }

            // Also align the }}
            tweakedTemplates = tweakedTemplates.replace(/ *\n? *\}\}/g,"\n  }}"); 
            // Replace the original templates with the tweaked versions
            txt.value = txt.value.replace(originalTemplates, tweakedTemplates); 
        } else {
            // Remove newlines
            tweakedTemplates = tweakedTemplates.replace(/\n/g, "");
            // Normalize spaces around the pipes and equal signs
            tweakedTemplates = tweakedTemplates.replace(/ *\| *(+) *= */g," |$1 = ");
            // Remove potencial extra spaces before template ends
            tweakedTemplates = tweakedTemplates.replace(/ *\}\}$/," }}");
            txt.value = txt.value.replace(originalTemplates, tweakedTemplates);                                      
        }
    }

    // Update the edit summary
    var sum = document.editform.wpSummary;
    var summary = vertical ? "convert citation templates to vertical format, for readability" : "harmonize whitespace in citation templates" ;
    summary += " (using ])";
    if (sum.value.indexOf(summary) == -1) {
        if (sum.value.match(/?\s*$/)) {
            sum.value += "; ";
        }
        sum.value += summary;
    }
    if(!vertical) { document.editform.wpMinoredit.checked = true; }
}

$(function () {
    if(document.forms.editform) {
    	mw.loader.using( , function() {
	        mw.util.addPortletLink('p-cactions', 'javascript:formatCitations(false)', '{{}}', 'ca-formatcitations', 'Format citations: add whitespace', '-', '');
	        mw.util.addPortletLink('p-cactions', 'javascript:formatCitations(true)', '{{}}+', 'ca-formatcitations-vertical', 'Formats citations vertically', '-', '');
    	});
    }
});