User:Frietjes/thumb-fixer.js

jQuery(document).ready(function($) {

var myContent = document.getElementsByName('wpTextbox1')[0]; // // var mysummary = "Fix infobox image syntax"; if(typeof thumbfixeditsummary == 'string') {mysummary = thumbfixeditsummary;} var mydiffpreview = "preview"; if(typeof thumbfixdiffpreview == 'string') {mydiffpreview = thumbfixdiffpreview;} var linktext = "ThumbFix"; // //

if(mw.config.get('wgNamespaceNumber') != -1 && myContent && (mw.config.get('wgUserName') == 'MB' || mw.config.get('wgUserName') == 'Frietjes')) { mw.loader.using(['mediawiki.util']).done( function {	 var portletlink = mw.util.addPortletLink('p-tb', '#', linktext, 't-fthumb');	  $(portletlink).click(function(e) { e.preventDefault; wpFixThumbs(0); });	}); } // // function wpThumbFix(s) {	var imagecapnames = [ ["(?:image|cover)", "(?:caption|image_caption)", "alt"], ["(?:image_skyline|image_name)", "(?:caption|image_caption)", "image_alt"], ["(?:static_image_name|static_image)", "static_image_caption", "static_image_alt"], ["(?:static_image_2_name|static_image2)", "static_image_2_caption", "static_image_2_alt"], ["logo", "logo_caption", "logo_alt"], ["Map", "map_caption", "map_alt"], ["Image", "Caption", "Alt"] ];	if (s.search(/\{\{[_\s]*(?:[Ii]nfobox|[Ss]peciesbox|[Tt]axobox|[Aa]utomatic[_ ]+[Tt]axobox)/g) >= 0 && s.search(/¶¶/g) < 0) { s = s.replace(/(\|[\s]*(?:image|image_skyline|caption|Image|Caption|image_name|cover|image_caption|static_image|static_image_name|static_image_2|static_image_2_name|static_image_caption|static_image_2_caption|logo|logo_caption|Map|map_caption)[\s]*=)/g, '¶¶$1'); s = s.replace(/(\|[\s]*thumb(?:nail|)[\s]*(?:\|[^\[\]]*|))\|[\s]*(?:x?[0-9]+|[0-9]+x[0-9]+)px[\s]*(\||\]\])/g, '$1$2'); s = s.replace(/(\|[\s]*thumb(?:nail|)[\s]*(?:\|[^\[\]]*|))\|[\s]*(?:[Ll][Ee][Ff][Tt]|[Rr][Ii][Gg][Hh][Tt]|[Nn][Oo][Nn][Ee]|[Cc][Ee][Nn][Tt][Ee][Rr]|[Cc][Ee][Nn][Tt][Rr][Ee])[\s]*(\||\]\])/g, '$1$2'); s = s.replace(/\|[\s]*(?:x?[0-9]+|[0-9]+x[0-9]+)px[\s]*((?:\|[^\[\]]*|)\|[\s]*thumb(?:nail|)[\s]*)(\||\]\])/g, '$1$2'); s = s.replace(/\|[\s]*(?:[Ll][Ee][Ff][Tt]|[Rr][Ii][Gg][Hh][Tt]|[Nn][Oo][Nn][Ee]|[Cc][Ee][Nn][Tt][Ee][Rr]|[Cc][Ee][Nn][Tt][Rr][Ee])[\s]*((?:\|[^\[\]]*|)\|[\s]*thumb(?:nail|)[\s]*)(\||\]\])/g, '$1$2'); s = s.replace(/(\[\image)[_\s]*:[^\|\[\]\{\}]*)(\|(?:[^\[\]\{\}]|\[\[[^\[\*\]\]|\{\{[^\{\}]*\}\})*)(\|[\s]*alt[\s]*=(?:[^\|\[\]\{\}]|\[\^\[\*\]\]|\{\{[^\{\}]*\}\})*?)((?:\||\]\]))/gi, '$1$3$2$4');		s = s.replace(/(\|[\s]*name[\s]*=[\s]*)(.*?)([\s¶]*\|(?:[^\[\]\{\}]|\[\^\[\*\]\]|\{\{[^\{\}]*\}\})*\[\image)[_\s]*:[^\[\]\{\}]*)\|[\s]*\2((?:\||\]\]))/gi, '$1$2$3$4');		var re, i;		var IBI = ['(¶¶\\|[\\s]*','[\\s]*=[\\s]*)'];		var FIL = '\\[\\[[_\\s]*(?:[Ff][Ii][Ll][Ee]|[Ii][Mm][Aa][Gg][Ee])[\\s_]*:[\\s_]*([^\\|\\[\\]\\{\\}]*?)[\\s_]*';		var ALT = '\\|[\\s]*alt[\\s]*=[\\s]*((?:[^\\|\\[\\]\\{\\}]|\\[\\[[^\\[\\*\\]\\]|\\{\\{[^{}]*\\}\\})*?)((?:\\||\\]\\])[^\r\n]*)';		var CAP = '\\|((?:[^\\[\\]\\{\\}\\|]|\\{\\{(?:\\{\\{[^{}]*\\}\\}|[^{}])*\\}\\}|\\^\\[\\*\\]|\\[\\^\\[\\*\\]\\])*)';		var TF = '(?:\\|[\\s]*thumb(?:nail|)[\\s]*|\\|[\\s]*frameless[\\s]*)*';		var END = '[\\s\\|]*\\]\\]([\\s]*(?:¶*\\||\\}\\}))';		var IENDDCAP = ['[\\s\\|]*\\]\\]((?:[\\s]*\\|[^¶]*|[\\s]*)¶¶\\|[\\s]*','[\\s]*=)[\\t ]*([^\\s\\|\\}])'];		var IENDCAP = ['[\\s\\|]*\\]\\]((?:[\\s]*\\|[^¶]*|[\\s]*)¶¶\\|[\\s]*','[\\s]*=)[\\t ]*'];

for (i=0; i<imagecapnames.length; i++) { re = new RegExp(IBI[0] + imagecapnames[i][0] + IBI[1] + FIL + ALT); s = s.replace(re, '$1[[File:$2$4\n| ' + imagecapnames[i][2] + ' = $3');		}		for (i=0; i<imagecapnames.length; i++) {			re = new RegExp(IBI[0] + imagecapnames[i][0] + IBI[1] + FIL + TF + END, 'g');			s = s.replace(re, '$1$2$3');		}		for (i=0; i<imagecapnames.length; i++) {			re = new RegExp(IBI[0] + imagecapnames[i][0] + IBI[1] + FIL + TF + CAP + IENDDCAP[0] + imagecapnames[i][1] + IENDDCAP[1], 'g');			var tmp = s;			s = s.replace(re, '$1$2$4 $3 $5');			if (tmp != s) {				re = new RegExp('(\\|[\\s]*' + imagecapnames[i][1] + '[\\s]*=[\\s]*(.*?))[\\s]* [\\s]*\\2');				s = s.replace(re, '$1');			}		}		for (i=0; i<imagecapnames.length; i++) {			re = new RegExp(IBI[0] + imagecapnames[i][0] + IBI[1] + FIL + TF + CAP + IENDCAP[0] + imagecapnames[i][1] + IENDCAP[1], 'g');			s = s.replace(re, '$1$2$4 $3');		}		for (i=0; i<imagecapnames.length; i++) {			var c = imagecapnames[i][1];			c = c.replace(/^\(\?:([^\|\(\)]*)(?:\|.*|\))/, '$1');			re = new RegExp(IBI[0] + imagecapnames[i][0] + IBI[1] + FIL + TF + CAP + END, 'g');			s = s.replace(re, '$1$2\n| ' + c + ' = $3$4');		}		s = s.replace(/¶¶/g, '');	}		return s; } // // function wpFixThumbs(debugflag) {	// Regular expression which matchs a template arg	var argexp = new RegExp("(\\|[\\s]*[^=\\|\\[\\]\\{\\}]*=[\\s]*)", "gm");	// Internal for and while loop variables	var i=0; var j=0; var loopcount=0;	// Array used to hold the list of unnested templates	var tlist = []; 	// Copy the contents of the text window so we can modify it without problems	var mytxt = myContent.value;

