User:Panamitsu/script/macrons.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Panamitsu/script/macrons. |
/*
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, '\\$&');
}
});