Jump to content
Wikipedia The Free Encyclopedia

User:Mesidast/Tidy citations.js

From Wikipedia, the free encyclopedia
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.
This user script seems to have a documentation page at User:Mesidast/Tidy citations.
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.
 // <nowiki>
 // Forked from [[User:Meteor sandwich yum/Tidy citations.js]] by [[User:Meteor sandwich yum]]
 // Forked by [[User:Mesidast]]. For full list of changes see [[User:Mesidast/Tidy citations]]
 // Version 2.3.2

 // Retrieve and Set Global variables
 if(gTidyCiteEditSum==null)vargTidyCiteEditSum=true;//generate a short summary
 if(gTidyCiteShowDiff==null)vargTidyCiteShowDiff=true;//show diff after
 if(gTidyCiteMarkMinor==null)vargTidyCiteMarkMinor=true;//mark edit as minor
 if(gTidyCiteReplaceParams==null)vargTidyCiteReplaceParams=true;//replace deprecated parameters
 if(gTidyCiteRemoveLive==null)vargTidyCiteRemoveLive=true;//remove url-status=live on unarchived refs
 if(gTidyCiteReplacementParams==null)vargTidyCiteReplacementParams={};//initialise empty object if no user values found

 mw.loader.using("mediawiki.util",function(){
 // Only run script if user is editing an article
 if(!document.forms.editform||(mw.config.get("wgAction")!=="edit"&&mw.config.get("wgAction")!=="submit")){
 return;
 }

 // Deprecated parameters to replace
 vardeprecatedParams={//gTidyCiteReplacementParams
 "accessdate":"access-date",
 "archiveurl":"archive-url",
 "archivedate":"archive-date",
 "urlstatus":"url-status",
 "urlaccess":"url-access",
 "transcripturl":"transcript-url",
 "authorlink":"author-link"
 };
 // If custom user list exists, append to and supersede the default param replace list
 deprecatedParams=Object.assign(deprecatedParams,gTidyCiteReplacementParams);

 functionTidyCitations(mode){
 // WikEd compatibility
 constuseWikEd=window.wikEd&&window.wikEd.useWikEd;
 if(useWikEd){
 window.wikEd.UpdateTextarea();
 }

 // Get and check textbox and summary field
 vartextbox=$("#wpTextbox1");
 varsummary=$("#wpSummary");
 if(!textbox){
 mw.notify("Textbox not found | Tidy Citations");
 returnfalse;
 }
 if(!summary){
 mw.notify("Summary box not found | Tidy Citations");
 returnfalse;
 }
 vartxt=textbox.val();
 constoriginal=txt;
 varhyphenToggle=false;
 varstatusToggle=false;

 constparamRegex=/ *\| *([a-zA-Z1-9-]+) *= */g;
 varcustomRegex;
 if(mode==="custom"){
 customRegex=prompt("Please enter your custom format, using the following examples: ' |1ドル=' or '|1ドル=' or ' | 1ドル = '");
 console.log(customRegex);
 if((customRegex.match(/["']?(\s*\|\s*\1ドル\s*=\s*)["']?/g)||[]).length!==1){
 mw.notify("Invalid custom format entered | Tidy Citations");
 returnfalse;
 }
 customRegex=customRegex.replace(/["']/g,"");
 }

 // Fill an array with one entry per each recognized citation template
 varoldTemplates=txt.match(/{{[_\s]*[Cc]it(?:ation|e[A-Za-z_ ]*)[_\s]*\|(?:{{[^}]*}}|[^}])+}}/g)||[];
 varnewTemplates=oldTemplates.slice();
 //var archiveTemplates = txt.match(/{{[_\s]*[Ww]eb(?:\.|[_ ]*)archive[_\s]*\|(?:{{[^}]*}}|[^}])+}}/g) || []; //TODO : Tie in
 //var allReferences = txt.match(/<ref(?:\s+name\s*=.+?)?\s*>.*?<\/ref\s*>|<ref\s+name\s*=.+?\/>/gsi); // Get full text within ref tags
 // /{{[_\s]*[Ll]ondon[_ ]*[Gg]azette[_\s]*\|	(London Gazette)

 for(vari=0;i<oldTemplates.length;i++){

 // Replace deprecated non-hyphenated parameters
 if(gTidyCiteReplaceParams){
 for(letkeyindeprecatedParams){
 if(deprecatedParams.hasOwnProperty(key)){
 letdeprecatedRegex=newRegExp("\\| *"+key+" *=","gi");
 if(deprecatedRegex.test(newTemplates[i])){
 newTemplates[i]=newTemplates[i].replace(deprecatedRegex,"|"+deprecatedParams[key]+"=");
 hyphenToggle=true;
 }
 }
 }
 }

 //Remove "|url-status=live" if there is no archive link
 if(gTidyCiteRemoveLive&&/\|\s*url-?status\s*=\s*live/.test(newTemplates[i])){//Fix .test
 if(!(/\|\s*archive-?url\s*=\s*http/.test(newTemplates[i]))){
 newTemplates[i]=newTemplates[i].replace(/\|\s*url-?status\s*=\s*live/,"");
 statusToggle=true;
 }
 }

 // standard, crammed, roomy & custom
 if(mode!=="vertical"){
 // Remove newlines
 newTemplates[i]=newTemplates[i].replace(/\n/g,"");

 switch(mode){// Normalize spaces around the pipes and equal signs
 case"standard":
 newTemplates[i]=newTemplates[i].replace(paramRegex," |1ドル=");
 break;
 case"crammed":
 newTemplates[i]=newTemplates[i].replace(paramRegex,"|1ドル=");
 break;
 case"roomy":
 newTemplates[i]=newTemplates[i].replace(paramRegex," | 1ドル = ");
 break;
 case"custom":
 newTemplates[i]=newTemplates[i].replace(paramRegex,customRegex);
 break;
 }
 // Remove potential extra spaces before template ends
 newTemplates[i]=newTemplates[i].replace(/\s*}}$/,"}}");
 txt=txt.replace(oldTemplates[i],newTemplates[i]);
 // vertical
 }else{
 // Fill an array with one entry per each parameter for this citation template
 varoldParams=oldTemplates[i].match(/ *\n? *\| *\n? *([a-zA-Z1-9-]+) *= */g);
 varnewParams=[];
 varmaxWidth=0;
 for(varj=0;j<oldParams.length;j++){
 // Get rid of the delimiters and spaces, keep only the parameter string
 newParams[j]=oldParams[j].match(/[a-zA-Z1-9-]+/)[0];
 // Calculate the length of the longest parameter
 maxWidth=(newParams[j].length>maxWidth)?newParams[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<oldParams.length;k++){
 varnumSpaces=maxWidth-newParams[k].length;
 varalignedParam="\n | "+newParams[k]+newArray(numSpaces).join(" ")+" = ";
 // Replace the original parameters with the tweakes ones
 newTemplates[i]=newTemplates[i].replace(oldParams[k],alignedParam);
 }

 // Also align the }}
 newTemplates[i]=newTemplates[i].replace(/ *\n? *}}/g,"\n}}");
 // Replace the original templates with the tweaked versions
 txt=txt.replace(oldTemplates[i],newTemplates[i]);
 }
 }
 // Only insert the text if something has changed
 if(txt!==original){
 textbox.val(txt);//update text
 if(useWikEd)wikEd.UpdateFrame();//wikEd compatibility
 if(gTidyCiteEditSum)summary.val(EditSummary(summary.val(),hyphenToggle,statusToggle));//edit summary
 if(gTidyCiteMarkMinor)document.editform.wpMinoredit.checked=true;//mark as minor
 if(gTidyCiteShowDiff)$("#wpDiff").click();//show changes
 }else{
 mw.notify("No changes made | Tidy Citations");
 }
 }

 // Generate a random alphanumeric ID of set length
 functionGetUniqueID(length){
 constchars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
 varresult="";
 for(leti=0;i<length;i++){
 result+=chars[Math.floor(Math.random()*chars.length)];
 }
 returnresult;
 }

 // Substitute comments with placeholders
 functionRemoveComments(citeTemplate){
 constcommentText=[];
 constcommentMatches=citeTemplate.match(/<!--.*?-->/gs);//Get all comments
 for(leti=0;i<commentMatches.length;i++){
 varsubText=GetUniqueID(10);
 while(citeTemplate.indexOf(subText)!==-1){
 subText=GetUniqueID(10);
 }
 commentText.push([subText,commentMatches[i]]);
 citeTemplate=citeTemplate.replace(commentMatches[i],subText);
 }
 return{commentText,citeTemplate};
 }
 // a = RemoveComments("Test")
 // b = a.commentText
 // c = a.newTemplates

 // Restore comments from placeholders
 functionRestoreComments(citeTemplate,commentText){
 for(letkeyincommentText){
 if(commentText.hasOwnProperty(key)){
 citeTemplate=citeTemplate.replace(key,commentText[key]);
 }
 }
 returnciteTemplate;
 }

 // Get array of parameters split by "|" and then split by "="
 functionSplitParams(citeTemplate){
 varparamList=citeTemplate
 .split("|")
 .map(element=>element.split(/=(.*)/s)
 .map(element=>element.trim())
 .filter(element=>element!==""));
 returnparamList;
 }

 // Append custom message unto the end of the current edit dummary
 functionEditSummary(sum,hyphenToggle,statusToggle){
 // Define edit summary messages
 constspaceSum="Standardise citation spacing";
 consthyphenSum=" replace deprecated fields";
 conststatusSum=" remove 'url-status=live' from non-archived refs";
 constlinkSum=" using [[User:Mesidast/Tidy citations|a script]]";

 varrepeatSummary=sum.indexOf(linkSum)!==-1;
 varcurrentRegex;
 if(repeatSummary){
 // Merge toggles from previous edit summary
 currentRegex=newRegExp(spaceSum+".*?"+linkSum.replace(/([\[\]\/\|])/g,"\\1ドル"),"gs");
 varcurrentSum=sum.match(currentRegex)||[""];
 hyphenToggle=hyphenToggle||currentSum[0].indexOf(hyphenSum)!==-1;
 statusToggle=statusToggle||currentSum[0].indexOf(statusSum)!==-1;
 }

 // Build edit summary
 varfirstAppend=true;
 varappendSum=linkSum;
 if(statusToggle===true){
 appendSum=" and"+statusSum+appendSum;
 firstAppend=false;
 }
 if(hyphenToggle===true){
 appendSum=(firstAppend?" and":",")+hyphenSum+appendSum;
 firstAppend=false;
 }
 appendSum=spaceSum+appendSum;

 // Merge script summary and existing summary
 if(repeatSummary){
 // Script re-run
 sum=sum.replace(currentRegex,appendSum);
 }else{
 // First time
 if(/[^\*\/\s][^\/\s]?\s*$/.test(sum)){
 sum+=" | "+appendSum;
 }else{
 sum=appendSum;
 }
 }
 returnsum;
 }

 vartidyButton1=mw.util.addPortletLink("p-tb","#","{{Tidy}}","t-tidyCiteDefault","Format citations: tidy whitespace");
 vartidyButton2=mw.util.addPortletLink("p-tb","#","{{Tidy}} (Vertical)","t-tidyCiteVertical","Formats citations & tidy whitespace (vertically)");
 vartidyButton3=mw.util.addPortletLink("p-tb","#","{{Crammed}}","t-tidyCiteCrammed","Formats citations without any whitespace whatsoever");
 vartidyButton4=mw.util.addPortletLink("p-tb","#","{{Roomy}}","t-tidyCiteRoomy","Formats citations with a lot of whitespace");
 vartidyButton5=mw.util.addPortletLink("p-tb","#","{{Custom}}","t-tidyCiteCustom","Formats citations with custom format");
 $(tidyButton1).click(function(){TidyCitations("standard");});
 $(tidyButton2).click(function(){TidyCitations("vertical");});
 $(tidyButton3).click(function(){TidyCitations("crammed");});
 $(tidyButton4).click(function(){TidyCitations("roomy");});
 $(tidyButton5).click(function(){TidyCitations("custom");});

 });
 // </nowiki>

AltStyle によって変換されたページ (->オリジナル) /