Jump to content

User:Taavi/Aligner.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Taavi (talk | contribs) at 16:34, 6 May 2020 (add support for multiple searches). 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.
mw.loader.using(['mediawiki.util'], function () {
    if (mw.config.get('wgAction') !== 'edit') {
        return;
    }

    mw.util.addPortletLink('p-cactions', 'javascript:void(0);', 'Align template params', 'us-majavah-align');
    $('#us-majavah-align').click(function () {
        const editBox = document.getElementById('wpTextbox1');

        if (!editBox) {
            mw.notify('Edit box not found');
            return;
        }

        const text = editBox.value;

        const searches = [
            "{{infobox",
            "{{speciesbox",
            '{{drugbox',
        ]

        let template = '';
        let open = 0;

        for (let i = 0; i < text.length; i++) {
            let foo = false;

            for (let searchIndex in searches) {
                const search = searches[searchIndex];
                const searchLength = search.length;

                if (text.length - i > searchLength) {
                    if (text.slice(i, i + searchLength).toLowerCase() === search) {
                        open += 1;
                        template += text[i];
                        foo = true;
                    }
                }
            }

            if (open >= 1 && !foo) {
                template += text[i];

                if (text[i] == '{') {
                    open += 1;
                } else if (text[i] == '}') {
                    open -= 1;

                    if (open === 0) {
                        break;
                    }
                }
            }
        }

        if (template === '') {
            mw.notify('Infobox not found');
            return;
        }

        if (open !== 0) {
            console.error({ open });
            mw.notify('Template was not properly closed');
            return;
        }

        let maxLength = 0;

        const origTemplate = String(template);
        const lines = template.split("\n");
        const newLines = [];

        for (let lineNumber in lines) {
            let line = lines[lineNumber].trim();
            if (line.split('|').length !== 2 || line.split('=').length !== 2) {
                newLines.push(line);
                continue;
            }

            let temp = String(line.trim());
            let tempCount = 1;

            while (!temp.startsWith('=') && temp.includes('=')) {
                temp = temp.slice(1);
                tempCount += 1;
            }

            if (tempCount > maxLength) {
                maxLength = tempCount;
            }

            newLines.push(line);
        }

        let output = '';

        for (let lineNumber in newLines) {
            let line = newLines[lineNumber];
            const parts = line.split('=');

            if (parts.length < 2) {
                output += line += "\n";
                continue;
            }

            let firstPart = parts[0];

            while (firstPart.length < maxLength) {
                firstPart += ' ';
            }

            output += firstPart;
            for (let i = 1; i < parts.length; i++) {
                output += '=' + parts[i];
            }
            output += "\n";
        }

        if (output.endsWith("\n")) {
            output = output.slice(0, -1);
        }

        editBox.value = editBox.value.replace(origTemplate, output);
        mw.notify('Successfully aligned templates. Remember to preview before saving and report any issues at User talk:Majavah');
    });
});