User:Quarl/___location canonicalize.js: Difference between revisions

Content deleted Content added
comma
〈176 words changed〉new org w/ namespaces; add USA to lone state links
Line 9:
//<pre><nowiki>
 
location_canonicalize = new Object();
// TODO: only link subsequent locations if not already mentioned earlier.
 
location_canonicalize.run = function() {
var location_canonicalize_USstates = [
location_canonicalize.initData();
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
wikiPage.getEditorAsync(location_canonicalize.edit);
'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);
}
 
location_canonicalize.edit = function(editor) {
function location_canonicalize_edit(editor) {
// make changes
 
Line 102 ⟶ 55:
}
 
location_canonicalize.initData = function() {
function location_canonicalize_load() {
if (this.init) return;
this.init = true;
 
this.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' ];
 
this.regexp_USsubstate = (
new RegExp('^([^,]+), *(' +
location_canonicalize_USstates.join('|') +
')$'));
this.regexp_USstate = (
new RegExp('^(?:' + location_canonicalize_USstates.join('|') +
')$'));
}
 
location_canonicalize.wikilink = function(wlink, wtext, left, right) {
// non-main namespace - usually a category
if (wtext.match(/:/)) return null;
 
if (wlink == wtext &&
wtext.match(location_canonicalize.regexp_USsubstate))
{
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};
}
 
if (wlink == wtext && wtext.match(location_canonicalize.regexp_USstate)) {
// state link -- just make sure there's a [[USA]] link if necessary
if (!left.match(/\[\[(?:United[ _]States(?:[ _][^|\]]+?)?|USA)(?:\|[^|\]]+?|)?\]\]/)) {
var wfull = '[['+wtext+']]';
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};
}
return null;
}
 
// TODO: Canada, India, etc.
 
return null;
}
 
location_canonicalize.load = function() {
if (wikiPage.nsSpecialP) return;
addTab('javascript:location_canonicalize.run()', 'locz', 'ca-locz', 'Canonicalize ___location wikilinks');
}
 
addOnloadHook(location_canonicalize_loadlocation_canonicalize.load);
 
//</nowiki></pre>