User:Tollens/speciesListFormatter.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Tollens/speciesListFormatter.
/*
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>