User:Waldyrious/formatcitations.js: Difference between revisions

Content deleted Content added
support templates using uppercase in the first letter
add comments and "var"s
Line 2:
var txt = document.editform.wpTextbox1;
// Fill an array with one entry per each recognized citation template
var originalTemplates = txt.value.match(/\{\{[Cc]it(ation|e [a-z]+) *\n? *\|[^}]+\}\}/g);
tweakedTemplates = originalTemplates.slice();
// Duplicate the array, for editing. We need to keep the original strings for the replacement step
var tweakedTemplates = originalTemplates.slice();
 
for(i in originalTemplates) {
if(vertical) {
var originalParams = originalTemplates[i].match(/ *\n? *\| *\n? *[a-z1-9]+ *= */g);
var tweakedParams = [];
 
// Clean up the parameters and calculate the length of the longest one
var maxWidth = 0;
for(j in originalParams){
tweakedParams[j] = originalParams[j].match(/[a-z1-9]+/)[0] // remove the delimiters and keep only the parameter string
Line 34 ⟶ 36:
// Normalize spaces around the pipes and equal signs
tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\| *([a-z1-9]+) *= */g," |$1 = ")
// Remove potencial extra spaces before template ends
tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\}\}$/," }}")
txt.value = txt.value.replace(originalTemplates[i], tweakedTemplates[i]);