Module:SpecializationLink: Difference between revisions

From Against the Storm Official Wiki
(Updated with Foxes specs)
(Updated with new specialization names; refactored the code; now using StyleUtils)
Line 1: Line 1:
-------------------------------------------------------------------------------
-- This module renders the {{Specialization _link}} template.
-- 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
 
--
--region Dependencies
-- specialization names
 
local StyleUtils = require("Module:StyleUtils")
 
--endregion
 
 
 
--region Private constants
 
local SPEC_ALCH = "Alchemy"
local SPEC_ALCH = "Alchemy"
local SPEC_BREW = "Brewing"
local SPEC_BREW = "Brewing"
local SPEC_CLOT = "Cloth"
local SPEC_ENGI = "Engineering"
local SPEC_ENGI = "Engineering"
local SPEC_FARM = "Farming"
local SPEC_FARM = "Farming"
Line 31: Line 20:
local SPEC_MEAT = "Meat production"
local SPEC_MEAT = "Meat production"
local SPEC_RAIN = "Rainwater"
local SPEC_RAIN = "Rainwater"
local SPEC_TAIL = "Tailoring"
local SPEC_WARM = "Warmth"
local SPEC_WARM = "Warmth"
local SPEC_WOOD = "Wood"
local SPEC_WOOD = "Woodworking"
 
-- Icon filenames
local SPECIALIZATION_ICON_FILENAMES = {
    [SPEC_ALCH] = "Icon Spec Alchemy 64x64.png",
    [SPEC_BREW] = "Icon Spec Brewing 64x64.png",
    [SPEC_ENGI] = "Icon Spec Gear 64x64.png",
    [SPEC_FARM] = "Icon_Spec_Farm_64x64.png",
    [SPEC_FORE] = "Icon Spec Forest 64x64.png",
    [SPEC_MEAT] = "Icon_Spec_Meat_64x64.png",
    [SPEC_RAIN] = "Icon Spec RainWater 64x64.png",
    [SPEC_TAIL] = "Icon Spec Cloth 64x64.png",
    [SPEC_WARM] = "Icon_Spec_Fire_64x64.png",
    [SPEC_WOOD] = "Icon_Spec_Wood_64x64.png"
}
 
--endregion
 
 
 
--region Private member variables
 
--endregion
 
 
 
--region Private methods
 
local function resolveIconSize(iconSizeRequested)
 
    -- default for these links is small
    local size = StyleUtils.IMG_S()
 
    if iconSizeRequested == "med" then
        size = StyleUtils.IMG_M()
    else
        if iconSizeRequested == "large" then
            size = StyleUtils.IMG_L()
        else
            if iconSizeRequested == "huge" then
                size = StyleUtils.IMG_H()
            end
        end
    end
 
    return size
end
 
 
 
local function makeIconPart(pageName, iconFilename, iconSize)
 
    -- Skip the icon if none requested. Continue if empty!
    if iconSize == "none" then
        return ""
    end
 
    local size = resolveIconSize(iconSize)
 
    return string.format("[[File:%s|%s|link=%s|alt=%s Specialization|%s Specialization]] ", iconFilename, size, pageName, pageName, pageName)
end
 
 
 
local function renderParts(specialization, iconFilename, iconSize)
 
    local iconPart = makeIconPart(specialization, iconFilename, iconSize)
 
    local linkPart = "[[" .. specialization .. " Specialization|" .. specialization .. "]]"
 
    return iconPart .. linkPart
end
 
--endregion


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


--
-- Main rendering function
--


-------------------------------------------------------------------------------
--region Public methods
-- Renders an icon the name of a specialization linked to a wiki page
 
-- corresponding to the provided specialization.
--- Renders MediaWiki markup to display the name of a wiki page and an icon.
--  
---
-- Uses MediaWiki markup to display the name of a wiki page and an icon that has
---@param frame table the MediaWiki calling context
-- been uploaded to the wiki.
---@return string wiki markup with the results
-- @param frame a table describing the MediaWiki frame; frame['args'] is a
-- table containg the template arguments; frame['args']['spec'] is the first
-- argument, assigned to the key 'spec'; frame['args']['iconsize'] is the
-- 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)
function SpecializationLink.renderLink(frame)
 
-- extract the arguments we care about from the frame
    -- Extract the template parameters.
local argSpecName = frame.args.spec
    local spec = frame.args.spec or frame.args[1]
local argIconsize = frame.args.iconsize
    local iconSize = frame.args.iconsize
 
-- validate that there's a resource name to use
    if not spec or "" == spec then
if not argSpecName or "" == argSpecName then
        return "The Specialization link template requires the name of a specialization."
return "SpecializationLink error: no resource given"
    end
end
 
    -- Use our local hardcoded data to validate the name provided.
-- simple lookup for a small number of specializations with little extra data
    local iconFilename = SPECIALIZATION_ICON_FILENAMES[spec]
