// 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, left, right) {
if (!window.location_canonicalize_regexp) {
window.location_canonicalize_regexp = (
new RegExp('^([^,]+), *(' +
location_canonicalize_USstates.join('|') +
')$'));
}
// non-main namespace - usually a category
if (wtext.match(/:/)) return null;
if (wlink == wtext &&
wtext.match(window.location_canonicalize_regexp))
{
var city = RegExp.$1, state = RegExp.$2;
var wfull = '[[' + wtext + '|' + city + ']]';
// only add link to state and country if we haven't yet mentioned them.
if (!left.match('[['+state+']]')) {
wfull += ', [['+state+']]';
if (!left.match(/\[\[(?:United[ _]States(?:[ _][^|\]]+?)?|USA)(?:\|[^|\]]+?|)?\]\]/)) {
wfull += ', [[United States|USA]]';
}
}
// get rid of any redundant subsequent [[USA]] link
right = right.replace(/^, *\[\[(?:United[ _]States(?:[ _][^|\]]+?)?|USA)(?:\|[^|\]]+?|)?\]\]/, '');
return { wfull: wfull, left: left, 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;
result += left;
var r = location_canonicalize_wikilink((wlink||wtext), wtext, result, right);
if (r) {
var new_wfull = r.wfull;
result = r.left;
right = r.right;
changes.push(wfull + ' → ' + new_wfull);
} else {
var new_wfull = wfull;
}
result += new_wfull;
input = right;
}
result += input;
if (changes.length) {
editor.wpTextbox1 = result;
editor.wpSummary = '___location canonicalization: ' + changes.join(', ');
editor.wpMinoredit = true;
editor.submit('wpDiff');
} else {
alert("No changes to make!");
}
}
function location_canonicalize_load() {
if (wikiPage.nsSpecialP) return;
addTab('javascript:location_canonicalize()', 'locz', 'ca-locz', 'Canonicalize ___location wikilinks');
}
addOnloadHook(location_canonicalize_load);
//</nowiki></pre>