User:Frietjes/TT.js

jQuery(document).ready(function($) { if(mw.config.get('wgNamespaceNumber') != -1) {	mw.loader.using(['mediawiki.util']).done( function { var portletlink = mw.util.addPortletLink('p-tb','#','Collapsible option','t-clps'); $(portletlink).click(function(e) {			e.preventDefault;			collapsibleoptionfunction;		}); }); } // // function collapsibleoptionfunction {  var myContent = document.getElementById('wpTextbox1').value;

// make sure there is a navbox on the page var myHasNavboxFlag = 0; if( myContent.search(/\{\{[_ ]*(?:template[_ ]*:[_ ]*|)(?:navbox|military[_ ]+navigation|campaign)/gi) >= 0 ) { myHasNavboxFlag = 1; } if( myContent.search(/\{\{[_ ]*(?:template[_ ]*:[_ ]*|)(?:Country[_ ]+topics|Region[_ ]+topics|Territory[_ ]+topics|Africa[_ ]+topic|Americas[_ ]+topic|Asia[_ ]+topic|Australia[_ ]+topic|Australia[_ ]+states[_ ]+and[_ ]+territories[_ ]+topic|Central[_ ]+America[_ ]+topic|Dutch[_ ]+municipality|European[_ ]+topic|Europe[_ ]+topic|Germany[_ ]+States[_ ]+topic|Indian[_ ]+State[_ ]+topic|North[_ ]+America[_ ]+topic|Oceania[_ ]+topic|South[_ ]+America[_ ]+topic|United[_ ]+States[_ ]+topic|US[_ ]+states[_ ]+navbox[_ ]+with[_ ]+columns)/gi) >= 0 ) { myHasNavboxFlag = 1; } if( myContent.search(/\{\{[_ ]*(?:template[_ ]*:[_ ]*|)(?:County|Crossings[_ ]+navbox|Football[_ ]+chairperson[_ ]+history|Cycling[_ ]+team|Football[_ ]+manager[_ ]+history|National[_ ]+field[_ ]+hockey[_ ]+squad|National[_ ]+futsal[_ ]+squad|Navbox[_ ]+rugby[_ ]+league[_ ]+squad|Penthouse[_ ]+Pets|SA[_ ]+district[_ ]+navigation[_ ]+box|Season[_ ]+navbox|Shades[_ ]+of[_ ]+color|Year[_ ]+in[_ ]+football|Riksdagsledamöter)/gi) >= 0 ) { myHasNavboxFlag = 1; } if( myContent.search(/\{\{[_ ]*(?:template[_ ]*:[_ ]*|)(?:US[_ ]+state[_ ]+navigation[_ ]+box)/gi) >= 0 ) { myHasNavboxFlag = 2; // use collapse_state or state } if( myContent.search(/\{\{[_ ]*(?:template[_ ]*:[_ ]*|)(?:Mexico[_ ]+state[_ ]+navigation[_ ]+box|US[_ ]+county[_ ]+navigation[_ ]+box|CurrentCongDeleg|USCongDistState|NRHP[_ ]+navigation[_ ]+box)/gi) >= 0 ) { myHasNavboxFlag = 3; // use collapse_state instead of state } if( myHasNavboxFlag > 0) { // simplify autocollapse/expanded myContent = myContent.replace(/(\{\{\{1)(\|autocollapse)(\}\}\})(<\/includeonly>) expanded<\/noinclude>/gi, '$2$1$3$5$4'); myContent = myContent.replace(/(\{\{\{1)(\|)(autocollapse<\/includeonly>) expanded<\/noinclude>(\}\}\})/gi, '$1$3$2$4$5'); // mark state parameters that are not in the navbox (unmarked later) myContent = myContent.replace(/(\{\{[_ ]*(?:template[_ ]*:[_ ]*|)(?:co|collapsible[_ ]+option)[_ ]*\|[^{}]*sta)(te[ ]*=)/gi, '$1$2'); // mark other problematic spots myContent = myContent.replace(/(\|[ ]*ti)(tle[ ]*=[ ]*\{\{(?:[^{}]|\{\{\{[^{}]*\}\}\})*[\r\n])/g, '$1$2'); // add state parameter if it's not there // (1) try adding to | state = or | collapse_state = if( myHasNavboxFlag > 1) { if( myContent.search(/\{\{\{[ ]*(?:collapse_|)state/g) < 0 ) { myContent = myContent.replace(/(\|[ ]*collapse_state[ ]*=)[ ]*((?:[^\|\{\}<>\n]|<\/?includeonly>|<\/?noinclude>|\{\{\{[^{}]*\}\}\})*)([\|\n]|-->[ ]*[\n])/, '$1 \{\{\{collapse_state|$2\}\}\}$3'); myContent = myContent.replace(/(\|[ ]*collapse_state[ ]*=[ ]*\{\{\{collapse_state)(\|) ([^{}<>]*)<\/includeonly> [^<>]*<\/noinclude>([^{}<>]*)(\}\}\})/g, '$1 $2$3$4 $5'); myContent = myContent.replace(/(\|[ ]*collapse_state[ ]*=[ ]*\{\{\{collapse_state)(\|[^{}<>]*) [^<>]*<\/noinclude>([^{}<>]*)(\}\}\})/g, '$1 $2$3 $4'); }   }    if( myHasNavboxFlag < 2) { if( myContent.search(/\{\{\{[ ]*(?:collapse_|)state/g) < 0 ) { myContent = myContent.replace(/(\|[ ]*state[ ]*=)[ ]*((?:[^\|\{\}<>\n]|<\/?includeonly>|<\/?noinclude>|\{\{\{[^{}]*\}\}\})*)([\|\n]|[ ]*[\n])/, '$1 \{\{\{state|$2\}\}\}$3'); myContent = myContent.replace(/(\|[ ]*state[ ]*=[ ]*\{\{\{state)(\|) ([^{}<>]*)<\/includeonly> [^<>]*<\/noinclude>([^{}<>]*)(\}\}\})/g, '$1 $2$3$4 $5'); myContent = myContent.replace(/(\|[ ]*state[ ]*=[ ]*\{\{\{state)(\|[^{}<>]*) [^<>]*<\/noinclude>([^{}<>]*)(\}\}\})/g, '$1 $2$3 $4'); }   } else { myContent = myContent.replace(/(\|[ ]*st)(ate[ ]*=)/g, '$1$2'); }

// (2) loop over field names, attempting to add the | state = after each one var farray = new Array("teamname", "title", "comp ", "comp", "country", "territory", "region", "titlename", "template_name", "name ", "name", "year ", "year", "province_code", "capital", "province", "wikiname", "State", "state"); for( var i=0; i < farray.length; i++ ) { if( myContent.search(/\{\{\{[ ]*(?:collapse_|)state/g) < 0 ) { if( myContent.search(/\|[ ]*(?:collapse_|)state[ ]*=/g) < 0 ) { var myPattern; myPattern = new RegExp("([ ]*\\|[ ]*[\\n]?[ ]*" + farray[i] + "[ ]*=(?:[^\\n\\[\\]\\|\\{\\}]|\\[\\^\\[\\*\\]\\]|\\{\\{[^\\{\\}]*\\}\\})*)(\\|)", "g"); myContent = myContent.replace( myPattern, '$1\n$2'); myPattern = new RegExp("([ ]*\\|[ ]*[\\n]?[ ]*)(" + farray[i] + ")([ ]*=)([^\\n]*[\\n])"); if( myHasNavboxFlag > 1) { myContent = myContent.replace( myPattern, '$1$2$3$4$1collapse_state$3 \{\{\{collapse_state|autocollapse\}\}\}|\n'); } else { myContent = myContent.replace( myPattern, '$1$2$3$4$1state$3 \{\{\{state|autocollapse\}\}\}|\n'); }         myContent = myContent.replace(/(\|[ ]*(?:collapse_|)state[ ]*=[ ]*\{\{\{(?:collapse_|)state\|[^{}]*\}\}\})[ ]*\|[ ]*([\n](?: |[\n])*\|)/, '$1$2'); myContent = myContent.replace(/(\|)[ ]*([\n])[ ]*\|[ ]*[\n]?[ ]*((?:collapse_|)state[ ]*=[ ]*\{\{\{(?:collapse_|)state\|[^{}]*\}\}\})/, '$1$2$3'); }     }    }    myContent = myContent.replace(/[\n]*[\n]*/g, ''); myContent = myContent.replace(//g, ''); if( myHasNavboxFlag > 1) { if( myContent.search(/\{\{\{[ ]*1/g) < 0 ) { myContent = myContent.replace(/(=[ ]*\{\{\{collapse_state\|)([^{}\|]*)(\}\}\})/g, '$1\{\{\{1|$2\}\}\}$3'); myContent = myContent.replace(/(=[ ]*\{\{\{collapse_state\|\{\{\{1\|)autocollapse(\}\}\})/g, '$1$2'); }   }    if( myContent.search(/\{\{\{[ ]*(?:collapse_|)state/g) < 0 ) { alert("Unable to determine where to add the state parameter!"); return; } } else { alert("Found no navbox on this page!"); return; } // add collapsible option if it's not there and the state parameter is there if( myContent.search(/\{\{\{[ ]*(?:collapse_|)state/g) >= 0 ) {

// mark redundant noinclude sections (unmark later) while( myContent.match(/ /gi).length > 1 ) { myContent = myContent.replace(/()/i, '$1$2'); }

// mark places to avoid adding collapsible option (unmark later) myContent = myContent.replace(/([^<>\[\]\n]*<\/no)(include>)/gi, '$1$2$3'); if( myContent.search(/(?:<\/?noinclude>|<\/?onlyinclude>)/gi) < 0 ) { myContent = myContent + ' '; myContent = myContent.replace(/((?:[\t ]|[\n])*)/gim, '$2$1'); myContent = myContent.replace(/<\/no(?:|)include> /gim, ''); }   if( myContent.search(/\{\{[_ ]*(?:[Cc]ollapsible[_ ]+option|[Cc][Oo][Pp])[_ ]*(?:\||\}\})/g) < 0 ) { if( myContent.search(/\{\{[_ ]*(?:template:[_ ]*|)(?:documentation|doc|docs|documentation,[_ ]+template|template[_ ]+doc|template[_ ]+documentation)[_ ]*[\n]?\|[ ]*content[ ]*=/gi) >= 0) { myContent = myContent.replace(/(\{\{[_ ]*(?:template:[_ ]*|)(?:documentation|doc|docs|documentation,[_ ]+template|template[_ ]+doc|template[_ ]+documentation)[_ ]*[\n]?\|[ ]*content[ ]*=[ ]*[\n]?)/i, '$1'); myContent = myContent.replace(/((?:[\n]|.)*==[ ]*Usage[ ]*===*[ ]*[\n])/gim, '$2$1'); myContent = myContent.replace(/([\n])(\[\[Category:)/gi, '$1$2'); myContent = myContent.replace(/(\[\[Category:)/i, '$1'); myContent = myContent.replace(/([\n])(==)/gi, '$1$2'); myContent = myContent.replace(/((?:[\n]|\{\{[^{}]*\}\}|[^{}])*)([\n]\[\[Category:)/gim, '$2$1$3'); myContent = myContent.replace(//gi, '\n\n'); myContent = myContent.replace(//gi, ''); }   }    if( myContent.search(/\{\{[_ ]*(?:[Cc]ollapsible[_ ]+option|[Cc][Oo][Pp])[_ ]*(?:\||\}\})/g) < 0 ) { myContent = myContent.replace(/(<[ ]*noinclude[ ]*>)[ ]*\n/i, '$1\n\n'); }   if( myContent.search(/\{\{[_ ]*(?:[Cc]ollapsible[_ ]+option|[Cc][Oo][Pp])[_ ]*(?:\||\}\})/g) < 0 ) { myContent = myContent.replace(/(<[ ]*noinclude[ ]*>)[ ]*/i, '$1\n\n'); }   if( myContent.search(/\{\{\{[ ]*(?:collapse_|)state[ ]*\|[ ]*\{\{\{[ ]*1/g) >= 0 ) { myContent = myContent.replace(/(\{\{collapsible option)(\}\})/i, '$1|statename=optional$2'); }

myContent = myContent.replace(//g, ''); }

// too much whitespace myContent = myContent.replace(/(\{\{\{[ ]*(?:collapse_|)state\|)[ ]+([^{}]*)[ ]*(\}\}\})/g, '$1$2$3'); myContent = myContent.replace(/(\{\{\{[ ]*(?:collapse_|)state\|)[ ]*([^{}]*[^{} ])[ ]+(\}\}\})/g, '$1$2$3');

// includeonly with collapsed if(myContent.search(/ /gi) < 0 ) { myContent = myContent.replace(/(\|[ ]*state[ ]*=[ ]*\{\{\{(?:collapse_|)state)\|collapsed(\}\}\})/, '$1 |collapsed $2'); } myContent = myContent.replace(/(\|[ ]*state[ ]*=[ ]*\{\{\{(?:collapse_|)state)\|<[ ]*includeonly[ ]*>[ ]*collapsed[ ]*<[ ]*\/[ ]*includeonly[ ]*>(\}\}\})/i, '$1 |collapsed $2'); myContent = myContent.replace(/(\{\{\{(?:collapse_|)state)\| uncollapsed<\/noinclude> collapsed<\/includeonly>(\}\}\})/gi, '$1 |collapsed $2');

// includeonly only half inside state myContent = myContent.replace(/(\{\{\{(?:collapse_|)state[ ]*\|[^\{\}<>]*)(<\/includeonly>)([^\{\}<>]*\}\}\})/, '$1$3$2'); myContent = myContent.replace(/(\{\{\{(?:collapse_|)state[ ]*\|[^\{\}<>]*)([^\{\}<>]*\}\}\})/, '$2$1$3');

// nested includeonly tags myContent = myContent.replace(/( (?:[^<>]|[\n])*) ((?:[^<>]|[\n])*)<\/includeonly>/gim, '$1$2');

if( myContent.search(/\{\{[_ ]*(?:template:[_ ]*|)(?:documentation|doc|docs|documentation,[_ ]+template|template[_ ]+doc|template[_ ]+documentation)[_ ]*\}\}/gi) >= 0 ) { myContent = myContent.replace(/\{\{[_ ]*(?:Template[_ ]*:[_ ]*|)collapsible[_ ]+option[_ ]*(?:\|[ ]*statename[ ]*=[ ]*optional[ ]*|)\}\}[ ]*[\n]?/, ''); alert("Found on this page, you should add the  to the /doc page!"); }

if(document.getElementById('wpTextbox1').value != myContent) { document.getElementById('wpTextbox1').value = myContent; if( myContent.search(/\{\{\{[ ]*state(?: | )*\|/g) >= 0 ) { document.getElementById('wpSummary').value = 'adding \{\{collapsible option\}\} and/or \{\{\{state\}\}\}'; } else { document.getElementById('wpSummary').value = 'adding \{\{collapsible option\}\} and/or \{\{\{collapse_state\}\}\}'; } } } //  // });