User:Waldyrious/formatcitations.js: Difference between revisions

Content deleted Content added
support pt ("citar xx") templates
m 1997kB moved page User:Waldir/formatcitations.js to User:Waldyrious/formatcitations.js: Automatically moved page while renaming the user "Waldir" to "Waldyrious"
 
(6 intermediate revisions by 2 users not shown)
Line 3:
// Fill an array with one entry per each recognized citation template
// Note: the regex should be changed to allow parameter values that are themselves templates,
var originalTemplates = txt.value.match(/\{\{[Cc]it(ation|(ar|e) [a-z ]+) *\n? *\|[^}]+\}\}/g);
// 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
var originalTemplates = txt.value.match(/\{\{[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();
Line 10 ⟶ 13:
if(vertical) {
// Fill an array with one entry per each parameter for this citation template
var originalParams = originalTemplates[i].match(/ *\n? *\| *\n? *[a-z1-9^ =]+ *= */g);
// Create array to be filled with the cleaned-up parameters.
// We need to keep the original strings for the replacement step.
Line 18 ⟶ 21:
for(var j = 0; j < originalParams.length; j++){
// Get rid of the delimiters and spaces, keep only the parameter string
tweakedParams[j] = originalParams[j].match(/[a-z1-99_-]+/)[0];
// Calculate the length of the longest parameter
maxWidth = (tweakedParams[j].length>maxWidth) ? tweakedParams[j].length : maxWidth;
Line 40 ⟶ 43:
tweakedTemplates[i] = tweakedTemplates[i].replace(/\n/g, "");
// Normalize spaces around the pipes and equal signs
tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\| *([a-z1-99_-]+) *= */g," |$1 = ");
// Remove potencial extra spaces before template ends
tweakedTemplates[i] = tweakedTemplates[i].replace(/ *\}\}$/," }}");
Line 49 ⟶ 52:
// Update the edit summary
var sum = document.editform.wpSummary;
var summary = vertical ? "Convertconvert citation templates to vertical format, for readability" : "Harmonizeharmonize whitespace in citation 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;
Line 62 ⟶ 65:
$(function () {
if(document.forms.editform) {
mw.loader.using( ['mediawiki.util'], function() {
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 vertically', '-', '');
});
}
});