Jump to content

User:Frietjes/unnest.js

From Wikipedia, the free encyclopedia
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($) {

var myContent = document.getElementsByName('wpTextbox1')[0];
// -------------------------------------------------------------------------------- //
var mysummary = "Unnest templates using [[:en:User:Frietjes/unnest.js|unnest]]";
if(typeof unnesteditsummary == 'string') {mysummary = unnesteditsummary;}
var linktext = "Unnest";
// -------------------------------------------------------------------------------- //

if(mw.config.get('wgNamespaceNumber') != -1 && myContent && (mw.config.get('wgUserName') == 'Frietjes')) {
	mw.loader.using(['mediawiki.util']).done( function() {
	  var portletlink = mw.util.addPortletLink('p-tb', '#', linktext, 't-unnest');
	  $(portletlink).click(function(e) {
	    e.preventDefault();
	    wpUnnestParameters(0);
	  });
	});
}
// -------------------------------------------------------------------------------- //
function wpUnnestParameters(debugflag)
{
	var s = myContent.value;
	var lt = '';
	while (lt != s) {
		lt = s;
		s = s.replace(/(\{\{\{[^{}\|<>]+\|)[\t\r\n ]*(\{\{\{[^{}\|]*\|?[\t\r\n ]*\}\}\}|\{\{[^{}]*\}\}|[^{}\|][^{}\|]*)[\t\r\n ]*(\}\}\})/g, '{'+'{if empty|$1$3|$2}'+'}');
		s = s.replace(/\{\{[\t \r\n]*#if:[\t \r\n]*(\{\{\{[^{}\|<>]+[^{}\|<>\t\r\n])[\t \r\n]*\|[\t\r\n ]*(\}\}\})\|[\t\r\n ]*\1\|?\2[\t\r\n ]*\|(\{\{\{[^{}\|]*\|?[\t\r\n ]*\}\}\}|\{\{[^{}]*\}\}|[^{}\|]*)[\t\r\n ]*\}\}/g, '{'+'{if empty|$1|$2|$3}'+'}');
		s = s.replace(/(\{\{if empty\|[\t ]*\{\{\{[^{}]*\}\}\}[\t ]*\|[\t ]*)\{\{if empty\|((?:\{\{\{[^{}\|]*\|?[\t\r\n ]*\}\}\}|\{\{[^{}]*\}\}|[^{}])*)\}\}/g, '$1$2');
		s = s.replace(/(\{\{\{[^{}\|<>]+\|)[\t\r\n ]*\{\{if empty\|((?:\{\{\{[^{}\|]*\|?[\t\r\n ]*\}\}\}|\{\{[^{}]*\}\}|[^{}])*)\}\}[\t\r\n ]*(\}\}\})/g, '{' + '{if empty|$1$3|$2}'+'}');
		s = s.replace(/(\{\{#if:(?:\{\{\{[^{}\|]*\|?[\t\r\n ]*\}\}\}|\{\{[^i][^{}]*\}\}|[^{}\|])*)(\{\{if empty\|)(\{\{\{[^{}]*\|\}\}\})/g, '$1$3$2');
		s = s.replace(/(\{\{if empty[\t ]*\|)[\t ]*\|/g, '$1');
		s = s.replace(/\{\{if empty[\|\t ]*\}\}/g, '');
	}
	if (s != myContent.value) {
		myContent.value = s;
		var editsummary = document.getElementsByName('wpSummary')[0];
		editsummary.value = mysummary;
		document.forms.editform.wpDiff.click();
	}
}
// -------------------------------------------------------------------------------- //

});