User:Passawuth/Scripts/Cleanup/monobook.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
// ========== Cleaning up article ==========
//<!-- nocleanup -->
// adapted and edited from [[:th:WP:US]] & [[:th:User:Jutiphan/Scripts/mainTabs/monobook.js]] -- use for speedy cleanup 
 
function doCleanupTag(){text=document.editform.wpTextbox1.value;if(text.indexOf("nocleanup")>=0){alert("This page does not expected you to cleanup because it may cause some problems");return}
 
//Format
  text = text.replace(/<br>|<br\/>/gi, "<br />");                                   //<br/> -> <br />
  text = text.replace(/([^\[]|^)\[([^\[^\]]*?)\]\]/gm, "$1[[$2]]");                   //[Link]] -> [[Link]]
  text = text.replace(/\[\[([^\[^\]].*?)[$[^\]]?]/gm, "[[$1]]");                      //[[Link] -> [[Link]]
  text = text.replace(/\[\[ ?([^\]\|]*?) \s*([\|\s|\]\]])/g, "[[$1$2");               //[ Link ]] -> [[Link]]
 
  text = text.replace(/^([\*#]+) /gm, "$1");
  text = text.replace(/^([\*#]+)/gm, "$1 ");
  text = text.replace(/^= ?([^=].*?) ?=/gm,"== $1 ==");                               //= Topic =  -> == Topic ==
  text = text.replace(/^(={1,5}) *(.*?) ?(={1,5}) *$/gm, "$1 $2 $3");                   //==Topic== -> == Topic ==
  text = text.replace(/&prime;/g, "'");
  text = text.replace(/&amp;/g, "&");
  text = text.replace(/&minus;/g, "−");
  text = text.replace(/&times;/g, "×");
  text = text.replace(/&mdash;/g, " —");
  text = text.replace(/&ndash;/g, " -");
 
  //[ [Category: xxx]] -> [ [Category:xxx]]
  text = text.replace(/\[\[Category:\s(.*?)\]\]/g,"[\[Category:$1]]");
 
  //{{Template:xxx}} -> {{xxx}}
  text = text.replace(/{{Template:(.*?)}}/g,"{{$1}}");
 
  //Fix spacing, </xxx > -> </xxx>
  text = text.replace(/<\/(.*?) ?>/g,"</$1>");
 
  //Fix spacing between char and paren
  text = text.replace(/(.[^\s\[\(]\_)\(/g,"$1 (");
  text = text.replace(/\)([^\s\]\)\|\.\_])/g,") $1");
 
  //Fix lang icons: Move from front to back
  text = text.replace(/\* ?({{.*?icon}}) ?(.*?)\r?\n/g,"* $2 $1\n");                            //Edit * {{...}} [...] -> * [...] {{...}}
  text = text.replace(/<ref(.*?)> ?({{.*?icon}}) ?(.*?) ?<\/ref>/g,"<ref$1>$3 $2</ref>");
 
  //Fix Template Parameters Layout: Move | from back to front (using Top to Bottom approach)
  text = text.replace(/ *\|(?!-) *\r?\n *([^=\*<|{}]*?) ?=(?!=) *([^\|={}]*?)/gm,"\n| $1 = $2");      
 
  //TODO: Need comments for code below for maintenance reasons: Hard to debug
  text = text.replace(/\n *\|(?!-) *([^={}\*].*?) ?= *([^<={}]*?) \| ?( *}} *\r?\n| *\r?\n *}} *\r?\n)/g, "\n| $1 = $2\n}}\n");
 
  //Fix Template Parameters Layout: Add extra space in betweens
  text = text.replace(/\n *\|(?!-) *([^=\|'"{}]*?) ?= *([^=]*?) *\r/g,"\n| $1 = $2");
 
  //Fix Year Formatting
  text = text.replace(/(A\.D\.|AD\.)(\d{1,4})/g,"A.D. $2");
 
  //Topic -- bold -- fixing
  text = text.replace(/\n(={1,5}) ?''' ?(.*) ?''' ?(={1,5})/gm, "\n$1 $2 $3"); //== '''header''' == -> == header ==
 
  //Fix Template: Remove extra | if exist at the end 
  text = text.replace(/\n *\|(?!-) *([^=\|'"{}]*?)=([^=\|]*?) ?\| ?\r?\n?\}\}/g, "\n| $1 = $2\n}}"); //| abc = 123 | }} -> | abc = 123 }} 
 
 
  //Spellings -- Needs to add more words
  text = text.replace(/seperate/g,"separate");
  text = text.replace(/recieve/g,"receive");
  text = text.replace(/alphabets/g,"alphabet");
  text = text.replace(/cardboards/g,"cardboard");
  text = text.replace(/treatries/g,"treaties");
  text = text.replace(/Window/g,"Windows");
  text = text.replace(/learnt/g,"learned");
  text = text.replace(/dreamt/g,"dreamed");
  text = text.replace(/USA/g,"United States");
  text = text.replace(/harry/g,"Harry");
 
  if(wgNamespaceNumber == 0 && wgPageName.indexOf("Wikipedia:") != 0) {
    //Remove signatures on article pages
    text = text.replace(/-{0,2} ?\[\[User:.*/g,"");
 
    //Remove begin line spaces to help newcomers
    text = text.replace(/== ?\n *(.*\n)/gi, "==\n$1");
  }
 
  text = text.replace(/(== ?) ?(Website|Websites|Other websites|External link) *==/g,'$1External links ==');
  text = text.replace(/(== ?) ?(Sources|Reference|references|reference|Source) *==/g,'$1References ==');
 
  //Category Check only for article, wikipedia, category and ignore check if editing section of article
  if((wgNamespaceNumber == 0 || wgNamespaceNumber == 4 || wgNamespaceNumber == 14) && document.URL.indexOf("&section=") == -1 && text.indexOf("#REDIRECT") == -1 && text.indexOf("# REDIRECT") == -1 && text.indexOf("{{Cleanup}}") == -1 && text.indexOf("{{Uncategorized}}") == -1 && text.indexOf("[[Category:") == -1 && text.indexOf("{{Disambiguation") == -1) {
    text = "{{Uncategorized}}\n\n" + text;
}
 
document.editform.wpTextbox1.value=text;document.editform.wpMinoredit.checked=true;document.editform.wpSummary.value="Edit + Cleaning up"}function addCleanupTag(){addTab("javascript:doCleanupTag() ","♦ Clean","ca-taclt","Cleanup ","")}if(document.title.indexOf("Editing ")==0){if(window.addEventListener)window.addEventListener("load",addCleanupTag,false);else if(window.attachEvent)window.attachEvent("onload",addCleanupTag)}
 
 
//*******************************
//** CREATE FOR FASTER COMMAND **
//**                           **
//**  Db, Last Diff & Purge    **
//**                           **
//**  IE & Firefox support     **
//*******************************
 
 
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('12(\'11:10 Z Y/X/W V\');b H(){6 c="F://E.D.C/w/B.A?4="+z()+"&G=U&T=S";5.y=c}b e(){6 a=5.x(\'p-v\').u(\'t\')[0];6 l=s(a,"r:H() ",\'R\',\'\');l.n.4="Q P G"}3(5.4.9("m ")==-1){3(k.9("j")==-1){3(2.8)2.8("i",e,h);g 3(2.7)2.7("f",e)}}b q(){6 c="F://E.D.C/w/B.A?4="+z()+"&O=o";5.y=c}b d(){6 a=5.x(\'p-v\').u(\'t\')[0];6 l=s(a,"r:q() ",\'o\',\'\');l.n.4="N M L K J I"}3(5.4.9("m ")==-1){3(k.9("j")==-1){3(2.8)2.8("i",d,h);g 3(2.7)2.7("f",d)}}',62,65,'||window|if|title|document|var|attachEvent|addEventListener|indexOf|tabs|function|nurl|addPurge|addLastDiff|onload|else|false|load|Special|wgCanonicalNamespace||Editing|lastChild|purge||doPurge|javascript|addlilink|ul|getElementsByTagName|cactions||getElementById|location|getPname|php|index|org|wikipedia|en|http|diff|doLastDiff|page|this|of|cache|the|Purge|action|recent|Most|last|prev|oldid|cur|db|Easy|Scripts|scripts|User|WikiProject|Wikipedia|importScript'.split('|'),0,{}))