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

Content deleted Content added
comma
«+"locz.qAutoEdit();"»
 
(38 intermediate revisions by the same user not shown)
Line 1:
// [[User:Quarl/location_canonicalize.js]] - canonicalizes ___location WikiLinks
// as per [[Wikipedia:WikiProject Location Format]]
 
// Example: [[Seattle, Washington]] becomes [[Seattle, Washington|Seattle]], [[Washington]], [[USA]].
 
// requiresdepends: wikipage.js, util.js, addlilinkwikitabs.js, wikiedit.js, autoedit.js
 
// quarl 2006-01-22 initial version
// quarl 2006-02-08 refactored to autoedit.js
 
//<pre><nowiki>
 
var locz = new autoedit(
// TODO: only link subsequent locations if not already mentioned earlier.
'locz',
'LocZ', 'ca-locz', 'Canonicalize ___location wikilinks',
'Location canonicalization');
 
locz.initData = function() {
var location_canonicalize_USstates = [
var CountryData = function(states, link_country, regexp_country) {
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
this.states = states;
'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
this.link_country = link_country.match(/\[/) ? link_country : '[['+link_country+']]';
'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine',
var regexp_country = regexp_country || '\\[\\['+link_country+'\\]\\]';
'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
this.regexp_country = new RegExp(regexp_country);
'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey',
this.regexp_country_sq = new RegExp('^, *'+regexp_country);
'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio',
this.regexp_substate = (
'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina',
new RegExp('^([^,]+), *(' + this.states.join('|') + ')$'));
'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia',
this.regexp_state = (
'Washington', 'West Virginia', 'Wisconsin', 'Wyoming' ];
new RegExp('^(?:' + this.states.join('|') + ')$'));
 
function location_canonicalize_wikilink(wlink, wtext, left, right) {
if (!window.location_canonicalize_regexp) {
window.location_canonicalize_regexp = (
new RegExp('^([^,]+), *(' +
location_canonicalize_USstates.join('|') +
')$'));
}
 
this.countries = [
// non-main namespace - usually a category
new CountryData( // USA
if (wtext.match(/:/)) return null;
['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',
'Washington, DC', 'Washington, D.C.' // not strictly a state, but needs to be qualified with country also
],
'[[United States|USA]]',
'\\[\\[(?:United[ _]States(?:[ _][^|\\\]]+?)?|USA)(?:\\|[^|\\\]]+?)?\\]\\]'),
 
new CountryData( // Canada
if (wlink == wtext &&
['British Columbia', 'Alberta', 'Saskatchewan', 'Manitoba',
wtext.match(window.location_canonicalize_regexp))
'Ontario', 'Quebec', 'New Brunswick', 'Nova Scotia',
{
var city = RegExp.$1 'Prince Edward Island', state'Newfoundland =and RegExp.$2;Labrador'],
'Canada'),
 
new CountryData( // England
var wfull = '[[' + wtext + '|' + city + ']]';
['Bedfordshire', 'Berkshire', 'City of Bristol',
// only add link to state and country if we haven't yet mentioned them.
'Buckinghamshire', 'Cambridgeshire', 'Cheshire',
if (!left.match('[['+state+']]')) {
wfull +='Cornwall', 'Cumbria', [['+state+Derbyshire']], ';Devon', 'Dorset',
'Durham', 'East Riding of Yorkshire', 'East Sussex', 'Essex',
'Gloucestershire', 'Greater London', 'Greater Manchester',
'Hampshire', 'Herefordshire', 'Hertfordshire', 'Isle of Wight',
'Kent', 'Lancashire', 'Leicestershire', 'Lincolnshire',
'City of London', 'Merseyside', 'Norfolk', 'Northamptonshire',
'Northumberland', 'North Yorkshire', 'Nottinghamshire',
'Oxfordshire', 'Rutland', 'Shropshire', 'Somerset',
'South Yorkshire', 'Staffordshire', 'Suffolk', 'Surrey',
'Tyne and Wear', 'Warwickshire', 'West Midlands', 'West Sussex',
'West Yorkshire', 'Wiltshire', 'Worcestershire'],
'England'),
 
];
if (!left.match(/\[\[(?:United[ _]States(?:[ _][^|\]]+?)?|USA)(?:\|[^|\]]+?|)?\]\]/)) {
}
wfull += ', [[United States|USA]]';
}
}
 
locz.splitText = function(input) {
// get rid of any redundant subsequent [[USA]] link
var inputs = [];
right = right.replace(/^, *\[\[(?:United[ _]States(?:[ _][^|\]]+?)?|USA)(?:\|[^|\]]+?|)?\]\]/, '');
 
// special case for hat link, if there is one
return { wfull: wfull, left: left, right: right};
if (input.match(/^: *''.*/)) {
var infobox = RegExp.lastMatch;
var right = RegExp.rightContext;
 
inputs.push(infobox);
input = right;
}
 
// special case the first Infobox, if there is one
// TODO: Canada, India, etc.
if (input.match(/^(?:{{Infobox(?:.|\n)*?\n}}|{\|(?:.|\n)*?\n\|})/i)) {
// var left = RegExp.leftContext;
var infobox = RegExp.lastMatch;
var right = RegExp.rightContext;
 
// treat the infobox separately, so that USA links get added to main
return null;
// article.
inputs.push(infobox);
input = right;
}
 
inputs.push(input);
return inputs;
}
 
