Module:SpeciesLink: Difference between revisions

From Against the Storm Official Wiki
No edit summary
m (removed trailing periods from error messages; they were getting duplicated.)
 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
-------------------------------------------------------------------------------
--- @module SpeciesLink
-- This module renders the {{Species _link}} template.
-- https://hoodedhorse.com/wiki/Against_the_Storm/Template:Resource_link
--
-- This template #invokes SpeciesLink.renderLink(frame), below.
--
-- The template requires an argument, the name of the Species to which to link.
-- Optionally, the template accepts a second argument to change the size of the
-- icon that accompanies the text hyperlink. The Species is passed to
-- Module:ResourceData, which is used to identify the wiki page to link to. It
-- also handles some basic normalization to tolerate small mistakes in spelling,
-- spacing, or punctuation.
--
-- Using the data returned, this module creates an icon and the name of the
-- Species, and wraps both in a link to the Species's wiki page. If no icon
-- size is specified, the icon will default to a size appropriate for display
-- in-line with other text.
--
-- IF there is a known Species that does not have an icon (might be the case if
-- the data is being updated), then a question-mark icon will take the place of
-- the Species's icon. This is so that people using this template will not
-- mistake a missing icon for an issue with this template and can efficienty
-- troubleshoot.
--
-- The icons are sized consistently by using existing wiki templates, {{ImgS}},
-- {{ImgM}}, etc. The sizes of icons are not stored or known by this module.
-- @module SpeciesLink
local SpeciesLink = {}
local SpeciesLink = {}


--
-- Dependencies
--
local ResourceData = require("Module:ResourceData") -- lookup table for resources




--region Dependencies


--
local SpeciesData = require("Module:SpeciesData")
-- Constants
--
-- names of the templates used to size the icons consistently
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGLARGE = "ImgL"
local TEMPLATE_IMGHUGE = "ImgH"


-- string options for icon size arguments
local VIEW_TEMPLATE = "Species_link/view"
local S_SML = "small"
local S_MED = "med"
local S_LRG = "large"
local S_HUG = "huge"


-- filename to use if there is no known icon for a known building
--endregion
local REPLACEMENT_FILENAME = "Question_mark.png"






--
--region Private constants
-- Main rendering function
--


-------------------------------------------------------------------------------
--endregion
-- Renders an icon the name of a Species linked to a wiki page corresponding
 
-- to the provided Species.
 
--
 
-- Uses MediaWiki markup to display the name of a wiki page and an icon that has
--region Private methods
-- been uploaded to the wiki. Both must be known by Module:ResourceData, or the
 
-- template's behavior may be different than expected.
local function validateSpeciesName(name)
-- @param frame  a table describing the MediaWiki frame; frame['args'] is a
 
-- table containg the template arguments; frame['args']['Species'] is the first
    -- Try a quick match with the keys first.
-- argument, assigned to the key 'Species'; frame['args']['iconsize'] is the
    local speciesFound = SpeciesData.species[name]
-- second argument, assigned to the key 'iconsize'
 
-- @return a string containing the wiki markup for an icon and an internal wiki
    if speciesFound then
-- page link (or an error if a problem occured)
        return speciesFound
function SpeciesLink.renderLink(frame)
    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.
-- extract the arguments we care about from the frame
        for key, species in pairs(SpeciesData.species) do
local argResourceName = frame.args.Species
            if string.lower(name) == string.lower(species[SpeciesData.NAME]) or
local argIconsize = frame.args.iconsize
                    string.lower(name) == string.lower(key) then
                return species
-- validate that there's a Species name to use
            end
if not argResourceName or "" == argResourceName then
        end
return "SpeciesLink error: no Species given"
    end
end
 
    error("You specified an invalid species name. Please see the template documentation for how to use the parameters")
-- get the page name to make sure there was one
local Species = ResourceData.getPagename(argResourceName)
if not Species or "" == Species then
return "SpeciesLink error: " .. argResourceName .." not found"
end
-- the wiki markup for the internal link to the Species's wiki page
local linkPart = "[[" .. Species .. "]]"
-- store the requested size for use in a moment
local size = ""
-- Check the requested size against the allowed options. Expand the
-- corresponding template.
if S_MED == argIconsize then
size = frame:expandTemplate{title=TEMPLATE_IMGMED}
elseif S_LRG == argIconsize then
size = frame:expandTemplate{title=TEMPLATE_IMGLARGE}
elseif S_HUG == argIconsize then
size = frame:expandTemplate{title=TEMPLATE_IMGHUGE}
else
-- default to small size if the argument wasn't valid
size = frame:expandTemplate{title=TEMPLATE_IMGSMALL}
end
-- If the icon filename doesn't exist, we can still continue by substituting
-- a question mark icon instead.
local icon = ResourceData.getIconFilename(argResourceName)
if not icon or "" == icon then
icon = REPLACEMENT_FILENAME
end
-- combine the size and filename to form the iconPart
local iconPart = "[[File:" .. icon .. "|" .. size .. "|link=" .. Species .. "|alt=" .. Species .. "|" .. Species .. "]]"
-- combine the file part with the link part
return iconPart .. " " .. linkPart
end
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 when required into another module
return SpeciesLink
return SpeciesLink

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