User:Zackmann08/scripts/GenerateDeprecated.js

//<nowiki>
jQuery(document).ready(function($) {

// Disable in article namespace as it shouldn't ever be used there
if(mw.config.get('wgNamespaceNumber') != 0 && document.getElementsByName('wpTextbox1')[0]) {
	mw.loader.using(['mediawiki.util']).done( function() {
		var portletlink = mw.util.addPortletLink('p-tb','#','GenDeprecated','t-gentdeprecated');
		$(portletlink).click(function(e) {
			e.preventDefault();
			generateDeprecated();
		});
	});
}
// -------------------------------------------------------------------------------- //
function generateDeprecated()
{
	var mycontent = $('#wpTextbox1').textSelection('getContents');
	// Copy the contents of the text window so we can modify it without problems
	var mytxt = mycontent;
	var original_text = mycontent;
	
	const params_regex = /\{\{#invoke\:Check for deprecated parameters\|check([\s\S]*)\}\}<noinclude>/i;
	var param_text = original_text.match(params_regex);
	
	if (param_text == null){
		alert('Match not found');
		return;
	}
	
	param_text = original_text.match(params_regex)[1];
	
	var final_text = '';
	const remove_regex = /\|\s*_remove\s*=\s*(.*)\n/;
	var removes;
	if (param_text.match(remove_regex)){
		removes = param_text.match(remove_regex)[1].split(";");	
	}
	
	if (removes) {
		final_text = final_text + '{\n\t"replaceText": "(^\\\\s*\\\\|\\\\s*)(PARAM';
		for (const remove of removes) {
			final_text = final_text + '|' + remove.trim();
		}
		final_text = final_text + ')(\\\\s*=.*)$\\\\n",\n\t"replaceWith": "",\n\t"useRegex": true,\n\t"regexFlags": "mg"\n},';
	}
	
	// Eliminate the _category and _remove as well as the starting newline 
	param_text = param_text.replace(/\|\s*\_.*\n/g,'').replace(/^\n/,'');
	
	const replace_regex = /\|\s([\w\s\(\)\-\'\#]*)\s=\s(\w+)/g;
	const replaces = param_text.matchAll(replace_regex);
	
	var count = 0;
	for (const replace of replaces) {
		count = count + 1;
		final_text = final_text 
			+ '{\n\t"replaceText": "(^\\\\s*\\\\|\\\\s*)('
			+ replace[1].replaceAll('-', '\\\\-')
			+ ')(\\\\s*=.*$\\\\n)",\n\t"replaceWith": "$1'
			+ replace[2]
			+ '$3",\n\t"useRegex": true,\n\t"regexFlags": "mg"\n},';
	}
	
	if (param_text.split(/\n/).length -1 != count){
		console.log(param_text);
		alert('One or more params, do not match replace_regex formula.');
		return;
	}
	$('#wpTextbox1').textSelection('setContents', final_text.replace(/^\}\,$/mg, '}'));
	mw.notify('Success');
}
// -------------------------------------------------------------------------------- //
});
//</nowiki>

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.