locz.buildRegExp = function() {
function location_canonicalize() {
return /\[\[ *(?:([^|\]]+?) *\| *)?([^\]]+?) *\]\]/;
wikiPage.getEditorAsync(location_canonicalize_edit);
}
 
locz.replaceRegExp = function(d, m) {
function location_canonicalize_edit(editor) {
//var makewlink changes= m[1] || m[2];
var wtext = m[2];
 
// non-main namespace - usually a category
var result = '';
if (wtext.match(/:/)) return;
var input = editor.wpTextbox1;
var changes = [];
 
if (wlink != wtext) return;
while (input.match(/\[\[ *(?:([^|\]]+?) *\| *)?([^\]]+?) *\]\]/)) {
var left = RegExp.leftContext;
var wfull = RegExp.lastMatch;
var wlink = RegExp.$1;
var wtext = RegExp.$2;
var right = RegExp.rightContext;
 
for (i in this.countries) {
result += left;
var c = this.countries[i];
var r = location_canonicalize_wikilink((wlink||wtext), wtext, result, right);
 
if (r) {
var new_wfullchanges = r.wfull0;
var result = r.leftwfull;
if (wtext.match(c.regexp_substate)) {
right = r.right;
changes.push(wfullvar +city '= RegExp.$1, 'state += new_wfull)RegExp.$2;
wfull = '[[' + wtext + '|' + city + ']]';
// only add link to state if we haven't link it yet.
if (d.left.match('\\[\\['+state+'\\]\\]')) {
wfull += ', ' + state;
} else {
wfull += ', [['+state+']]';
}
++changes;
} else if (wtext.match(c.regexp_state)) {
// state link -- just need to add country link as necessary
wfull = '[['+wtext+']]';
}
 
if (!wfull) continue;
 
if (d.left.match(c.regexp_country)) {
// Already mentioned country. Delete redundant subsequent
// country links
if (d.right.match(c.regexp_country_sq)) {
d.right = RegExp.rightContext;
// only count as a change if we actually delete it!
++changes;
}
} else {
var// new_wfullHaven't =mentioned wfull;country earlier
if (d.right.match(c.regexp_country_sq)) {
// it's right after the current link; good.
} else {
// not there; add it.
wfull += ', ' + c.link_country;
++changes;
}
}
result += new_wfull;
input = right;
}
result += input;
 
if (changes.length) {
editor d.wpTextbox1text = resultwfull;
}
editor.wpSummary = '___location canonicalization: ' + changes.join(', ');
editor.wpMinoredit = truereturn;
editor.submit('wpDiff');
} else {
alert("No changes to make!");
}
 
return;
}
 
locz._load = function() {
function location_canonicalize_load() {
locz.qAutoEdit();
if (wikiPage.nsSpecialP) return;
locz.addTab();
addTab('javascript:location_canonicalize()', 'locz', 'ca-locz', 'Canonicalize ___location wikilinks');
}
 
addOnloadHook(location_canonicalize_loadlocz._load);
 
//</nowiki></pre>