Jump to content

User:Zackmann08/scripts/GenerateTable.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<nowiki>
jQuery(document).ready(function($) {

if(mw.config.get('wgPageName').toLowerCase().includes('sandbox') && mw.config.get('wgNamespaceNumber') != 0 && document.getElementsByName('wpTextbox1')[0]) {
	mw.loader.using(['mediawiki.util']).done( function() {
		var portletlink = mw.util.addPortletLink('p-tb','#','GenDepTable','t-gentdeptable');
		$(portletlink).click(function(e) {
			e.preventDefault();
			generateDepTable();
		});
	});
}
// -------------------------------------------------------------------------------- //
function generateDepTable()
{
	var mycontent = $('#wpTextbox1').textSelection('getContents');
	// Copy the contents of the text window so we can modify it without problems
	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];

	console.log(param_text);

	param_text = param_text.replace(/\|\s*\_.*\n/g,'').replace(/^\n/,'');
	
	var final_text = '== Pushpin map cleanup ==\n\nAs part of a larger refactor of [[Module:Location map]], I will be refactoring the way that this Infobox calls the location map code. The only noticeable change for most will be that the parameter names related to the pushpin maps will now all start with {{para|pushpin_...}}. This is part of a larger effort to make things consistent across Infoboxes. When this is done, ALL infoboxes using pushpin maps will use the same parameters. For more information about this, please see [[Module_talk:Location_map#Proposal_to_refactor_the_way_Template:Location_map_works_in_Infoboxes|this post]]. As part of this cleanup, I am also going to resolving any conflicting parameters that I can find and cleaning up parameter names that fail to conform to [[MOS:INFOBOXNAME]]. Note that this follows a long list of similar clean ups that I have done on countless other Infoboxes. The parameters I plan to replace are the following:\n\n{| class="wikitable"\n! Deprecate\n! Replace with\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| {{para|' + replace[1] + '}}\n| {{para|' + replace[2] + '}}\n'
	}
	
	if (param_text.split(/\n/).length -1 != count){
		alert('One or more params, do not match replace_regex formula.');
	}

	final_text += '|}\n\nPlease chime in below with any questions, comments or concerns about this cleanup.'

	$('#wpTextbox1').textSelection('setContents', final_text);
	mw.notify('Success');
}
// -------------------------------------------------------------------------------- //
});
//</nowiki>