Jump to content

User:Frietjes/TT.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
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(/(<includeonly>)(\{\{\{1)(\|autocollapse)(\}\}\})(<\/includeonly>)<noinclude>expanded<\/noinclude>/gi, '$2$1$3$5$4');
    myContent = myContent.replace(/(\{\{\{1)(\|)(<includeonly>)(autocollapse<\/includeonly>)<noinclude>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<!-- TT script mark -->$2');
    // mark other problematic spots
    myContent = myContent.replace(/(\|[ ]*ti)(tle[ ]*=[ ]*\{\{(?:[^{}]|\{\{\{[^{}]*\}\}\})*[\r\n])/g, '$1<!-- TT script mark -->$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>([^{}<>]*)<\/includeonly><noinclude>[^<>]*<\/noinclude>([^{}<>]*)(\}\}\})/g, '$1<includeonly>$2$3$4</includeonly>$5');
        myContent = myContent.replace(/(\|[ ]*collapse_state[ ]*=[ ]*\{\{\{collapse_state)(\|[^{}<>]*)<noinclude>[^<>]*<\/noinclude>([^{}<>]*)(\}\}\})/g, '$1<includeonly>$2$3</includeonly>$4');
     }
    }
    if( myHasNavboxFlag < 2) {
      if( myContent.search(/\{\{\{[ ]*(?:collapse_|)state/g) < 0 ) {
        myContent = myContent.replace(/(\|[ ]*state[ ]*=)[ ]*((?:[^\|\{\}<>\n]|<\/?includeonly>|<\/?noinclude>|\{\{\{[^{}]*\}\}\})*)([\|\n]|<!--[^\n<>]*-->[ ]*[\n])/, '$1 \{\{\{state|$2\}\}\}$3');
        myContent = myContent.replace(/(\|[ ]*state[ ]*=[ ]*\{\{\{state)(\|)<includeonly>([^{}<>]*)<\/includeonly><noinclude>[^<>]*<\/noinclude>([^{}<>]*)(\}\}\})/g, '$1<includeonly>$2$3$4</includeonly>$5');
        myContent = myContent.replace(/(\|[ ]*state[ ]*=[ ]*\{\{\{state)(\|[^{}<>]*)<noinclude>[^<>]*<\/noinclude>([^{}<>]*)(\}\}\})/g, '$1<includeonly>$2$3</includeonly>$4');
      }
    } else {
      myContent = myContent.replace(/(\|[ ]*st)(ate[ ]*=)/g, '$1<!-- TT script mark -->$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", "st<!-- TT script mark -->ate");
    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<!-- TT newline mark -->\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]*<!-- TT newline mark -->[\n]*/g, '');
    myContent = myContent.replace(/<!-- TT script mark -->/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(/<noinclude>/gi).length > 1 ) {       
       myContent = myContent.replace(/(<no)(include>)/i, '$1<!-- TT script mark -->$2');
    }

    // mark places to avoid adding collapsible option (unmark later)
    myContent = myContent.replace(/(<no)(include>[^<>\[\]\n]*<\/no)(include>)/gi, '$1<!-- TT script mark -->$2<!-- TT script mark -->$3');
    if( myContent.search(/(?:<\/?noinclude>|<\/?onlyinclude>)/gi) < 0 ) {
      myContent = myContent + '<noinclude></noinclude>';
      myContent = myContent.replace(/((?:[\t ]|[\n])*)(<noinclude>)/gim, '$2$1');
      myContent = myContent.replace(/<\/no(?:<!-- TT script mark -->|)include><noinclude>/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<!-- TT script add CO here -->');
        myContent = myContent.replace(/(<!-- TT script add CO here -->)((?:[\n]|.)*==[ ]*Usage[ ]*===*[ ]*[\n])/gim, '$2$1');
        myContent = myContent.replace(/([\n])(\[\[Category:)/gi, '$1<!-- {TT script mark} -->$2');
        myContent = myContent.replace(/<!-- {TT script mark} -->(\[\[Category:)/i, '$1');
        myContent = myContent.replace(/([\n])(==)/gi, '$1<!-- {TT script mark} -->$2');
        myContent = myContent.replace(/(<!-- TT script add CO here -->)((?:[\n]|\{\{[^{}]*\}\}|[^{}])*)([\n]\[\[Category:)/gim, '$2$1$3');
        myContent = myContent.replace(/<!-- TT script add CO here -->/gi, '\n{{collapsible option}}\n');
        myContent = myContent.replace(/<!-- {TT script mark} -->/gi, '');
      }
    }
    if( myContent.search(/\{\{[_ ]*(?:[Cc]ollapsible[_ ]+option|[Cc][Oo][Pp])[_ ]*(?:\||\}\})/g) < 0 ) {
      myContent = myContent.replace(/(<[ ]*noinclude[ ]*>)[ ]*\n/i, '$1\n{{collapsible option}}\n');
    }
    if( myContent.search(/\{\{[_ ]*(?:[Cc]ollapsible[_ ]+option|[Cc][Oo][Pp])[_ ]*(?:\||\}\})/g) < 0 ) {
      myContent = myContent.replace(/(<[ ]*noinclude[ ]*>)[ ]*/i, '$1\n{{collapsible option}}\n');
    }
    if( myContent.search(/\{\{\{[ ]*(?:collapse_|)state[ ]*\|[ ]*\{\{\{[ ]*1/g) >= 0 ) {
      myContent = myContent.replace(/(\{\{collapsible option)(\}\})/i, '$1|statename=optional$2');
    }

    myContent = myContent.replace(/<!-- TT script mark -->/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(/<includeonly>/gi) < 0 ) {
    myContent = myContent.replace(/(\|[ ]*state[ ]*=[ ]*\{\{\{(?:collapse_|)state)\|collapsed(\}\}\})/, '$1<includeonly>|collapsed</includeonly>$2');
  }
  myContent = myContent.replace(/(\|[ ]*state[ ]*=[ ]*\{\{\{(?:collapse_|)state)\|<[ ]*includeonly[ ]*>[ ]*collapsed[ ]*<[ ]*\/[ ]*includeonly[ ]*>(\}\}\})/i, '$1<includeonly>|collapsed</includeonly>$2');
  myContent = myContent.replace(/(\{\{\{(?:collapse_|)state)\|<noinclude>uncollapsed<\/noinclude><includeonly>collapsed<\/includeonly>(\}\}\})/gi, '$1<includeonly>|collapsed</includeonly>$2');

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

  // nested includeonly tags
  myContent = myContent.replace(/(<includeonly>(?:[^<>]|[\n])*)<includeonly>((?:[^<>]|[\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 {{documentation}} on this page, you should add the {{collapsible option}} to the /doc page!");
  }

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