Content deleted Content added
Waldyrious (talk | contribs) also align }} and don't keep adding newlines if there already is one |
m 1997kB moved page User:Waldir/formatcitations.js to User:Waldyrious/formatcitations.js: Automatically moved page while renaming the user "Waldir" to "Waldyrious" |
||
(18 intermediate revisions by 2 users not shown) | |||
Line 2:
var txt = document.editform.wpTextbox1;
// Fill an array with one entry per each recognized citation template
originalCiteTemplates = txt.value.match(/\{\{cit(ation|e [a-z]+) *\n? *\|[^}]+\}\}/g);▼
// Note: the regex should be changed to allow parameter values that are themselves templates,
// e.g. {{cite book | author = John Doe | title = The Book | year = 1234 | ref = {{sfnRef|Doe (1234)}} | isbn = 0-12345-678-9 }}
// I got as far as this: /(\{\{[Cc]it(?:ation|(?:ar|e) [a-z ]+)\s*)(\|\s*[^=]+=\s*(?:\{\{)?.+(?:\}\})?\s*)+(\}\})/g
▲
// Duplicate the array, for editing. We need to keep the original strings for the replacement step
var tweakedTemplates = originalTemplates.slice();
for(var i
if(vertical) {
// Fill an array with one entry per each parameter for this citation template
var originalParams =
tweakedParams = [];▼
// Create array to be filled with the cleaned-up parameters.
// We need to keep the original strings for the replacement step.
▲ var tweakedParams = [];
var maxWidth = 0;
// Clean up the parameters and calculate the length of the longest one▼
// Get rid of the delimiters and spaces, keep only the parameter string
tweakedParams[j] = originalParams[j].match(/[a-z1-
maxWidth = (tweakedParams[j].length>maxWidth) ? tweakedParams[j].length : maxWidth;
}
Line 19 ⟶ 28:
// Generate the aligned versions of the parameters (with padding before the equal signs)
for(var k
var numSpaces = maxWidth - tweakedParams[k].length;
var alignedParam = "\n | " + tweakedParams[k] + new Array(numSpaces).join(" ") + " = ";
tweakedTemplates[i] = tweakedTemplates[i].replace (originalParams[k], alignedParam);
}
// Also align the }}
tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\n? *\}\}/g,"\n }}");
// Replace the original templates with the tweaked versions
txt.value = txt.value.replace(
} else {
// Remove newlines
tweakedTemplates[i] =
tweakedTemplates[i] =
// Remove potencial extra spaces before template ends
tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\}\}$/," }}");
txt.value = txt.value.replace(originalTemplates[i], tweakedTemplates[i]);
}
}
Line 37 ⟶ 52:
// Update the edit summary
var sum = document.editform.wpSummary;
var summary = vertical ? "
summary += " (using [[w:en: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; }
}
if(document.forms.editform) {
mw.loader.using( ['mediawiki.util'], function() {
addPortletLink('p-cactions', 'javascript:formatCitations(false)', '{{}}', 'ca-formatcitations', 'Format citations: add whitespace', '-', '');▼
mw.util.addPortletLink('p-cactions', 'javascript:formatCitations(
▲ mw.util.addPortletLink('p-cactions', 'javascript:formatCitations(
});
}
});
|