User:GregorB/ConvertLocationMap.js

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.
//<syntaxhighlight lang="JavaScript" line>
if( document.editform) {
var myContent = document.editform.wpTextbox1;
 
if (mw.config.get('wgNamespaceNumber') != -1 && myContent) $(onLoad);

function onLoad() {
	var portletLink = mw.util.addPortletLink('p-tb', '#', 'Convert Location map to Infobox settlement', 't-convertlocationmap');
	$(portletLink).click(wpConvertLocationMap);
}
 
function wpConvertLocationMap() {
	var name = mw.config.get('wgTitle').replace(/(,| \().*/, '');
	var categories = mw.config.get('wgCategories');
	var settlementType = categories.indexOf('Municipalities of Croatia') != -1 ? 'Municipality' : '';
	var countyName = '';
	
	for (var i in categories) {
		if (categories[i].match(/^Populated places in .* (County|City of Zagreb)$/) !== null) {
			countyName = '[[' + categories[i].replace(/Populated places in (the )?/, '') + ']]';
			break;
		}
	}

	var newContent = myContent.value.replace(
		/{{[Ll]ocation map[\s\S]*\|\s*caption\s*=(.*)\n\|\s*lat\s*=(.*)\n\|\s*long\s*=(.*)}}/, 
		'{{Infobox settlement\n' +
		'| name                = ' + name + '\n' +
		'| settlement_type     = ' + settlementType + '\n' +
		'| pushpin_map         = Croatia\n' +
		'| pushpin_map_caption =$1\n' +
		'| coordinates         = {{coord|$2|$3|region:HR_type:city|display=inline,title}}\n' +
		'| subdivision_type    = Country\n' +
		'| subdivision_name    = {{flag|Croatia}}\n' +
		'| subdivision_type1   = [[Counties of Croatia|County]]\n' +
		'| subdivision_name1   = ' + countyName + '\n' +
		'| subdivision_type2   = \n' +
		'| subdivision_name2   = \n' +		
	    '}}'
	);

    if (myContent.value != newContent) {
    	myContent.value = newContent;
		document.editform.wpSummary.value = 'Convert Location map to Infobox settlement using [[User:GregorB/ConvertLocationMap.js|script]]';
		document.editform.wpDiff.click(); // Auto-Show changes
    }
}
}
//</syntaxhighlight>