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.
// [[User:Sam Sailor/Scripts/Tidy citations.js]]// is a modified version of [[User:Zyxw/Tidy citations.js]] ([[Special:PermaLink/696805321]])// which in turn is a mod of [[User:Meteor sandwich yum/Tidy citations.js]]// which per [[User:Meteor sandwich yum/Tidy citations]]// is a modified version of [[User:Waldir/formatcitations.js]].// 2018-07-05: Vertical alignment will have no spaces preceding the pipe on a new line, i.e. a citation will be vertically aligned like an infobox.// Tidy citations should not be run on its own per WP:COSMETICBOT and WP:MEATBOTif(autosummary==null)varautosummary=true;//generate a short summaryif(showdiff==null)varshowdiff=true;//show diff after pressing buttonif(markminor==null)varmarkminor=false;//mark as minorfunctiontidy(str){vartxt=document.editform.wpTextbox1;// Fill an array with one entry per each recognized citation templatevaroriginalTemplates=txt.value.match(/\{\{[Cc]it(ation|e [A-Za-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++){// Fill an array with one entry per each parameter for this citation templatevaroriginalParams=originalTemplates[i].match(/ *\n? *\| *\n? *([a-zA-Z1-9-_]+|[a-zA-Z1-9-_]+ [a-zA-Z1-9-_]+) *= */g);// Create array to be filled with the cleaned-up parameters.// We need to keep the original strings for the replacement step.vartweakedParams=[];if(str=="horizontal"||str=="crammed"||str=="roomy"){// Remove newlinestweakedTemplates[i]=tweakedTemplates[i].replace(/\n/g,"");// Normalize spaces around the pipes and equal signsswitch(str){case"horizontal":tweakedTemplates[i]=tweakedTemplates[i].replace(/ *\| *([a-zA-Z1-9-_]+|[a-zA-Z1-9-_]+ [a-zA-Z1-9-_]+) *= */g," |$1=");break;case"crammed":tweakedTemplates[i]=tweakedTemplates[i].replace(/ *\| *([a-zA-Z1-9-_]+) *= */g,"|$1=");break;case"roomy":tweakedTemplates[i]=tweakedTemplates[i].replace(/ *\| *([a-zA-Z1-9-_]+) *= */g," | $1 = ");break;}// Remove potential extra spaces before template endstweakedTemplates[i]=tweakedTemplates[i].replace(/ *\}\}$/,"}}");txt.value=txt.value.replace(originalTemplates[i],tweakedTemplates[i]);}elseif(str=="vertical"){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-zA-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 tweakes 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]);}if(autosummary)edit_summary();if(showdiff)diff();if(markminor)document.editform.wpMinoredit.checked=true;}}functionedit_summary(){varsum=document.editform.wpSummary;varsummary=";";summary+=" [[User:Sam Sailor/Scripts/Tidy citations.js|tidy citations]]";if(sum.value.indexOf(summary)==-1){if(sum.value.match(/[^\*\/\s][^\/\s]?\s*$/)){sum.value+="";}sum.value+=summary;}}functiondiff(){document.editform.wpDiff.click();}$(function(){if(document.forms.editform){mw.util.addPortletLink('p-tb','javascript:tidy("horizontal")','\{\{Tidy\}\}','ca-formatcitations','Formats citations: tidy whitespace');mw.util.addPortletLink('p-tb','javascript:tidy("crammed")','\{\{Crammed\}\}','ca-formatcitations-crammed','Formats citations without any whitespace whatsoever');mw.util.addPortletLink('p-tb','javascript:tidy("roomy")','\{\{Roomy\}\}','ca-formatcitations-roomy','Formats citations with more whitespace');mw.util.addPortletLink('p-tb','javascript:tidy("vertical")','\{\{Tidy\}\} (vertically)','ca-formatcitations-vertical','Formats citations vertically & tidy whitespace');}});/*</source>[[Category:Wikipedia scripts]]*/