Jump to content

Module:IUCN status

From Calamity Wiki

Documentation for this module may be created at Module:IUCN status/doc

local p = {}

-- Data table for colors, full names, standardized display, and wiki article links
local status_data = {
    lc = { long = "Least Concern", bg = "gray", fg = "white", is_gray = true, display = "LC", link = "Least Concern" },
    vu = { long = "Vulnerable", bg = "#CC9900", fg = "#FFFFCB", display = "VU", link = "Vulnerable species" },
    dd = { long = "Data Deficient", bg = "gray", fg = "white", is_gray = true, display = "DD", link = "Data Deficient" },
    en = { long = "Endangered", bg = "#CC6633", fg = "#FFCC99", display = "EN", link = "Endangered species" },
    cr = { long = "Critically Endangered", bg = "#CC3333", fg = "#FFCCCC", display = "CR", link = "Critically Endangered" },
    nt = { long = "Near Threatened", bg = "#006666", fg = "#99CC99", display = "NT", link = "Near Threatened" },
    ex = { long = "Extinct", bg = "black", fg = "#CC3333", display = "EX", link = "Extinction" },
    ew = { long = "Extinct in the Wild", bg = "black", fg = "white", display = "EW", link = "Extinct in the Wild" },
    re = { long = "Regionally Extinct", bg = "black", fg = "white", display = "RE", link = "Regional extinction" },
    na = { long = "Not Applicable", bg = "gray", fg = "white", is_gray = true, display = "NA", link = "Not Applicable (IUCN)" },
    ne = { long = "Not Evaluated", bg = "gray", fg = "white", is_gray = true, display = "NE", link = "Not Evaluated" },
    -- Normalized Special Statuses
    pe = { long = "Critically Endangered (Possibly Extinct)", bg = "#CC3333", fg = "#FFCCCC", display = "CR (PE)", link = "Critically Endangered (Possibly Extinct)" },
    ["cr(pe)"] = { long = "Critically Endangered (Possibly Extinct)", bg = "#CC3333", fg = "#FFCCCC", display = "CR (PE)", link = "Critically Endangered (Possibly Extinct)" },
    pew = { long = "Critically Endangered (Possibly Extinct in the Wild)", bg = "#CC3333", fg = "#FFCCCC", display = "CR (PEW)", link = "Critically Endangered (Possibly Extinct in the Wild)" },
    ["cr(pew)"] = { long = "Critically Endangered (Possibly Extinct in the Wild)", bg = "#CC3333", fg = "#FFCCCC", display = "CR (PEW)", link = "Critically Endangered (Possibly Extinct in the Wild)" },
    -- Legacy/Alternative Aliases
    ["lr/nt"] = { long = "Near Threatened", bg = "#006666", fg = "#99CC99", display = "NT", link = "Near Threatened" },
    ["lr/lc"] = { long = "Least Concern", bg = "gray", fg = "white", is_gray = true, display = "LC", link = "Least Concern" },
    ["lr/cd"] = { long = "Lower Risk/conservation dependent", bg = "#006666", fg = "#99CC99", display = "LR/cd", link = "Conservation Dependent" },
}

function p.main(frame)
    local args = frame:getParent().args
    local raw_input = mw.text.trim(args[1] or "")
    local code = string.lower(raw_input):gsub("%s+", "")
    
    local id = mw.text.trim(args[2] or "")
    local class = mw.text.trim(args[3] or args.class or "")
    local year = mw.text.trim(args.year or "")
    local label = mw.text.trim(args.label or "")

    local category = ""
    local data = status_data[code]
    
    -- Handle unknown codes
    if not data then
        data = { long = raw_input, bg = "#004080", fg = "white", display = raw_input, link = "" }
        if code ~= "nr" and code ~= "" then
            category = "[[Category:IUCN status templates with invalid parameters]]"
        end
    end

    -- 1. Build the Status Badge
    local style = string.format("background-color: %s; color: %s; padding: 0 1pt;", data.bg, data.fg)
    if data.is_gray then style = "color: gray;" end
    
    local title = "IUCN Red List: " .. (code == "nr" and "(taxon not recognized)" or data.long)
    local display_text = (code == "nr") and ("(" .. raw_input .. ")") or data.display
    
    local res = string.format('<span style="%s" title="%s"><b>%s</b></span>', style, title, display_text)

    -- 2. Apply Wiki Link (Class 2 or 12)
    if (class == "2" or class == "12") and data.link ~= "" then
        res = string.format("[[%s|%s]]", data.link, res)
    end

    -- 3. Build the URL/Reference (Class 1 or 12)
    if (class == "1" or class == "12") and id ~= "" then
        local url_path = id:find("/") and "species/" or "details/"
        local clean_id = id:find("/") and id or (id .. "/0")
        
        local link_text = "IUCN"
        if label ~= "" then link_text = label
        elseif year ~= "" then link_text = "IUCN " .. year end

        local url = string.format("https://www.iucnredlist.org/%s%s", url_path, clean_id)
        res = res .. string.format('<sup> <span class="plainlinks" title="IUCN Red List assessment">[%s %s]</span></sup>', url, link_text)
    end

    return res .. category
end

return p