Jump to content

User:Panamitsu/script/macrons.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.
/*
This script adds macrons to New Zealand place names and words.
For example, Manawatu -> Manawatū, Maori -> Māori.
*/

$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
	const WORDS = ["Ahikōuka","Ākitio","Ākura","Āpiti","Ātiamuri","Āwhitu","Āwhitu Central","Colac Bay/Ōraka","East Tākaka","East Tāmaki","East Tāmaki Heights","Eketāhuna","Hāmama","Hāwea Flat","Hāwera","Hūkerenui","Kahikatea Pā","Kaikōura","Kōpuaranga","Kūaotunu","Kūaotunu West","Kumeū","Lake Hāwea","Lake Ōmāpere","Lower Kawhātau","Māhia","Māhia Beach","Māhina Bay","Mākara","Mākara Beach","Mākareao","Manawatū","Mangarākau","Mangatāwhai","Mangatāwhiri","Māngere","Māngere Bridge","Māngere East","Mangōnui","Māpua","Mārahau","Māriri","Mātaikōtare","Matatā","Mōkau","Mōrere","Mōtū","Motumānawa / Pollen Island","Ngāhape","Ngāhinapōuri","Ngāruawāhia","Ngātīmoti","Ngongotahā","Ngongotahā Valley","Nūhaka","Ōakura","Ōakura","Ōhaeawai","Ōhau","Ōhaupō","Ōhingaiti","Ōhiwa","Ōhope","Ōhura","Ōkaihau","Ōkārito","Ōkato","Ōkiwi Bay","Ōkura","Ōmāpere","Ōmiha","Ōmokoroa","Ōmokoroa Beach","Ōnoke","Ōpaheke","Ōpaki","Ōpārara","Ōpārau","Ōpōtiki","Ōpou","Ōpunake","Ōrere","Ōrere Point","Ōtāhuhu","Ōtaki","Ōtaki Beach","Ōtaki Forks","Ōtāne","Ōtara","Ōtara","Ōtaua","Otūmoetai","Ōwhango","Ōwhata","Ōwhata","Owhiro","Ōwhiro","Ōwhiro Bay","Paekākāriki","Paekākāriki Hill","Pāhaoa","Pākawau","Pākuratahi","Pangatōtara","Papakōwhai","Pārae Karetu","Pāremoremo","Pāuatahanui","Pōhara","Pōkeno","Pōrangahau","Port Ōhope","Port Pūponga","Port Whangārei","Pūerua","Pūkio","Pūkorokoro / Miranda","Pūkorokoro / Miranda Hot Springs","Pūponga","Pūrākaunui","Puramāhoi","Putāruru","Rākau","Rāngaiika","Rangitūmau","Rānui","Rātana","Ruakākā","Ruakōkoputuna","Rūnanga Pā","Tāhunanui","Taitā","Tākaka","Takapūwāhia","Tākou Bay","Tāmaki","Tāneatua","Tāngarākau","Taupō","Taupō Bay","Tāwharanui","Te Ākau","Te Ākau South","Te Atatū Peninsula","Te Atatū South","Te Hāpua","Te Kōpuru","Te Mārua","Te Pōhue","Te Rāpaki-o-Te Rakiwhakaputa","Tīnui","Tīrau","Tīraumea","Tītahi Bay","Tongapōrutu","Tōrere","Tōtara Park","Tōtara Vale","Tōtaranui","Tūī Glen","Tūrangi","Umukurī","Upper Ātiamuri","Upper Kawhātau","Upper Tākaka","Waihāhā","Waitākere","Waimā","Waimā Valley","Waimārama","Waipātiki Beach","Wairau Pā","Wairoa Pā","Waitākere","Waitārere","Waitōtara","Wānaka","Whakamārama","Whakatāne","Whakatīwai","Whāngaimoana","Whangākea / Pandora","Whangamatā","Whangamōmona","Whangaparāoa","Whangaparāoa","Whangapē","Whangārei","Whangārei Heads","Wharekōpae","Wharepūhunga","Māori"];
	const VOWELS = ["ā", "a", "ē", "e", "ī", "i", "ō", "o", "ū", "u"];
	
	// A placement of the word in relation to the surrounding text, with the asterisk respresenting the Māori word.
	// (to solve the Scunthorpe problem).
	const PLACEMENTS = [" * ", " *.", " *,", " *;", " *—", " *s ", " *s.", " *s,", " *s;", " *s—", "[*]", "(*)", "|*]", "[*|"];
	
	pathoschild.TemplateScript.add([
		{
			name: 'Māori macrons',
			script: function(editor) {
				// Do replacements
				for (let word of WORDS) {
					for (let placement of PLACEMENTS) {
						let strWithMacrons = placement.replace("*", word);
						let strNoMacrons   = placement.replace("*", removeMacrons(word));
						
						editor.replace(new RegExp(escapeRegExp(removeMacrons(strNoMacrons)), "g"), strWithMacrons);
					}
				}
				
				// Options
				editor
					.options({ minor: true })
					.appendEditSummary("add macron(s) by [[User:Panamitsu/script/macrons.js|script]]")
					.clickDiff();
			}
		},
	]);
	
	function removeMacrons(s) {
		for (let i = 0; i < VOWELS.length; i += 2) {
			let first = VOWELS[i];
			let second = VOWELS[i+1];
			s = s.replaceAll(first, second);
			s = s.replaceAll(first.toUpperCase(), second.toUpperCase());
		}
		return s;
	}
	
	function escapeRegExp(text) {
		//https://stackoverflow.com/a/9310752/14063938
		return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
	}
});