Module:SpecializationLink: Difference between revisions

From Against the Storm Official Wiki
(created)
 
m (renaming first parameter to name for consistency with other link templates)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
-------------------------------------------------------------------------------
---Retrieves and validates data on the specified specialization, then sends data over to the view, another template.
-- This module renders the {{Specialization _link}} template.
---@module SpecializationLink
-- https://hoodedhorse.com/wiki/Against_the_Storm/Template:Specialization_link
--
-- This template #invokes SpecializationLink.renderLink(frame), below.
--
-- The template requires an argument, the name of the specialization to which to
-- link. Optionally, the template accepts a second argument to change the size
-- of the icon that accompanies the text hyperlink.
--
-- Using the arguments, this module creates an icon and the name of the
-- specialization, and wraps both in a link to the specialization's wiki page.
-- If no icon size is specified, the icon will default to a size appropriate for
-- display in-line with other text.
--
-- 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 SpecializationLink
local SpecializationLink = {}
local SpecializationLink = {}


--
-- Constants
--
-- specialization names
local SPEC_ALCH = "Alchemy"
local SPEC_BREW = "Brewing"
local SPEC_CLOT = "Cloth"
local SPEC_ENGI = "Engineering"
local SPEC_FARM = "Farming"
local SPEC_MEAT = "Meat production"
local SPEC_WARM = "Warmth"
local SPEC_WOOD = "Wood"


-- 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
--region Dependencies
local S_SML = "small"
local S_MED = "med"
local S_LRG = "large"
local S_HUG = "huge"


--
local SpecializationsData = mw.loadData("Module:SpecializationsData")
-- Main rendering function
--


-------------------------------------------------------------------------------
local VIEW_TEMPLATE = "Specialization_link/view"
-- Renders an icon the name of a specialization linked to a wiki page
 
-- corresponding to the provided specialization.
--endregion
--  
 
-- Uses MediaWiki markup to display the name of a wiki page and an icon that has
 
-- been uploaded to the wiki.
 
-- @param frame a table describing the MediaWiki frame; frame['args'] is a
--region Private constants
-- table containg the template arguments; frame['args']['spec'] is the first
 
-- argument, assigned to the key 'spec'; frame['args']['iconsize'] is the
--endregion
-- second argument, assigned to the key 'iconsize'
 
-- @return a string containing the wiki markup for an icon and an internal wiki
 
-- page link (or an error if a problem occured)
 
function SpecializationLink.renderLink(frame)
--region Public methods
 
-- extract the arguments we care about from the frame
---@public
local argSpecName = frame.args.spec
---Validates data then compiles arguments to send to the view template.
local argIconsize = frame.args.iconsize
---
---@param frame table the MediaWiki calling context
-- validate that there's a resource name to use
---@return string wiki markup with the results
if not argSpecName or "" == argSpecName then
function SpecializationLink.main(frame)
return "SpecializationLink error: no resource given"
 
end
    --Extract the template parameters.
    local specialization = frame.args.name or frame.args[1]
-- simple lookup for a small number of specializations with little extra data
    local iconSize = frame.args.size
local specLookup = {
    local showType = frame.args.display == "show_type"
[SPEC_ALCH] = { iconfile="Icon Spec Alchemy 64x64.png" },
    local noText = frame.args.display == "notext"
[SPEC_BREW] = { iconfile="Icon Spec Brewing 64x64.png" },
 
[SPEC_CLOT] = { iconfile="Icon Spec Cloth 64x64.png" },
    if not specialization or "" == specialization then
[SPEC_ENGI] = { iconfile="Icon Spec Gear 64x64.png" },
        error("The Specialization link template requires the name of a specialization")
[SPEC_FARM] = { iconfile="Icon_Spec_Farm_64x64.png" },
    end
[SPEC_MEAT] = { iconfile="Icon_Spec_Meat_64x64.png" },
 
[SPEC_WARM] = { iconfile="Icon_Spec_Fire_64x64.png" },
    --Use the icon filename for validation check.
[SPEC_WOOD] = { iconfile="Icon_Spec_Wood_64x64.png" }
    local iconFilename = nil
}
    local type= "Type"
    local species = "Species"
