User:Waldyrious/formatcitations.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Waldyrious/formatcitations. |
function formatCitations(vertical) {
var txt = document.editform.wpTextbox1;
originalCiteTemplates = txt.value.match(/\{\{cit(ation|e [a-z]+) *\n? *\|[^}]+\}\}/g);
tweakedCiteTemplates = originalCiteTemplates.slice();
for(i in originalCiteTemplates) {
if(vertical) {
originalParams = originalCiteTemplates[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(" ") + " = ";
tweakedCiteTemplates[i] = tweakedCiteTemplates[i].replace (originalParams[k], alignedParam); // Replace the original parameters with the tweakes ones
}
// Replace the original templates with the tweaked versions
txt.value = txt.value.replace(originalCiteTemplates[i], tweakedCiteTemplates[i].replace(/\}\}/,"\n}}")); // The replace() adds a newline to finish the alignment
} 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
}
}
// 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', '-', '');
}
});