User:Mike Dillon/Scripts/convert-brewbox.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

// <pre><nowiki>
addOnloadHook(function () {
    var textBox = document.getElementById('wpTextbox1');
    if (!textBox) return;
    if (!textBox.value.match(/\{\{brewbox[ _]begin/)) return;
    if (textBox.value.match(/addOnloadHook/)) return;
 
    var summary = document.getElementById('wpSummary');
    var minor = document.getElementById('wpMinoredit');
    var diff = document.getElementById('wpDiff');
    if (!summary || !minor || !diff) return;
 
    var li = addPortletLink("p-tb", "#", "Replace brewbox", "ca-brewbox", "Replace brewbox");
    var a = li.getElementsByTagName("a")[0];
    a.onclick = function () {
        var lines = textBox.value.split(/\r\n|\n/);
 
        var output = '';
        var brewbox = {};
        var current_beers;
        var in_brewbox = false;
        var m;
        for (var i in lines) {
            var cur = lines[i];
 
            if ((m = cur.match(/\{\{brewbox[ _]begin/)) || in_brewbox) {
                if (!in_brewbox) {
                    brewbox = {
                        name: '',
                        image: '',
                        caption: '',
                        location: '',
                        owner: '',
                        opened: '',
                        production: '',
                        active_beers: '',
                        seasonal_beers: '',
                        other_beers: ''
                    };
                    delete current_beers;
                    in_brewbox = true;
                }
 
                cur = cur.replace(/^\s+/, "");
                cur = cur.replace(/\s+$/, "");
                cur = cur.replace(/\{\{brewbox /, "{{brewbox_");
 
                if (m = cur.match(/^\{\{brewbox_begin\|name=(.*)\}\}$/)) {
                    brewbox.name = m[1];
                } else if (m = cur.match(/^\{\{brewbox_image\|image=(.*)\|caption=(.*)\}\}$/)) {
                    brewbox.image = m[1];
                    brewbox.caption = m[2];
                } else if (m = cur.match(/^\{\{brewbox_image\|image=(.*)\}\}$/)) {
                    brewbox.image = m[1];
                } else if (m = cur.match(/^\{\{brewbox_location\|location=(.*)\}\}$/)) {
                    brewbox.location = m[1];
                } else if (m = cur.match(/^\{\{brewbox_owner\|owner=(.*)\}\}$/)) {
                    brewbox.owner = m[1];
                } else if (m = cur.match(/^\{\{brewbox_opened\|year=(.*)\}\}$/)) {
                    brewbox.opened = m[1];
                } else if (m = cur.match(/^\{\{brewbox_production\|amount=(.*)\}\}$/)) {
                    brewbox.production = m[1];
                } else if (m = cur.match(/^\{\{brewbox_beers\}\}$/)) {
                    current_beers = 'active_beers';
                } else if (m = cur.match(/^\{\{brewbox_beers\|type=\s*Seasonal\s*\}\}$/)) {
                    current_beers = 'seasonal_beers';
                } else if (m = cur.match(/^\{\{brewbox_beers\b/)) {
                    current_beers = 'other_beers';
                    brewbox[current_beers] = brewbox[current_beers] + "\n" +
                        "    " + cur;
                } else if (cur.match(/\{\{brewbox_end/)) {
                    in_brewbox = false;
 
                    if (output) output += "\n";
                    output +=
                        "{{Infobox Brewery\n" +
                        "| name           = " + brewbox.name + "\n" +
                        "| image          = " + brewbox.image + "\n" +
                        "| caption        = " + brewbox.caption + "\n" +
                        "| location       = " + brewbox.location + "\n" +
                        "| owner          = " + brewbox.owner + "\n" +
                        "| opened         = " + brewbox.opened + "\n" +
                        "| production     = " + brewbox.production + "\n" +
                        "| active_beers   = " + brewbox.active_beers + "\n" +
                        "| seasonal_beers = " + brewbox.seasonal_beers + "\n" +
                        "| other_beers    = " + brewbox.other_beers + "\n" +
                        "}}";
                } else if (current_beers) {
                    brewbox[current_beers] += "\n    " + cur;
                }
            } else {
                if (output) output += "\n";
                output += cur;
            }
        }
 
        summary.value = 'Convert to {{Infobox Brewery}}';
        if (!minor.checked) minor.click();
        textBox.value = output;
        diff.click();
 
        return false;
    };
});
// </nowiki></pre>