-- make sure the argument was a valid name
 
local specInfo = specLookup[argSpecName]
    for _, specData in ipairs(SpecializationsData.specializations) do
if not specInfo then
        --Once match found, extract the data
return "SpecializationLink error: no specialization " .. argSpecName .. " found."
        if string.lower(specData[SpecializationsData.NAME]) == string.lower(specialization) then
end
            iconFilename = specData[SpecializationsData.ICON_FILENAME]
            type = (specData[SpecializationsData.IS_COMFORTABLE] and "Comfort") or (specData[SpecializationsData.IS_PROFICIENCY] and "Proficiency") or "Type"
-- the wiki markup for the internal link to the specializations's wiki page
        end
local linkPart = "[[" .. argSpecName .. "]]"
    end
 
-- store the requested size for use in a moment
    if not iconFilename then
local size = ""
        error("No specialization found with that name: " .. specialization)
    end
-- Check the requested size against the allowed options. Expand the
 
-- corresponding template.
    local viewParameters = {
if S_MED == argIconsize then
        ["name"] = specialization,
size = frame:expandTemplate{title=TEMPLATE_IMGMED}
        ["iconfilename"] = iconFilename,
elseif S_LRG == argIconsize then
        ["iconsize"] = iconSize,
size = frame:expandTemplate{title=TEMPLATE_IMGLARGE}
        ["type"] = showType and type or "",
elseif S_HUG == argIconsize then
        ["display"] = noText and "notext" or "",
size = frame:expandTemplate{title=TEMPLATE_IMGHUGE}
    }
else
 
-- default to small size if the argument wasn't valid
    return frame:expandTemplate{
size = frame:expandTemplate{title=TEMPLATE_IMGSMALL}
        title = VIEW_TEMPLATE,
end
        args = viewParameters,
    }
-- If the icon filename doesn't exist, we can still continue by substituting
-- a question mark icon instead.
local icon = specInfo.iconfile
-- combine the size and filename to form the iconPart
local iconPart = "[[File:" .. icon .. "|" .. size .. "|link=" .. argSpecName .. "|alt=" .. argSpecName .. "|" .. argSpecName .. "]]"
-- combine the file part with the link part
return iconPart .. " " .. linkPart
end
end


--endregion


-- return when required into another module
return SpecializationLink
return SpecializationLink

Latest revision as of 21:53, 8 November 2024

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

---Retrieves and validates data on the specified specialization, then sends data over to the view, another template.
---@module SpecializationLink
local SpecializationLink = {}



--region Dependencies

local SpecializationsData = mw.loadData("Module:SpecializationsData")

local VIEW_TEMPLATE = "Specialization_link/view"

--endregion



--region Private constants

--endregion



--region Public methods

---@public
---Validates data then compiles arguments to send to the view template.
---
---@param frame table the MediaWiki calling context
---@return string wiki markup with the results
function SpecializationLink.main(frame)

    --Extract the template parameters.
    local specialization = frame.args.name or frame.args[1]
    local iconSize = frame.args.size
    local showType = frame.args.display == "show_type"
    local noText = frame.args.display == "notext"

    if not specialization or "" == specialization then
        error("The Specialization link template requires the name of a specialization")
    end

    --Use the icon filename for validation check.
    local iconFilename = nil
    local type= "Type"
    local species = "Species"

    for _, specData in ipairs(SpecializationsData.specializations) do
        --Once match found, extract the data
        if string.lower(specData[SpecializationsData.NAME]) == string.lower(specialization) then
            iconFilename = specData[SpecializationsData.ICON_FILENAME]
            type = (specData[SpecializationsData.IS_COMFORTABLE] and "Comfort") or (specData[SpecializationsData.IS_PROFICIENCY] and "Proficiency") or "Type"
        end
    end

    if not iconFilename then
        error("No specialization found with that name: " .. specialization)
    end

    local viewParameters = {
        ["name"] = specialization,
        ["iconfilename"] = iconFilename,
        ["iconsize"] = iconSize,
        ["type"] = showType and type or "",
        ["display"] = noText and "notext" or "",
    }

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

--endregion

return SpecializationLink