🇮🇷 Iran Proxy | https://www.wikipedia.org/wiki/User:Tollens/speciesListFormatter.js
Jump to content

User:Tollens/speciesListFormatter.js

From Wikipedia, the free encyclopedia
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.
/*
    Quickly formats a list of species copied from Catalogue of Life or ITIS.
    To use, paste the list into the dialog and click "Format", then copy the
    formatted list from the dialog. Footnotes will be attached to every entry.
    The tool does not validate input, so please ensure the resulting list is
    correct before using it in an article.

    To use this script, add the following line to your Special:MyPage/common.js:

    importScript("User:Tollens/speciesListFormatter.js"); // Backlink: [[User:Tollens/speciesListFormatter.js]]

    The button will be placed in the same menu as the "Move" button, which
    depending on the skin used could be called "More" or "Tools".
*/

//<nowiki>
$.when(mw.loader.using(["mediawiki.util"]), $.ready).done(function () {
    let button = mw.util.addPortletLink(
        "p-cactions",
        "#",
        "Format species list",
        "ca-format-specieslist",
        "Format a species list from Catalogue of Life or ITIS"
    );

    if (button) {
        button.addEventListener("click", function () {
            $(
                "<div id='speciesListFormatterDialog' title='Species List Formatter'><textarea id='speciesListFormatterTextarea' style='width: 100%; height: 300px; resize: none; padding: 1px; border-width: 1px; box-sizing: border-box;' placeholder='Paste here from Catalogue of Life or ITIS'></textarea><input id='speciesListFormatterFootnoteInput' style='width:100%; padding: 1px; border-width: 1px; box-sizing: border-box;' placeholder='Footnotes to include on each entry (ex. a b c)'></input></div>"
            ).dialog({
                buttons: [
                    {
                        text: "Format",
                        click: function () {
                            let textarea = document.getElementById("speciesListFormatterTextarea");
                            let footnotes = document.getElementById("speciesListFormatterFootnoteInput").value.trim();
                            textarea.value = textarea.value
                                .trim()
                                .replace(/species:?( |\t)*/gi, "")
                                .split("\n")
                                .map((line) => line.trim())
                                .map((line) => {
                                    const isExtinct = line.includes("†");
                                    line = line.replace(/† */, "");

                                    const secondSpace = line.indexOf(" ", line.indexOf(" ") + 1);
                                    return {
                                        name: line.substring(0, secondSpace),
                                        published: line.substring(secondSpace + 1).replace(/\(|\)/g, ""),
                                        extinct: isExtinct
                                    };
                                })
                                .map(
                                    (entry) =>
                                        "* " +
                                        (entry.extinct ? "{{extinct}}" : "") +
                                        "''[[" +
                                        entry.name +
                                        "]]'' <small>" +
                                        entry.published +
                                        "</small>" +
                                        (footnotes !== ""
                                            ? '<span style="color:gray"><sup> ' + footnotes + "</sup></span>"
                                            : "")
                                )
                                .join("\n");
                        }
                    }
                ],
                close: function () {
                    $(this).dialog("destroy").remove();
                },
                width: Math.max(400, window.innerWidth / 2)
            });
        });
    }
});
//</nowiki>