Jump to content

User:Waldyrious/formatcitations.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Waldyrious (talk | contribs) at 19:55, 21 October 2011 (forgot a small detail that was tweaked during testing). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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 formatCitations(vertical) {
    var txt = document.editform.wpTextbox1;
 
    originalCiteTemplates = txt.value.match(/\{\{cit(ation|e [a-z]+) *\n? *\|[^}]+\}\}/g);
    tweakedCiteTemplates  = originalCiteTemplates.slice();

    for(i in originalCiteTemplates) {
        if(vertical) {
            originalParams = originalCiteTemplates[i].match(/ *\n? *\| *\n? *[a-z1-9]+ *= */g);
            tweakedParams = [];

            // Clean up the parameters and calculate the length of the longest one
            maxWidth = 0;
            for(j in originalParams){
                tweakedParams[j] = originalParams[j].match(/[a-z1-9]+/)[0] // remove the delimiters and keep only the parameter string
                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(k in originalParams) {
                numSpaces = maxWidth - tweakedParams[k].length;
                alignedParam = "\n  | " + tweakedParams[k] + Array(numSpaces).join(" ") + " = ";
                tweakedCiteTemplates[i] = tweakedCiteTemplates[i].replace (originalParams[k], alignedParam); // Replace the original parameters with the tweakes ones 
            }

            // Replace the original templates with the tweaked versions
            txt.value = txt.value.replace(originalCiteTemplates[i], tweakedCiteTemplates[i].replace(/\}\}/,"\n}}")); // The replace() adds a newline to finish the alignment
        } else {
            txt.value = txt.value.replace(originalCiteTemplates[i],
                                          tweakedCiteTemplates[i].replace(/\n/g, "")           // Remove newlines
                                                                 .replace(/ *\| */g, " |")     // Normalize spaces before the pipes...
                                                                 .replace(/ *= */g,  " = "));  // ...and after the equal signs
        }
    }

    // Update the edit summary
    var sum = document.editform.wpSummary;
    var summary = vertical ? "Convert citation templates to vertical format, for readability" : "Standardize whitespace in citation templates" ;
    summary += " (using [[User:Waldir/formatcitations.js|Regex citation formatter]])";
    if (sum.value.indexOf(summary) == -1) {
        if (sum.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
            sum.value += " | ";
        }
        sum.value += summary;
    }
    if(!vertical) document.editform.wpMinoredit.checked = true;
}
 
addOnloadHook(function () {
    if(document.forms.editform) {
        addPortletLink('p-cactions', 'javascript:formatCitations(false)', '{{}}', 'ca-formatcitations', 'Format citations: add whitespace', '-', '');
        addPortletLink('p-cactions', 'javascript:formatCitations(true)', '{{}}+', 'ca-formatcitations-vertical', 'Formats citations vertically', '-', '');
    }
});