User:Davey2010/Scripts/Regex menu framework.js

/************* *** Regex menu framework 1.2 *** by [[m:user:Pathoschild]] *** documentation: http://meta.wikimedia.org/wiki/User:Pathoschild/Script:Regex_menu_f…

/*************
*** Regex menu framework 1.2
*** by [[m:user:Pathoschild]]
*** documentation: http://meta.wikimedia.org/wiki/User:Pathoschild/Script:Regex_menu_framework
*************/
var rmf_globals = {};

if($.inArray( mw.config.get( 'wgAction' ), [ 'edit' , 'submit' ]) > -1) {
	/****************
	*** config tools
	****************/
	// function
	window.regexTool = function (name,functionname) {
		var newline = document.createElement('li');
	
		var newlink = document.createElement('a');
		newlink.setAttribute('href','javascript:'+functionname);
		newlink.setAttribute('title',name);
		newlink.setAttribute('class','sidebar-link');
		newlink.appendChild(document.createTextNode(name));
			
		newline.appendChild(newlink);
		toollist.appendChild(newline);
	};

	/****************
	*** framework tools
	****************/
	/* search only */
	window.regsearch = function (search) {
		return editbox.value.match(search);
	};
	
	/* search & replace */
	window.regex = function (search,replace,repeat) {
		/* define how many times to perform */
		if(!repeat || repeat<0) {
			var repeat = 1;
		}
			/* perform */
		for(var i=0; i<repeat; i++) {
			editbox.value = editbox.value.replace(search,replace);
		}
	};

	/* change reason */
	window.setreason = function(reason,mode) {
		// append once
		if( mode=='appendonce' && !rmf_globals['summary_'+reason] ) {
			rmf_globals['summary_'+reason] = 1;
			if(editreason.value.match(/[^\s]/))
				editreason.value += ', ' + reason;
			else
				editreason.value = reason;
		}
		// append mode
		else if(mode=='append' && editreason.value.match(/[^\s]/)) {
			editreason.value += ', ' + reason;
		}
		// default mode
		else {
			editreason.value = reason;
		}
	};

	/* set edit options */
	window.setoptions = function (minor,watch) {	
		// minor edit?
		if(minor=='true') {
			document.getElementById('wpMinoredit').checked = true;
		}
		else if(minor=='false') {
			document.getElementById('wpMinoredit').checked = false;
		}
		
		// watch page?
		if(watch=='true') {
			document.getElementById('wpWatchthis').checked = true;
		}
		else if(watch=='false') {
			document.getElementById('wpWatchthis').checked = false;
		}
	};

	/* automatically perform an action */
	window.doaction = function (action) {
		if(action=='preview') {
			document.getElementById('wpPreview').click();
		}
		else if(action=='diff') {
			document.getElementById('wpDiff').click();
		}
		else if(action=='save') {
			document.getElementById('wpSave').click();
		}
	};

	/****************
	*** Custom regex form tool
	****************/
	/* create form */
	window.custom = function () {
		/* if already open */
		if(document.getElementById('regexform')) {
			/* display reset warning, unless it's already displayed */
			if(!document.getElementById('resetwarning')) {
				/* ask the user whether he wants to reset the form or cancel */
				// create elements
				resetwarning = document.createElement('div');
				resetwarning.setAttribute('id','resetwarning');
				
				var resetlink = document.createElement('a');
				resetlink.setAttribute('href','javascript:customremove(); custom();');
				resetlink.setAttribute('title','Reset form');
				resetlink.appendChild(document.createTextNode('reset form'));
	
				var cancellink = document.createElement('a');
				cancellink.setAttribute('href','javascript:function customreset() { regexform.removeChild(resetwarning); }; customreset();');
				cancellink.setAttribute('title','Cancel new launch');
				cancellink.appendChild(document.createTextNode('cancel new launch'));
	
				// put message together
				resetwarning.appendChild(document.createTextNode('You are launching the Custom regex tool, but it is already open. Please select an action: '));
				resetwarning.appendChild(resetlink);
				resetwarning.appendChild(document.createTextNode(' or '));
				resetwarning.appendChild(cancellink);
	
				// output
				regexform.insertBefore(resetwarning,regexform.firstChild);
			}
		}
		else {	
			/* container */
			regexform = document.createElement('div');
			regexform.setAttribute('id','regexform');
			editbox.parentNode.insertBefore(regexform,editbox.parentNode.firstChild);
	
			/* header */
			var formheader = document.createElement('h3');
			formheader.appendChild(document.createTextNode('Custom regex form'));
			regexform.appendChild(formheader);
	
			/* instructions */
			var newlink = document.createElement('a');
			newlink.setAttribute('href','javascript:function opennew(url) { window.open(url); }; opennew("http://www.regular-expressions.info/javascript.html");');
			newlink.setAttribute('title','JavaScript regex tutorial');
			newlink.setAttribute('class','external text');
			newlink.appendChild(document.createTextNode('tutorial'));
	
			var newtext = document.createElement('p');
			newtext.appendChild(document.createTextNode('This form allows you to dynamically run any number of regular expression patterns. Enter a search pattern in the search box, in the form "search pattern" or "/search pattern/modifiers" (see '));
			newtext.appendChild(newlink);
			newtext.appendChild(document.createTextNode('), and a replacement text in the replacement box (reference groups use "$1" through "$9").'));
			regexform.appendChild(newtext);

			/* mini tutorial */
			var newtext = document.createElement('p');
			newtext.appendChild(document.createTextNode('Available modifiers (can be combined, like /foo/mig):'));
			regexform.appendChild(newtext);

			var newtext = document.createElement('ul');
			var newitem = document.createElement('li');
			newitem.appendChild(document.createTextNode('multiline (/foo/m): pattern matches per-line, instead of treating the whole document as one line.'));
			regexform.appendChild(newtext);
			newtext.appendChild(newitem);

			var newitem = document.createElement('li');
			newitem.appendChild(document.createTextNode('global (/foo/g): replace all matches, not just the first one.'));
			newtext.appendChild(newitem);

			var newitem = document.createElement('li');
			newitem.appendChild(document.createTextNode('case-insensitive (/foo/i): pattern ignores case, so "/foo/i" matches both "foo" and "FOO".'));
			newtext.appendChild(newitem);
			
			/* form tag */
			var formform = document.createElement('form');
			formform.setAttribute('id','regexformform');
			regexform.appendChild(formform);
	
			// place marker for input boxes
			inputlist = document.createElement('ol');
			formform.appendChild(inputlist);
	
			/* options */
			// list format
			var formoptions = document.createElement('ul');
			formform.appendChild(formoptions);
			
			// go! link
			var container = document.createElement('li');
			var newlink = document.createElement('a');
			newlink.setAttribute('href','javascript:customgo();');
			newlink.setAttribute('title','Perform the above patterns');
			newlink.appendChild(document.createTextNode('Perform the above patterns'));
			newlink.setAttribute('style','font-weight:bold;');
			container.appendChild(newlink);
			formoptions.appendChild(container);
	
			// add new boxes link
			var container = document.createElement('li');
			var newlink = document.createElement('a');
			newlink.setAttribute('href','javascript:customaddnew();');
			newlink.setAttribute('title','Add new search & replace boxes');
			newlink.appendChild(document.createTextNode('Add search & replace boxes'));
			container.appendChild(newlink);
			formoptions.appendChild(container);
	
			// remove form
			var container = document.createElement('li');
			var newlink = document.createElement('a');
			newlink.setAttribute('href','javascript:customremove();');
			newlink.setAttribute('title','Remove this form');
			newlink.appendChild(document.createTextNode('Remove this form'));
			newlink.setAttribute('style','color:red;');
			container.appendChild(newlink);
			formoptions.appendChild(container);
	
			// add first pair of input boxes
			customaddnew();
		}
	};
	
	/* add a new pair of input boxes */
	window.customaddnew = function () {
		/* count for later processing */
		if("patterncount" in window) {
			patterncount++;
		}
		else {
			patterncount = 0;
		}

		/* add new list item */
		var container = document.createElement('li');
		inputlist.appendChild(container);

		/* create search */
		var newinput = document.createElement('textarea');
		newinput.setAttribute('class','formsearch');
		newinput.setAttribute('tabindex',patterncount+100);
		newlabel = document.createElement('label');
		newlabel.setAttribute('for','formsearch');
		newlabel.appendChild(document.createTextNode('Search #'+(patterncount+1)));

		container.appendChild(newlabel);
		container.appendChild(newinput);
		
		/* force newline */
		container.appendChild(document.createElement('br'));

		/* create replace */
		var newinput = document.createElement('textarea');
		newinput.setAttribute('class','formreplace');
		newinput.setAttribute('tabindex',patterncount+101);
		newlabel = document.createElement('label');
		newlabel.setAttribute('for','formreplace');
		newlabel.appendChild(document.createTextNode('Replace #'+(patterncount+1)));
		
		container.appendChild(newlabel);
		container.appendChild(newinput);
	};

	/* run patterns */
	window.customgo = function () {
		/* get search and replace arrays */
		var arrpatterns = regexform.getElementsByTagName('textarea');
		arrsearch = [];
		arrreplace = [];
		var searchcount = -1;
		var replacecount = -1;

		for(i=0; i<arrpatterns.length; i++) {
			/* search boxes */
			if(arrpatterns[i].getAttribute('class') == 'formsearch') {
				searchcount++;
				arrsearch[searchcount] = arrpatterns[i].value;
			}
			/* replace boxes */
			else if (arrpatterns[i].getAttribute('class') == 'formreplace') {
				replacecount++;
				arrreplace[replacecount] = arrpatterns[i].value;
			}
		}

		/* perform patterns */
		for(var i=0; i<arrsearch.length; i++) {
			/* convert input to regex */
			// without delimiters
			if(!arrsearch[i].match(/^\s*\/[\s\S]*\/[a-z]*\s*$/i)) {
				arrsearch[i] = new RegExp(arrsearch[i]);
			}
			// with delimiters
			else {
				// break into parts
				var regpattern = arrsearch[i].replace(/^\s*\/([\s\S]*)\/[a-z]*\s*$/i,'$1');
				var regmodifiers = arrsearch[i].replace(/^\s*\/[\s\S]*\/([a-z]*)\s*$/,'$1');

				// filter invalid flags
				regmodifiers = regmodifiers.replace(/[^gim]/ig,'');

				// convert into array
				arrsearch[i] = new RegExp(regpattern, regmodifiers);
			}

			/* perform */
			editbox.value = editbox.value.replace(arrsearch[i],arrreplace[i]);
		}
	};

	/* remove form */
	function customremove() {
		regexform.parentNode.removeChild(regexform);
		patterncount = -1;
	};

	/****************
	*** create menu
	****************/
	window.rfmenu = function (rfmconfig) {
		/* get elements */
		// note: these must be global.
		editbox = document.getElementById('wpTextbox1');
		editreason = document.getElementById('wpSummary');
		sidebar = document.getElementById('mw-panel') /* vector */
			|| document.getElementById('column-one') /* monobook, myskin, chick & simple */
			|| document.getElementById('mw_portlets'); /* modern */

		if(!sidebar) return;
		/* add CSS */
		mw.util.addCSS('<style type="text/css">'
			+ '#p-regex li { list-style:none !important; }'
			+ '#p-regex a { color:gray !important; }'
			+ '#regexform { margin:1em; padding:0.5em; border:2px solid blue; }'
			+ '#regexform ol { list-style:none; border:1px solid #CCC; padding:0.5em; }'
			+ '#resetwarning { border:1px solid red; padding:3px; }'
			+ '</style>');

		/* create menu */
		var container = document.createElement('div');
		container.setAttribute('class','portlet portal collapsed');
		container.setAttribute('id','p-regex');
		sidebar.appendChild(container);
						
		var header = document.createElement('h3');
		header.appendChild(document.createTextNode('Scripts'));
		container.appendChild(header);
						
		var toolbox = document.createElement('div');
		toolbox.setAttribute('class','p-body pBody body');
		container.appendChild(toolbox);
						
		toollist = document.createElement('ul');
		toolbox.appendChild(toollist);
		

		/* load user-defined sidebar links */
		if(self['gadget_rmflinks']) { gadget_rmflinks(); }
		if(self['rmflinks']) { rmflinks(); }
	};

	/* load script */
	$(rfmenu);
	
	/* mark deprecated */
	(function() {
		var methods = ['regexTool', 'regsearch', 'regex', 'setreason', 'setoptions', 'doaction', 'custom', 'customaddnew', /*'customremove',*/ 'rfmenu'];
		for (var i = 0; i < methods.length; i++) {
			var method = methods[i];
			try {
				mw.log.deprecate(window, method, window[method], 'Use [[m:TemplateScript]] instead.', 'userscript-regexframework-' + method);
			} catch ( e ) {
				// Avoid TypeError: Cannot redefine property: regex
			}
		}
	})();
}


Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.