User:Waldyrious/formatcitations.js: Difference between revisions

Content deleted Content added
´sp
Several fixes, and add documentation
Line 2:
var txt = document.editform.wpTextbox1;
originalCiteTemplates = txt.value.match(/\{\{citecit(ation|e [a-z]+) *\n? *\|[^}]+\}\}/g);
tweakedCiteTemplates = originalCiteTemplates.slice();
 
for(i in originalCiteTemplates) {
if(vertical) {
paramsLongoriginalParams = originalCiteTemplates[i].match(/ *\n? *\| *\n? *[a-zz1-9]+ *= */g);
paramsShorttweakedParams = originalCiteTemplates[i].match(/[a-z]+(?= *=)/g);
 
// Clean up the parameters and calculate the length of the longest one
maxWidth = 0;
for(j in paramsShortoriginalParams){
tweakedParams[j] = originalParams[j].match(/[a-z1-9]+/)[0] // remove the delimiters and keep only the parameter string
maxWidth = (paramsShorttweakedParams[j].length>maxWidth) ? paramsShorttweakedParams[j].length : maxWidth;
}
maxWidth++; // We need an extra one because Array(n).join(' ') will produce a string with n-1 chars
for(k in paramsLong) {
 
numSpaces = maxWidth + 2 - paramsShort[k].length;
// Generate the aligned =versions "|of "the +parameters paramsShort[k](with +padding Array(numSpaces).join("before ")the +equal "=";signs)
for(k in paramsLongoriginalParams) {
tweakedCiteTemplates[i] = tweakedCiteTemplates[i].replace (paramsLong[k], aligned);
numSpaces = maxWidth + 2 - paramsShorttweakedParams[k].length;
alignedParam = "\n | " + tweakedParams[k] + Array(numSpaces).join(" ") + " = ";
tweakedCiteTemplates[i] = tweakedCiteTemplates[i].replace (paramsLongoriginalParams[k], alignedalignedParam); // Replace the original parameters with the tweakes ones
}
 
txt.value = txt.value.replace(originalCiteTemplates[i],
// Replace the original templates with the tweaked versions
tweakedCiteTemplates[i].replace(/\n/g, "")
txt = txt.replace(originalCiteTemplates[i], tweakedCiteTemplates[i].replace(/\}\}/,"\n}}")); // The replace() adds a newline to finish the alignment
.replace(/ *\|/g, " |")
.replace(/= */g, "= ")
.replace(/ \|/g, "\n |")
.replace(/\}\}/, "\n}}"));
} 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
}
}