// Remove comments loopcount = 0; while( (mytxt.search(//g) >= 0) && (loopcount < 20) ) { // Split into chunks, isolating the comments var strlist = mytxt.split(//); // Loop through the chunks, removing the comments templates for (i = 0; i < strlist.length; i++) { if( strlist[i].search(/^$/) >= 0 ) { tlist.push(strlist[i]); strlist[i] = '♦§' + (tlist.length - 1) + '§♦'; }		}		// Join the chunks back together for the next iteration mytxt = strlist.join(''); loopcount++; }

var istart = tlist.length; // Now start unnesting the templates loopcount = 0; while( (mytxt.search(/(?:\{\{|\}\})/g) >= 0) && (loopcount < 20) ) { // Split into chunks, isolating the unnested templates var strlist = mytxt.split(/(\{\{[^\{\}]*\}\})/); // Loop through the chunks, removing the unnested templates for (i = 0; i < strlist.length; i++) { if( strlist[i].search(/^\{\{[^\{\}]*\}\}$/) >= 0 ) { tlist.push(strlist[i]); strlist[i] = '♦§' + (tlist.length - 1) + '§♦'; }		}		// Join the chunks back together for the next iteration mytxt = strlist.join(''); loopcount++; }

// Now fix thumbs for(i=istart; i < tlist.length; ++i) { tlist[i] = wpThumbFix(tlist[i]); }	// Reassemble the article while(mytxt.search(/♦§[0-9][0-9]*§♦/g) >= 0) { var n = mytxt.match(/♦§[0-9][0-9]*§♦/)[0].replace(/♦§([0-9]*)§♦/, '$1'); mytxt = mytxt.replace(new RegExp('♦§' + n + '§♦', 'g'), tlist[Number(n)]); }

mytxt = mytxt.replace(/[\t ]*[\r\n]*[\t ]*[\r\n][\t ]*(\{\{)[\t ]*([Ii]nfobox)/g, '\n$1$2'); if(myContent.value != mytxt) { myContent.value = mytxt; var editsummary = document.getElementsByName('wpSummary')[0]; document.forms.editform.wpMinoredit.checked = true; editsummary.value = mysummary; if (mydiffpreview == 'diff') { document.forms.editform.wpDiff.click; }		if (mydiffpreview == 'preview') { document.forms.editform.wpPreview.click; }	} } // //

});