Jump to content

User:Quarl/location canonicalize.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Quarl (talk | contribs) at 06:06, 31 January 2006 (〈+"if (wikiPage nsSpecialP) return"〉). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// 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('|') +
                       ')$'));
    }

    // non-main namespace - usually a category
    if (wtext.match(/:/)) return null;

    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() {
    if (wikiPage.nsSpecialP) return;
    addTab('javascript:location_canonicalize()', 'locz', 'ca-locz', 'Canonicalize location wikilinks');
}

addOnloadHook(location_canonicalize_load);

//</nowiki></pre>