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.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
functionformatCitations(vertical){vartxt=document.editform.wpTextbox1;// 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,// 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*)+(\}\})/gvaroriginalTemplates=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 stepvartweakedTemplates=originalTemplates.slice();for(vari=0;i<originalTemplates.length;i++){if(vertical){// Fill an array with one entry per each parameter for this citation templatevaroriginalParams=originalTemplates[i].match(/ *\n? *\| *\n? *[^ =]+ *= */g);// Create array to be filled with the cleaned-up parameters.// We need to keep the original strings for the replacement step.vartweakedParams=[];varmaxWidth=0;for(varj=0;j<originalParams.length;j++){// Get rid of the delimiters and spaces, keep only the parameter stringtweakedParams[j]=originalParams[j].match(/[a-z1-9_-]+/)[0];// Calculate the length of the longest parametermaxWidth=(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(vark=0;k<originalParams.length;k++){varnumSpaces=maxWidth-tweakedParams[k].length;varalignedParam="\n | "+tweakedParams[k]+newArray(numSpaces).join(" ")+" = ";// Replace the original parameters with the tweaked onestweakedTemplates[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 versionstxt.value=txt.value.replace(originalTemplates[i],tweakedTemplates[i]);}else{// Remove newlinestweakedTemplates[i]=tweakedTemplates[i].replace(/\n/g,"");// Normalize spaces around the pipes and equal signstweakedTemplates[i]=tweakedTemplates[i].replace(/ *\| *([a-z1-9_-]+) *= */g," |$1 = ");// Remove potencial extra spaces before template endstweakedTemplates[i]=tweakedTemplates[i].replace(/ *\}\}$/," }}");txt.value=txt.value.replace(originalTemplates[i],tweakedTemplates[i]);}}// Update the edit summaryvarsum=document.editform.wpSummary;varsummary=vertical?"convert citation templates to vertical format, for readability":"harmonize 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;}if(!vertical){document.editform.wpMinoredit.checked=true;}}$(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','-','');});}});