User:Quarl/location canonicalize.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:Quarl/location canonicalize. |
// User:Quarl/location_canonicalize.js - canonicalizes location WikiLinks
// Example: [[Seattle, Washington]] becomes [[Seattle, Washington|Seattle]], [[Washington]], [[USA]].
// requires: wikipage.js, util.js, addlilink.js
// quarl 2006-01-22 initial version
//<pre><nowiki>
// TODO: only link subsequent locations if not already mentioned earlier.
var location_canonicalize_USstates = [
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine',
'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey',
'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio',
'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina',
'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia',
'Washington', 'West Virginia', 'Wisconsin', 'Wyoming' ];
function location_canonicalize_wikilink(wlink, wtext, right) {
if (!window.location_canonicalize_regexp) {
window.location_canonicalize_regexp = (
new RegExp('^([^,]+), *(' +
location_canonicalize_USstates.join('|') +
')$'));
}
if (wlink == wtext &&
wtext.match(window.location_canonicalize_regexp))
{
var wfull = ('[[' + wtext + '|' + RegExp.$1 + ']], [[' + RegExp.$2 +
']], [[United States|USA]]');
right = right.replace(/^, *\[\[(?:United[ _]States(?:[ _][^|\]]+?)?|USA)(?:\|[^|\]]+?|)?\]\]/, '');
return { wfull: wfull, right: right};
}
// TODO: Canada, India, etc.
return null;
}
function location_canonicalize() {
wikiPage.getEditorAsync(location_canonicalize_edit);
}
function location_canonicalize_edit(editor) {
// make changes
var result = '';
var input = editor.wpTextbox1;
var changes = [];
while (input.match(/\[\[ *(?:([^|\]]+?) *\| *)?([^\]]+?) *\]\]/)) {
var left = RegExp.leftContext;
var wfull = RegExp.lastMatch;
var wlink = RegExp.$1;
var wtext = RegExp.$2;
var right = RegExp.rightContext;
var r = location_canonicalize_wikilink((wlink||wtext), wtext, right);
if (r) {
var new_wfull = r.wfull;
right = r.right;
changes.push(wfull + ' → ' + new_wfull);
} else {
var new_wfull = wfull;
}
result += left;
result += new_wfull;
input = right;
}
result += input;
if (changes.length) {
editor.wpTextbox1 = result;
editor.wpSummary = 'location canonicalization: ' + changes.join(', ');
editor.submit('wpDiff');
} else {
alert("No changes to make!");
}
}
function location_canonicalize_load() {
addTab('javascript:location_canonicalize()', 'locz', 'ca-locz', 'Canonicalize location wikilinks');
}
addOnloadHook(location_canonicalize_load);
//</nowiki></pre>