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 07:24, 31 January 2006 (no "this" in editor function). 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>

location_canonicalize = new Object();

location_canonicalize.run = function() {
    location_canonicalize.initData();
    wikiPage.getEditorAsync(location_canonicalize.edit);
}

location_canonicalize.edit = function(editor) {
    // make changes

    var result = '';
    var input = editor.wpTextbox1;
    var changes = [];

    // special case the first Infobox, if there is one
    if (input.match(/^{{Infobox(.|\n)*?\n}}/)) {
        // var left = RegExp.leftContext;
        var infobox = RegExp.lastMatch;
        var right = RegExp.rightContext;

        // treat the infobox separately, so that USA links get added to main
        // article.
        result = (location_canonicalize.canonicalizeString(infobox, changes) +
                  location_canonicalize.canonicalizeString(right, changes));
    } else {
        result = location_canonicalize.canonicalizeString(input, changes);
    }

    if (changes.length) {
        editor.wpTextbox1 = result;
        editor.wpSummary = 'location canonicalization: ' + changes.join(', ');
        editor.wpMinoredit = true;
        editor.submit('wpDiff');
    } else {
        alert("No changes to make!");
    }
}

location_canonicalize.canonicalizeString = function(input, changes) {
    var result = '';
    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;
    return result;
}

location_canonicalize.initData = function() {
    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('^([^,]+), (' + this.USstates.join('|') + ')$'));
    this.regexp_USstate = (
        new RegExp('^(?:' + this.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.load);

//</nowiki></pre>