User:Waldyrious/formatcitations.js: Difference between revisions

Content deleted Content added
fix
m 1997kB moved page User:Waldir/formatcitations.js to User:Waldyrious/formatcitations.js: Automatically moved page while renaming the user "Waldir" to "Waldyrious"
 
(26 intermediate revisions by 2 users not shown)
Line 1:
function formatCitations(vertical) {
var txt = document.editform.wpTextbox1;
// Fill an array with one entry per each recognized citation template
originalCiteTemplates = txt.value.match(/\{\{cite [a-z]+ *\|[^}]+\}\}/g);
// Note: the regex should be changed to allow parameter values that are themselves templates,
tweakedCiteTemplates = originalCiteTemplates.slice();
// 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
originalCiteTemplatesvar originalTemplates = txt.value.match(/\{\{cite[Cc]it(ation|(ar|e) [a-z ]+) *\n? *\|[^{}]+\}\}/g);
// Duplicate the array, for editing. We need to keep the original strings for the replacement step
var tweakedTemplates = originalTemplates.slice();
 
for(var i in= originalCiteTemplates0; i < originalTemplates.length; i++) {
if(vertical) {
paramsLong = originalCiteTemplates[i].match(/\| *[a-z]+ *=/g);
// Fill an array with one entry per each parameter for this citation template
paramsShort = originalCiteTemplates[i].match(/[a-z]+(?= *=)/g);
var originalParams = originalTemplates[i].match(/ *\n? *\| *\n? *[^ =]+ *= */g);
maxWidth = 0;
// Create array to be filled with the cleaned-up parameters.
for(j in paramsShort){
// We need to keep the original strings for the replacement step.
maxWidth = (paramsShort[j].length>maxWidth) ? paramsShort[j].length : maxWidth;
} var tweakedParams = [];
 
for(k in paramsLong) {
numSpaces =var maxWidth + 2 -= paramsShort[k].length0;
alignedfor(var j = "|0; "j +< paramsShort[k]originalParams.length; j++ Array(numSpaces).join(" ") + "=";{
// Get rid of the delimiters and spaces, keep only the parameter string
tweakedCiteTemplates[i] = tweakedCiteTemplates[i].replace (paramsLong[k], aligned);
tweakedParams[j] = originalParams[j].match(/[a-z1-9_-]+/)[0];
// Calculate the length of the longest parameter
maxWidth = (paramsShorttweakedParams[j].length>maxWidth) ? paramsShorttweakedParams[j].length : maxWidth;
maxWidth = 0; }
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(var k = 0; k < originalParams.length; k++) {
var numSpaces = maxWidth - tweakedParams[k].length;
var alignedParam = "\n | " + tweakedParams[k] + new Array(numSpaces).join(" ") + " = ";
// Replace the original parameters with the tweaked ones
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(originalCiteTemplatesoriginalTemplates[i], tweakedTemplates[i]);
} else {
// Remove newlines
tweakedTemplates[i] = tweakedTemplates[i].replace(/\n/g, "");
// 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]);
}
txt.value = txt.value.replace(originalCiteTemplates[i],
tweakedCiteTemplates[i].replace(/\n/g, "")
.replace(/ *\|/g, " |")
.replace(/= */g, "= ")
.replace(/ \|/g, "\n |")
.replace(/\}\}/, "\n}}"));
}
 
// Update the edit summary
var sum = document.editform.wpSummary;
var summary = vertical ? "[[Userconvert citation templates to vertical format, for readability" :Waldir/formatcitations.js|Regex "harmonize whitespace in citation formatter]]templates" ;
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; }
}
 
addOnloadHook$(function () {
if(document.forms.editform) {
mw.loader.using( ['mediawiki.util'], function() {
addPortletLink('p-cactions', 'javascript:formatCitations()', '{{}}', 'ca-formatcitations', 'Formats citations for easier editing in code view', '-', '');
mw.util.addPortletLink('p-cactions', 'javascript:formatCitations(false)', '{{}}', 'ca-formatcitations', 'Format citations: add whitespace', '-', '');
mw.util.addPortletLink('p-cactions', 'javascript:formatCitations(true)', '{{}}+', 'ca-formatcitations-vertical', 'Formats citations for easier editing in code viewvertically', '-', '');
});
}
});