User:S.A. Julio/indent.js

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

var myContent = document.getElementsByName('wpTextbox1')[0]; // // var mysummary = "Indent templates using indent"; if(typeof findargdupseditsummary == 'string') {mysummary = findargdupseditsummary;} var linktext = "Indent"; // //

if(mw.config.get('wgNamespaceNumber') != -1 && myContent) { mw.loader.using(['mediawiki.util']).done( function {	 var portletlink = mw.util.addPortletLink('p-tb', '#', linktext, 't-fdup');	  $(portletlink).click(function(e) { e.preventDefault; wpIndentTemplates(0); });	}); } // // function wpIndent(s) {	var alignflag = 0; // Unroll infobox if(s.search(/[Ii]nfobox/g) >=0 ) { s = s.replace(/([^\t \r\n])([\t ]*\|[\t ]*[^=\|\[\]\{\]\t ][^=\|\[\]\{\]]*=)/g, '$1\n$2'); s = s.replace(/(=)([\t ]*\|[\t ]*[^=\|\[\]\{\]\t ][^=\|\[\]\{\]]*=)/g, '$1\n$2'); }	// Add some markers s = s.replace(/[\t ]*[\r\n][\t ]*([\r\n][\t ]*\|[\t ]*[A-Za-z0-9_\-\t ]*=)/g, '\n$1'); s = s.replace(/([0-9])([\t ]*qualifiers?)([\t ]*=)/g, '$1$3'); s = s.replace(/(resting[_ ]*place)([\t _]*coordinates)([\t ]*=)/g, '$1$3'); // uniform pre-pipe indentation var c0 = s.split(/[\r\n]\|/g).length; var c1 = s.split(/[\r\n][\t ]\|/g).length; var c2 = s.split(/[\r\n][\t ][\t ][\t ]*\|/g).length; if (c0 >= c1 && c0 >= c2) { s = s.replace(/([\r\n])[\s]*(\|)/g, '$1$2'); } else if (c1 >= c0 && c1 >= c2 ) { s = s.replace(/([\r\n])[\s]*(\|)/g, '$1 $2'); } else { s = s.replace(/([\r\n])[\s]*(\|)/g, '$1 $2'); }	// uniform post-pipe indentation c0 = s.split(/[\r\n][\t ]*\|[^=\|\[\]\{\]\t ]/g).length; c1 = s.split(/[\r\n][\t ]*\|[\t ][\t ]*[^=\|\[\]\{\]\t ]/g).length; if (c1 >= c0) { s = s.replace(/([\r\n][\t ]*\|)[\t ]*([^=\|\[\]\{\]\t ])/g, '$1 $2'); } else { s = s.replace(/([\r\n][\t ]*\|)[\t ]*([^=\|\[\]\{\]\t ])/g, '$1$2'); }	// flip space and = s = s.replace(/([^=\|\[\]\{\]\t ][\t ]*)(=)([\t ][\t ][\t ]*)/g, '$1$3$2'); if (s.split(/[^=\|\[\]\{\}][\s][\s][\s]*=/g).length > 2) { // Unalign s = s.replace(/[\s]*\|[\s]*[\r\n]([\s]*[^=\|\[\]\{\]]*?=)[\t ]*/g, '\n|$1 '); s = s.replace(/(\|[\s ]*[^=\|\[\]\{\]]*?)[\s]*=[\t ]*/g, '$1 = '); s = s.replace(/[\r\n]([\s]*[^\s\|])/g, '$1'); s = s.replace(/[\s]*GAP MARKER[\s]*[\s]*-->/g, '');

