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 20:57, 16 May 2011 (even milder). 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(/\{\{cite [a-z]+ *\|[^}]+\}\}/g);
    tweakedCiteTemplates  = originalCiteTemplates.slice();

    for(i in originalCiteTemplates) {
        if(vertical) {
            paramsLong = originalCiteTemplates[i].match(/\| *[a-z]+ *=/g);
            paramsShort = originalCiteTemplates[i].match(/[a-z]+(?= *=)/g);
            maxWidth = 0;
            for(j in paramsShort){
                maxWidth = (paramsShort[j].length>maxWidth) ? paramsShort[j].length : maxWidth;
            }
            for(k in paramsLong) {
                numSpaces = maxWidth + 2 - paramsShort[k].length;
                aligned = "| " + paramsShort[k] + Array(numSpaces).join(" ") + "=";
                tweakedCiteTemplates[i] = tweakedCiteTemplates[i].replace (paramsLong[k], aligned);
            }
            txt.value = txt.value.replace(originalCiteTemplates[i],
                                          tweakedCiteTemplates[i].replace(/\n/g, "")
                                                                 .replace(/ *\|/g,   " |")
                                                                 .replace(/= */g,    "= ")
                                                                 .replace(/ \|/g,    "\n  |")
                                                                 .replace(/\}\}/,    "\n}}"));
        } else {
            txt.value = txt.value.replace(originalCiteTemplates[i],
                                          tweakedCiteTemplates[i].replace(/\n/g, "")
                                                                 .replace(/ *\| */g, " |")
                                                                 .replace(/ *= */g,  " = "));
        }
    }

    // Update the edit summary
    var sum = document.editform.wpSummary;
    var summary = "[[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;
    }
}
 
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', '-', '');
    }
});