Module:BuildingLink: Difference between revisions
From Against the Storm Official Wiki
updating default icon to be none instead of medium Tag: Reverted |
refactored the parameter logic to be more robust. Tag: Reverted |
||
| Line 20: | Line 20: | ||
--region Private constants | --region Private constants | ||
local VALID_SIZE = { | |||
["none"] = true, | |||
["small"] = true, | |||
["medium"] = true, | |||
["large"] = true, | |||
["huge"] = true, | |||
} | |||
local VALID_PLURAL = { | |||
["s"] = true, | |||
["es"] = true, | |||
} | |||
--endregion | --endregion | ||
| Line 79: | Line 92: | ||
local name = frame.args.name | local name = frame.args.name | ||
local iconSize = frame.args.size | local iconSize = frame.args.size | ||
local display = frame.args.display | local display = frame.args.display | ||
local plural | |||
-- | -- We need to handle the parameters here in Lua, since there is some flexibility afforded to authors. Create a stack FIFO in reverse order and use table.remove to pop the stack. | ||
local paramStack = { frame.args[3], frame.args[2], frame.args[1], } | |||
while #paramStack > 0 do | |||
-- Pop the stack. | |||
local nextParameter = table.remove(paramStack) | |||
-- We likely added nil items to the array, so verify it exists | |||
if nextParameter then | |||
if not name then | |||
name = nextParameter | |||
end | |||
if not size and VALID_SIZE[nextParameter] then | |||
iconSize = nextParameter | |||
end | |||
if not plural and VALID_PLURAL[nextParameter] then | |||
plural = nextParameter | |||
end | |||
end | |||
end | end | ||
-- It is valid by now; the above method already threw an error if not. | -- It is valid by now; the above method already threw an error if not. | ||
validatedIcon = validateBuildingName(name) | validatedIcon = validateBuildingName(name) | ||
-- Provide a default icon size of none. | |||
if not iconSize or not VALID_SIZE[iconSize] then | |||
iconSize = "none" | |||
end | |||
-- The only valid value for mustBePlural is "s," so then make on check to see if name ends in a way that we need to add "es" at the end instead of just "s". For names that end in "y", we swap out the last character for "ies" instead. (This will require one-time setup for redirect pages.) | -- The only valid value for mustBePlural is "s," so then make on check to see if name ends in a way that we need to add "es" at the end instead of just "s". For names that end in "y", we swap out the last character for "ies" instead. (This will require one-time setup for redirect pages.) | ||
if | if plural == "s" or plural == "es" then | ||
if string.match(name, "y$") then | if string.match(name, "y$") then | ||
name = string.sub(name, 1, -2) .. "ies" | name = string.sub(name, 1, -2) .. "ies" | ||
plural = nil -- unset, since it's now incorporated into the name | |||
else | else | ||
local needsSyllable = string.match(name, "[sxz]$") or string.match(name, "sh$") or string.match(name, "ch$") | local needsSyllable = string.match(name, "[sxz]$") or string.match(name, "sh$") or string.match(name, "ch$") | ||
plural = needsSyllable and "es" or "s" | |||
end | end | ||
else | else | ||
plural = nil | |||
end | end | ||
viewParameters = { | viewParameters = { | ||
["name"] = name, | ["name"] = name, | ||
["plural"] = | ["plural"] = plural, | ||
["iconfilename"] = validatedIcon, | ["iconfilename"] = validatedIcon, | ||
["iconsize"] = iconSize, | ["iconsize"] = iconSize, | ||
Revision as of 02:34, 17 October 2024
Documentation for this module may be created at Module:BuildingLink/doc
--- @module BuildingLink
local BuildingLink = {}
--region Dependencies
-- This module lazily loads dependencies, since they're expensive but only one is needed, we just don't know which in advance.
local WorkshopsData
local InstitutionsData
local FarmsData
local CampsData
local CollectorsData
local VIEW_TEMPLATE = "Building_link/view"
--endregion
--region Private constants
local VALID_SIZE = {
["none"] = true,
["small"] = true,
["medium"] = true,
["large"] = true,
["huge"] = true,
}
local VALID_PLURAL = {
["s"] = true,
["es"] = true,
}
--endregion
--region Private methods
local function tryData(name, buildingInterface)
local buildingID = buildingInterface.getID(name)
return buildingInterface.getIcon(buildingID)
end
local function validateBuildingName(name)
-- Lazy-load of external data, since it's expensive. Start with the most likely so that most pages load as fast as possible.
WorkshopsData = require("Module:WorkshopsData")
local validatedIcon = tryData(name, WorkshopsData)
if not validatedIcon then
InstitutionsData = require("Module:InstitutionsData")
validatedIcon = tryData(name, InstitutionsData)
if not validatedIcon then
FarmsData = require("Module:FarmsData")
validatedIcon = tryData(name, FarmsData)
if not validatedIcon then
CollectorsData = require("Module:CollectorsData")
validatedIcon = tryData(name, CollectorsData)
if not validatedIcon then
CampsData = require("Module:CampsData")
validatedIcon = tryData(name, CampsData)
if not validatedIcon then
error("No building found with name: " .. name .. ". Please see the template documentation for how to use the parameters")
end
end
end
end
end
return validatedIcon
end
--endregion
--region Public methods
---main
--- Extracts parameters, validates the building by getting its icon, and then sends the data to the view template for rendering.
---
--- @param frame table the template's context, with arguments
--- @return string wiki markup
function BuildingLink.main(frame)
local name = frame.args.name
local iconSize = frame.args.size
local display = frame.args.display
local plural
-- We need to handle the parameters here in Lua, since there is some flexibility afforded to authors. Create a stack FIFO in reverse order and use table.remove to pop the stack.
local paramStack = { frame.args[3], frame.args[2], frame.args[1], }
while #paramStack > 0 do
-- Pop the stack.
local nextParameter = table.remove(paramStack)
-- We likely added nil items to the array, so verify it exists
if nextParameter then
if not name then
name = nextParameter
end
if not size and VALID_SIZE[nextParameter] then
iconSize = nextParameter
end
if not plural and VALID_PLURAL[nextParameter] then
plural = nextParameter
end
end
end
-- It is valid by now; the above method already threw an error if not.
validatedIcon = validateBuildingName(name)
-- Provide a default icon size of none.
if not iconSize or not VALID_SIZE[iconSize] then
iconSize = "none"
end
-- The only valid value for mustBePlural is "s," so then make on check to see if name ends in a way that we need to add "es" at the end instead of just "s". For names that end in "y", we swap out the last character for "ies" instead. (This will require one-time setup for redirect pages.)
if plural == "s" or plural == "es" then
if string.match(name, "y$") then
name = string.sub(name, 1, -2) .. "ies"
plural = nil -- unset, since it's now incorporated into the name
else
local needsSyllable = string.match(name, "[sxz]$") or string.match(name, "sh$") or string.match(name, "ch$")
plural = needsSyllable and "es" or "s"
end
else
plural = nil
end
viewParameters = {
["name"] = name,
["plural"] = plural,
["iconfilename"] = validatedIcon,
["iconsize"] = iconSize,
["display"] = display,
}
return frame:expandTemplate{
title = VIEW_TEMPLATE,
args = viewParameters,
}
end
--endregion
return BuildingLink