local specLookup = {
    if not iconFilename then
[SPEC_ALCH] = { iconfile="Icon Spec Alchemy 64x64.png" },
        return "No specialization found with that name: " .. spec .. "."
[SPEC_BREW] = { iconfile="Icon Spec Brewing 64x64.png" },
    end
[SPEC_CLOT] = { iconfile="Icon Spec Cloth 64x64.png" },
 
[SPEC_ENGI] = { iconfile="Icon Spec Gear 64x64.png" },
    return renderParts(spec, iconFilename, iconSize)
[SPEC_FARM] = { iconfile="Icon_Spec_Farm_64x64.png" },
 
[SPEC_FORE] = { iconfile="Icon Spec Forest 64x64.png" },
[SPEC_MEAT] = { iconfile="Icon_Spec_Meat_64x64.png" },
[SPEC_RAIN] = { iconfile="Icon Spec RainWater 64x64.png" },
[SPEC_WARM] = { iconfile="Icon_Spec_Fire_64x64.png" },
[SPEC_WOOD] = { iconfile="Icon_Spec_Wood_64x64.png" }
}
-- make sure the argument was a valid name
local specInfo = specLookup[argSpecName]
if not specInfo then
return "SpecializationLink error: no specialization " .. argSpecName .. " found."
end
-- the wiki markup for the internal link to the specializations's wiki page
local linkPart = "[[" .. argSpecName .. " Specialization|" .. argSpecName .. "]]"
-- 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 = specInfo.iconfile
-- combine the size and filename to form the iconPart
local iconPart = "[[File:" .. icon .. "|" .. size .. "|link=" .. argSpecName .. " Specialization|alt=" .. argSpecName .. " Specialization|" .. argSpecName .. " Specialization]]"
-- combine the file part with the link part
return iconPart .. " " .. linkPart
end
end


--endregion


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

Revision as of 00:38, 29 March 2024

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

local SpecializationLink = {}



--region Dependencies

local StyleUtils = require("Module:StyleUtils")

--endregion



--region Private constants

local SPEC_ALCH = "Alchemy"
local SPEC_BREW = "Brewing"
local SPEC_ENGI = "Engineering"
local SPEC_FARM = "Farming"
local SPEC_FORE = "Forest"
local SPEC_MEAT = "Meat production"
local SPEC_RAIN = "Rainwater"
local SPEC_TAIL = "Tailoring"
local SPEC_WARM = "Warmth"
local SPEC_WOOD = "Woodworking"

-- Icon filenames
local SPECIALIZATION_ICON_FILENAMES = {
    [SPEC_ALCH] = "Icon Spec Alchemy 64x64.png",
    [SPEC_BREW] = "Icon Spec Brewing 64x64.png",
    [SPEC_ENGI] = "Icon Spec Gear 64x64.png",
    [SPEC_FARM] = "Icon_Spec_Farm_64x64.png",
    [SPEC_FORE] = "Icon Spec Forest 64x64.png",
    [SPEC_MEAT] = "Icon_Spec_Meat_64x64.png",
    [SPEC_RAIN] = "Icon Spec RainWater 64x64.png",
    [SPEC_TAIL] = "Icon Spec Cloth 64x64.png",
    [SPEC_WARM] = "Icon_Spec_Fire_64x64.png",
    [SPEC_WOOD] = "Icon_Spec_Wood_64x64.png"
}

--endregion



--region Private member variables

--endregion



--region Private methods

local function resolveIconSize(iconSizeRequested)

    -- default for these links is small
    local size = StyleUtils.IMG_S()

    if iconSizeRequested == "med" then
        size = StyleUtils.IMG_M()
    else
        if iconSizeRequested == "large" then
            size = StyleUtils.IMG_L()
        else
            if iconSizeRequested == "huge" then
                size = StyleUtils.IMG_H()
            end
        end
    end

    return size
end



local function makeIconPart(pageName, iconFilename, iconSize)

    -- Skip the icon if none requested. Continue if empty!
    if iconSize == "none" then
        return ""
    end

    local size = resolveIconSize(iconSize)

    return string.format("[[File:%s|%s|link=%s|alt=%s Specialization|%s Specialization]] ", iconFilename, size, pageName, pageName, pageName)
end



local function renderParts(specialization, iconFilename, iconSize)

    local iconPart = makeIconPart(specialization, iconFilename, iconSize)

    local linkPart = "[[" .. specialization .. " Specialization|" .. specialization .. "]]"

    return iconPart .. linkPart
end

--endregion




--region Public methods

--- Renders MediaWiki markup to display the name of a wiki page and an icon.
---
---@param frame table the MediaWiki calling context
---@return string wiki markup with the results
function SpecializationLink.renderLink(frame)

    -- Extract the template parameters.
    local spec = frame.args.spec or frame.args[1]
    local iconSize = frame.args.iconsize

    if not spec or "" == spec then
        return "The Specialization link template requires the name of a specialization."
    end

    -- Use our local hardcoded data to validate the name provided.
    local iconFilename = SPECIALIZATION_ICON_FILENAMES[spec]
    if not iconFilename then
        return "No specialization found with that name: " .. spec .. "."
    end

    return renderParts(spec, iconFilename, iconSize)

end

--endregion

return SpecializationLink