Module:SpeciesLink: Difference between revisions

From Against the Storm Official Wiki
(is no longer case sensitive)
m (removed trailing periods from error messages; they were getting duplicated.)
 
(2 intermediate revisions by the same user not shown)
Line 15: Line 15:


--region Private constants
--region Private constants
local VALID_SIZES = {
    ["none"] = true,
    ["small"] = true,
    ["medium"] = true,
    ["large"] = true,
    ["huge"] = true,
}


--endregion
--endregion
Line 41: Line 33:
         for key, species in pairs(SpeciesData.species) do
         for key, species in pairs(SpeciesData.species) do
             if string.lower(name) == string.lower(species[SpeciesData.NAME]) or
             if string.lower(name) == string.lower(species[SpeciesData.NAME]) or
            string.lower(name) == string.lower(key) then
                    string.lower(name) == string.lower(key) then
                 return species
                 return species
             end
             end
Line 47: Line 39:
     end
     end


     error("You specified an invalid species name. Please see the template documentation for how to use the parameters.")
     error("You specified an invalid species name. Please see the template documentation for how to use the parameters")
end
end


Line 59: Line 51:


     local name = frame.args.species or frame.args[1]
     local name = frame.args.species or frame.args[1]
     local iconSize = frame.args.iconsize or frame.args[2]
     local iconSize = frame.args.size or frame.args[2]
     local skipText = frame.args.display
     local displayOverride = frame.args.display


     -- Check that something at all was provided for the name before we can validate it.
     -- Check that something at all was provided for the name before we can validate it.
     if not name or name == "" then
     if not name or name == "" then
         error("You must specify the name of the species. Please see the template documentation for how to use the parameters.")
         error("You must specify the name of the species. Please see the template documentation for how to use the parameters")
     end
     end


Line 74: Line 66:


     -- The only valid term here is `notext`, which should be passed through; anything else we blank out.
     -- The only valid term here is `notext`, which should be passed through; anything else we blank out.
     if skipText ~= "notext" then
     if displayOverride ~= "notext" then
         skipText = ""
         displayOverride = ""
     end
     end


Line 81: Line 73:
         ["name"] = speciesName,
         ["name"] = speciesName,
         ["iconfilename"] = iconFilename,
         ["iconfilename"] = iconFilename,
         ["size"] = iconSize,
         ["iconsize"] = iconSize,
         [1] = skipText,
         ["display"] = displayOverride,
     }
     }



Latest revision as of 03:09, 15 October 2024

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

--- @module SpeciesLink
local SpeciesLink = {}



--region Dependencies

local SpeciesData = require("Module:SpeciesData")

local VIEW_TEMPLATE = "Species_link/view"

--endregion



--region Private constants

--endregion



--region Private methods

local function validateSpeciesName(name)

    -- Try a quick match with the keys first.
    local speciesFound = SpeciesData.species[name]

    if speciesFound then
        return speciesFound
    else
        -- Look into the data to see if the name matches any of the names in the data, whether key or name, and be flexible with capitalization.
        for key, species in pairs(SpeciesData.species) do
            if string.lower(name) == string.lower(species[SpeciesData.NAME]) or
                    string.lower(name) == string.lower(key) then
                return species
            end
        end
    end

    error("You specified an invalid species name. Please see the template documentation for how to use the parameters")
end

--endregion



--region Public methods

function SpeciesLink.main(frame)

    local name = frame.args.species or frame.args[1]
    local iconSize = frame.args.size or frame.args[2]
    local displayOverride = frame.args.display

    -- Check that something at all was provided for the name before we can validate it.
    if not name or name == "" then
        error("You must specify the name of the species. Please see the template documentation for how to use the parameters")
    end

    local validatedSpecies = validateSpeciesName(name)
    local speciesName = validatedSpecies[SpeciesData.NAME]
    local iconFilename = validatedSpecies[SpeciesData.ICON_FILENAME] .. ".png"

    -- Defer to the view to handle the iconSize. We don't need to validate it.

    -- The only valid term here is `notext`, which should be passed through; anything else we blank out.
    if displayOverride ~= "notext" then
        displayOverride = ""
    end

    local parameters = {
        ["name"] = speciesName,
        ["iconfilename"] = iconFilename,
        ["iconsize"] = iconSize,
        ["display"] = displayOverride,
    }

    return frame:expandTemplate{
        title = VIEW_TEMPLATE,
        args = parameters,
    }
end

--endregion

return SpeciesLink