Module:SpecializationLink

From Against the Storm Official Wiki

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