User:Salix alba/SubSup.js

/*************

Generates a skeleton template data for a given template

Author User:Salix alba Date: 28 July 2013 Version: 0.20

Heavily borrowed from TemplateDataEditor http://fr.wikipedia.org/w/index.php?title=Utilisateur:Ltrlg/scripts/TemplateDataEditor.js

***************/

function SubSup($) {

////// Customization for local wikis

lang = mw.config.get('wgUserLanguage'); messages = { "en": { "toolbox-label": 'SubSup', "toolbox-tooltip": 'Find missmatched sub and sup tags', "title": 'SubSup ', 'section-description': 'SubSup', "close": 'Close' }	};   if(messages[lang]==null) lang='en';

// gets the basetemplate name, on en strips the /doc off the end

////// Global variables

var $title, $body, $cont; var pagename; var patSubO = /]*>/g; var patSubC = /<\/sub>/g; var patSupO = /]*>/g; var patSupC = /<\/sup>/g;

////// Called when toolbox link clicked

function openSubSup { pagename = mw.config.get('wgPageName');

var URL = mw.util.wikiScript; $.ajax({			url: URL,			data: { title: pagename, action: "raw" },			dataType: "text"		}).done(doneAjax); }

/////// Called when raw page code read in

function doneAjax(data) { var array = data.split('\n'); var code = ""; for(var i = 0; i < array.length; ++i) { var line = array[i]; var r1 = line.match(patSubO); var r2 = line.match(patSubC); var r3 = line.match(patSupO); var r4 = line.match(patSupC); var c1 = (r1 == null) ? 0 : r1.length; var c2 = (r2 == null) ? 0 : r2.length; var c3 = (r3 == null) ? 0 : r3.length; var c4 = (r4 == null) ? 0 : r4.length; if(c1 != c2 || c3 != c4) { code += "Line " + i + " counts " + c1 + " " + c2 + " " + c3 + " " + c4 + "\n"; code += line; code += '\n'; }		}		$cont.fadeIn('slow');

$title.text( messages[lang]['title'] + pagename ); $body.html( $(' ')				.attr('id', 'td-sk-code')				.text(code+'\n')		); }

///// Close the dialog function close { $cont.fadeOut('slow', function{		}); }

////////// Building called after page loads function buildHTML { $body = $(' ') .attr('id', 'td-sk-body'); $title = $(' ').text( messages[lang]['title'] ); $cont = $(' ') .attr('id', 'subsup-cont') .append($(' ')				.attr('id', 'td-sk-dialog')				.append( $title )				.append($('') .attr({							id: 'td-sk-close',							href: '#',							title: messages[lang]['close']						}) .click(function{							close;							return false;						}) .append($(' ')								.attr({ alt: messages[lang]['close'], src: '//upload.wikimedia.org/wikipedia/commons/thumb/8/8d/VisualEditor_-_Icon_-_Close.svg/24px-VisualEditor_-_Icon_-_Close.svg.png' })						)				)				.append($body)		) .hide;

$(document.body).append($cont); }

////// Adds a link in the toolbox function addPortletLink {

$(				mw.util.addPortletLink('p-tb', '#', messages['en']['toolbox-label'], 'td-skel', messages['en']['toolbox-tooltip'] )		).click(function{			openSubSup;			return false;		}) }

/////// Actions to do once page loads, function start { //alert("TDSkel 0.21" + mw.config.get( 'wgPageName' )); addPortletLink; buildHTML; }

////////// START ////////// start; }

/////// Wrapper code, only run if in user or template namespaces if( $.inArray( mw.config.get('wgNamespaceNumber'), [ 0 ] ) !== -1 /* && $.inArray( mw.config.get('wgAction'), [ 'edit', 'submit' ] ) !== -1 */ ) { mw.loader.load(  	  '//en.wikipedia.org/w/index.php?title=User:Salix alba/TDSkell.css&action=raw&ctype=text/css&smaxage=21600&maxage=86400',	'text/css' ); mw.loader.using('mediawiki.util', function{	 $(document).ready(SubSup);        }); }