Content deleted Content added
〈218 words changed〉add Canada |
〈672 words changed〉fully generalized, more robust |
||
Line 78:
this.init = true;
var initRegexp = function(states, link_country, regexp_country) {
d = new Object();
d.states = states;
d.link_country = link_country.match(/\[/) ? link_country : '[['+link_country+']]';
var regexp_country = regexp_country || '\\[\\['+link_country+'\\]\\]';
d.regexp_country = new RegExp(regexp_country);
d.regexp_country_sq = new RegExp(', '+regexp_country);
d.regexp_substate = (
new RegExp('^([^,]+), (' + d.states.join('|') + ')$'));
d.regexp_state = (
new RegExp('^(?:' + this.states.join('|') + ')$'));
}
this.
initRegexp( // USA
['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' ],
'[[United States|USA]]',
'\\[\\[(?:United[ _]States(?:[ _][^|\\\]]+?)?|USA)(?:\|[^|\\\]]+?)?\\]\\]'),
initRegexp( // Canada
['British Columbia', 'Alberta', 'Saskatchewan', 'Manitoba',
'Canada'),
];
}
Line 110 ⟶ 117:
if (wtext.match(/:/)) return null;
if (wlink != wtext) return;
for (i in this.countries) {
var c = this.countries[i];
var wfull;
if (wtext.match(c.regexp_substate)) {
var city = RegExp.$1, state = RegExp.$2;
wfull = '[[' + wtext + '|' + city + ']]';
// only add link to state and country if we haven't yet mentioned them.
if (left.match('\\[\\['+state+'\\]\\]')) {
} else {
wfull += ', [['+state+']]';
}
} else if (wtext.match(c.regexp_state)) {
// state link -- just need to add country link as necessary
wfull = '[['+wtext+']]';
}
if (!wfull) continue;
if (left.match(c.regexp_country)) {
// Already mentioned country. Delete redundant subsequent
// country links
right = right.replace(c.regexp_country_sq, '');
} else {
// Haven't mentioned country earlier
} else {
wfull += ', ' + c.link_country;
}
}
return { wfull: wfull, left: left, right: right};
}
return null;
|