This is an old revision of this page, as edited by Waldyrious(talk | contribs) at 15:48, 26 October 2011(support templates using uppercase in the first letter). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.Revision as of 15:48, 26 October 2011 by Waldyrious(talk | contribs)(support templates using uppercase in the first letter)
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;originalTemplates=txt.value.match(/\{\{[Cc]it(ation|e [a-z]+) *\n? *\|[^}]+\}\}/g);tweakedTemplates=originalTemplates.slice();for(iinoriginalTemplates){if(vertical){originalParams=originalTemplates[i].match(/ *\n? *\| *\n? *[a-z1-9]+ *= */g);tweakedParams=[];// Clean up the parameters and calculate the length of the longest onemaxWidth=0;for(jinoriginalParams){tweakedParams[j]=originalParams[j].match(/[a-z1-9]+/)[0]// remove the delimiters and keep only the parameter stringmaxWidth=(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(kinoriginalParams){numSpaces=maxWidth-tweakedParams[k].length;alignedParam="\n | "+tweakedParams[k]+Array(numSpaces).join(" ")+" = ";tweakedTemplates[i]=tweakedTemplates[i].replace(originalParams[k],alignedParam);// Replace the original parameters with the tweakes ones }// 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 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":"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','-','');}});