Module:BuildingLink: Difference between revisions
From Against the Storm Official Wiki
(Changed NBSP to regular space) |
(New version of building links using external view and with more flexibility) |
||
Line 1: | Line 1: | ||
--- @module BuildingLink | --- @module BuildingLink | ||
local BuildingLink = {} | local BuildingLink = {} | ||
Line 24: | Line 4: | ||
--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 | |||
--endregion | --endregion | ||
Line 49: | Line 25: | ||
--region | --region Private methods | ||
local function tryData(name, buildingInterface) | |||
local buildingID = buildingInterface.getID(name) | |||
return buildingInterface.getIcon(buildingID) | |||
end | |||
local function validateBuildingName(name) | |||
local | -- Lazy-load of external data, since it's expensive. Start with the most likely so that most pages load as fast as possible. | ||
if | WorkshopsData = require("Module:WorkshopsData") | ||
local validatedIcon = tryData(name, WorkshopsData) | |||
if not validatedIcon then | |||
InstitutionsData = require("Module:InstitutionsData") | |||
if | validatedIcon = tryData(name, InstitutionsData) | ||
if not validatedIcon then | |||
FarmsData = require("Module:FarmsData") | |||
validatedIcon = tryData(name, FarmsData) | |||
if | if not validatedIcon then | ||
CollectorsData = require("Module:CollectorsData") | |||
validatedIcon = tryData(name, CollectorsData) | |||
if not validatedIcon then | |||
if | CampsData = require("Module:CampsData") | ||
validatedIcon = tryData(name, CampsData) | |||
if not validatedIcon then | |||
error("No building found with name: " .. name .. ".") | |||
if | |||
end | end | ||
end | end | ||
Line 104: | Line 61: | ||
end | end | ||
-- | return validatedIcon | ||
local | 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 needsPlural = frame.args.plural | |||
local display = frame.args.display | |||
-- If the specified | -- If the iconSize was not specified, but plural was, then these will match. We need to unset the iconSize to be default. | ||
if | if iconSize == needsPlural then | ||
iconSize = nil | |||
end | end | ||
-- It is valid by now; the above method already threw an error if not. | |||
validatedIcon = validateBuildingName(name) | |||
-- | -- 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 needsPlural == "s" or needsPlural == "es" then | ||
if string.match(name, "y$") then | |||
name = string.sub(name, 1, -2) .. "ies" | |||
needsPlural = nil -- unset, since it's incorporated into the name | |||
else | |||
local needsSyllable = string.match(name, "[sxz]$") or string.match(name, "sh$") or string.match(name, "ch$") | |||
needsPlural = needsSyllable and "es" or "s" | |||
end | |||
else | |||
needsPlural = nil | |||
end | end | ||
viewParameters = { | |||
["name"] = name, | |||
["plural"] = needsPlural, | |||
["iconfilename"] = validatedIcon, | |||
["iconsize"] = iconSize, | |||
["display"] = display, | |||
} | |||
return frame:expandTemplate{ | |||
title = VIEW_TEMPLATE, | |||
args = viewParameters, | |||
} | |||
end | end | ||
Revision as of 01:45, 15 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 --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 .. ".") 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 needsPlural = frame.args.plural local display = frame.args.display -- If the iconSize was not specified, but plural was, then these will match. We need to unset the iconSize to be default. if iconSize == needsPlural then iconSize = nil end -- It is valid by now; the above method already threw an error if not. validatedIcon = validateBuildingName(name) -- 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 needsPlural == "s" or needsPlural == "es" then if string.match(name, "y$") then name = string.sub(name, 1, -2) .. "ies" needsPlural = nil -- unset, since it's incorporated into the name else local needsSyllable = string.match(name, "[sxz]$") or string.match(name, "sh$") or string.match(name, "ch$") needsPlural = needsSyllable and "es" or "s" end else needsPlural = nil end viewParameters = { ["name"] = name, ["plural"] = needsPlural, ["iconfilename"] = validatedIcon, ["iconsize"] = iconSize, ["display"] = display, } return frame:expandTemplate{ title = VIEW_TEMPLATE, args = viewParameters, } end --endregion return BuildingLink