// Realign for(var i=40; i>1; i--) { var r1 = new RegExp('([\\r\\n][\\s]*\\|[^=\\|\\[\\]\\{\\}]{'+(i)+'}=[^\\r\\n]*[\\r\\n][\\s]*\\|[^=\\|\\[\\]\\{\\}]{0,'+(i-1)+'})(=)','g'); while( s.search(r1) > -1) { s = s.replace(r1, '$1 $2'); }			var r2 = new RegExp('([\\r\\n][\\s]*\\|[^=\\|\\[\\]\\{\\}]{0,'+(i-1)+'})(=[^\\r\\n]*[\\r\\n][\\s]*\\|[^=\\|\\[\\]\\{\\}]{'+(i)+'}=)','g'); while( s.search(r2) > -1) { s = s.replace(r2, '$1 $2'); }		}	}	s = s.replace(//g, '\n'); s = s.replace(//g, ''); s = s.replace(/([0-9]|resting[_ ]*place)[\t ]*(=)[\t ]*[\t ]*/g, '$1$3 $2 '); s = s.replace(/([^\|\{\t ])[\t ]*(\|)[\t ]*([\r\n])([^\*:;\{\|])/g, '$1$3$2$4'); return s; } // // function wpIndentTemplates(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 start indenting the list of unnested templates for(i=istart; i < tlist.length; ++i) { tlist[i] = wpIndent(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(/([\r\n]\|[\t ]*\[\[)([^\[\]\|]*)((?:[^{}]|\{\{Winning[^{}]*\}\})*)(\{\{)(College Football[^{}=]*=)[0-9]+/gi, '$1$2$3$4$5$4subst:User:Frietjes/cf|$2}}'); if (mytxt.search(/^[\t ]*(?:\{\{#switch:[\t ]*\{\{\{1\|?\}\}\}[\t \|]*|return[\t ]*\{)[\r\n]/gi) >= 0) { var config = mw.config.get(['wgNamespaceNumber', 'wgTitle', 'wgUserGroups', 'skin']); mytxt = mytxt.replace(/\{\{\{2[\t ]*(?:\|[^{}]*|)\}\}\}/g, '$1'); mytxt = mytxt.replace(/\{\{\{3[\t ]*(?:\|[^{}]*|)\}\}\}/g, '$2'); if (config.wgNamespaceNumber === 10) { mytxt = mytxt.replace(/<\/noinclude>/g, ''); mytxt = mytxt.replace(/[\t ][\t ]*$/g, ''); mytxt = mytxt.replace(/^[\t ]*[\r\n]/g, ''); mytxt = mytxt.replace(/^[\t ]*\{\{#switch:[\t ]*\{\{\{1\|?\}\}\}[\t \|]*[\r\n]/, 'return {\n'); mytxt = mytxt.replace(/[\t ]*\|[\t ]*([a-z0-9]*)[\t ]*=[\t ]*([\-0-9][0-9\.]*)[\t ]*([\r\n])/g, '\t$1 = $2,$3'); mytxt = mytxt.replace(/[\t ]*\|[\t ]*([a-z0-9]*)[\t ]*=[\t ]*([^\r\n]*)/g, "\t$1 = \'$2\',"); mytxt = mytxt.replace(/[\t ]+(\',)/g, '$1'); mytxt = mytxt.replace(/[\t ]*([\r\n])/g, '$1$2'); mytxt = mytxt.replace(/,[\t ]*[\r\n]*[\t ]*\}\}[\t ]*[\r\n]*[\t ]* /gi, 				'\n}\nreplace|\{\{subst:PAGENAME\}\}|Location map |Location map/data/}}'); mytxt = mytxt.replace(/\{\{[Ll]ocation[_ ]*map\/Info[^{}]*\}\}/gi, ''); mytxt = mytxt + '\n* [{' + '{fullurl:Special:Movepage|wpOldTitle=Template:{' + '{PAGENAMEE}' +'}&wpNewTitle=Module:{' + '{#invoke:string|replace|{' + '{PAGENAMEE}' + '}|Location_map_|Location_map/data/}' + '}/doc&wpReason={' + '{urlencode:preserve history for attribution after merge with the module}' + '}' + '}' + '} move]'; mytxt = mytxt + '\n* [{' + '{fullurl:Special:Movepage|wpOldTitle=Template_talk:{' + '{PAGENAMEE}' +'}&wpNewTitle=Module_talk:{' + '{#invoke:string|replace|{' + '{PAGENAMEE}' + '}|Location_map_|Location_map/data/}' + '}&wpReason={' + '{urlencode:preserve talk page history after merge with the module}' + '}' + '}' + '} move talk]'; }		if (config.wgNamespaceNumber === 828) { mytxt = mytxt.replace(/<\/noinclude>/g, '}' + '} '); mytxt = mytxt.replace(/ /g, ' {' + '{#ifeq:{' 				+ '{SUBPAGENAME}' + '}|sandbox||\n\n'); mytxt = mytxt.replace(/^[\t ]*\{\{#switch:[\t ]*\{\{\{1\|?\}\}\}[\t \|]*[\r\n](?:[\r\n]|[^{}])*\}\}/g, 				'\n'				+ '{' + '{Module:Location map/data/doc\n| see also = \n* \n\n}' + '}'); mytxt = mytxt.replace(/\{\{[Ll]ocation[_ ]*map\/Info[^{}]*\}\}[\t ]*[\r\n]*/gi, ''); mytxt = mytxt.replace(/(-->)[\t ]*[\r\n][\t ]*[\r\n]/g, '$1\n'); mytxt = mytxt.replace(/(-->)[\t ]*[\r\n][\t ]*[\r\n]/g, '$1\n'); }	}	mytxt = mytxt.replace(/[\t ]*[\r\n]*[\t ]*[\r\n][\t ]*(\{\{)[\t ]*([Ii]nfobox)/g, '\n$1$2'); myContent.value = mytxt; } // //

});