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 13:40, 24 October 2011 (fix another bug: only normalize whitespace around equal signs that are actually from a parameter (not from, e.g., an url)). 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;
 
    originalTemplates = txt.value.match(/\{\{cit(ation|e [a-z]+) *\n? *\|[^}]+\}\}/g);
    tweakedTemplates  = originalTemplates.slice();

    for(i in originalTemplates) {
        if(vertical) {
            originalParams = originalTemplates[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(" ") + " = ";
                tweakedTemplates[i] = tweakedTemplates[i].replace (originalParams[k], alignedParam); // Replace the original parameters with the tweakes ones 
            }

            // 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]); 
        } else {
            // Remove newlines
            tweakedTemplates[i] = tweakedTemplates[i].replace(/\n/g, "")
            // Normalize spaces around the pipes and equal signs
            tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\| *([a-z1-9]+) *= */g," |$1 = ")
            // Remove potencial spaces before template ends
            tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\}\}$/," }}")
            txt.value = txt.value.replace(originalTemplates[i], tweakedTemplates[i]);                                      
        }
    }

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