Module:BuildingLink: Difference between revisions
From Against the Storm Official Wiki
(created) |
(copied over appropriate updates to ResourceLink here) |
||
Line 9: | Line 9: | ||
local BuildingLink = {} | local BuildingLink = {} | ||
local | local BuildingData = require("Module:BuildingData") -- lookup table for buildings | ||
------------------------------------------------------------------------------- | |||
-- Constants | |||
------------------------------------------------------------------------------- | |||
local TEMPLATE_IMGSMALL = "ImgS" | |||
local TEMPLATE_IMGMED = "ImgM" | |||
local TEMPLATE_IMGLARGE = "ImgL" | |||
local TEMPLATE_IMGHUGE = "ImgH" | |||
local REPLACEMENT_FILENAME = "Question_mark.png" | |||
------------------------------------------------------------------------------- | |||
-- Main rendering function | |||
-- uses ResourceData lookup function, parses the result and handles errors | |||
-- with default values, then assembles a final string to return to the wiki | |||
------------------------------------------------------------------------------- | |||
function BuildingLink.renderLink(frame) | function BuildingLink.renderLink(frame) | ||
local | local argBuildingName = frame.args.building | ||
local | local argBuildingIconSize = frame.args.iconsize | ||
local | if not argBuildingName then | ||
return "Building_Link Error: no building given" | |||
-- if looking up the provided | end | ||
if not | |||
return " | -- get the data about the building and then adopt the provided name | ||
-- in case we need to look it up again | |||
local tableData = BuildingData.getData(argBuildingName) | |||
if not tableData then | |||
return "Building_Link Error: " .. argBuildingName .." not found" | |||
end | |||
local strPageName = tableData.page | |||
-- if looking up the provided resource name returned no page, then render an error | |||
if not strPageName then | |||
return "Building_Link Error: " .. argBuildingName .." not found" | |||
end | end | ||
-- | -- if the icon filename didn't exist, then show a default but subtle question mark icon instead | ||
-- this will help people with troubleshooting, instead of just showing no icon then editors | |||
-- wonder what went wrong | |||
local strIconFilename = tableData.iconfile or REPLACEMENT_FILENAME | |||
-- use the established templates for image sizes, with the default being | -- use the established templates for image sizes, with the default being | ||
-- | -- no icon for buildings | ||
local | local switchIconSize = { | ||
["med"] = frame:expandTemplate{title=TEMPLATE_IMGMED}, | |||
["large"] = frame:expandTemplate{title=TEMPLATE_IMGLARGE}, | |||
["huge"] = frame:expandTemplate{title=TEMPLATE_IMGHUGE} | |||
} | |||
-- if it's not in the switch, then skip the file part and just return the link | |||
local strIconSize = switchIconSize[argBuildingIconSize] | |||
if not strIconSize then | |||
return "[[" .. strPageName .. "]]" | |||
end | end | ||
-- | -- combine the string parts to return to the page | ||
local | local strFilePart = string.format("[[File:%s|%s|link=%s|alt=%s|%s]]", strIconFilename, strIconSize, strPageName, strPageName, strPageName) | ||
-- combine the file part with the link part | |||
return strFilePart .. " [[" .. strPageName .. "]]" | |||
end | end | ||
return BuildingLink | return BuildingLink |
Revision as of 19:27, 3 February 2023
Documentation for this module may be created at Module:BuildingLink/doc
------------------------------------------------------------------------------- -- Renders the {{Building_link}} template -- -- Takes an argument, the name of the building. Optionally, accepts a second -- argument, "med" or "large" or "huge" to render an icon when the link is -- used outside of in-line with text. ------------------------------------------------------------------------------- local BuildingLink = {} local BuildingData = require("Module:BuildingData") -- lookup table for buildings ------------------------------------------------------------------------------- -- Constants ------------------------------------------------------------------------------- local TEMPLATE_IMGSMALL = "ImgS" local TEMPLATE_IMGMED = "ImgM" local TEMPLATE_IMGLARGE = "ImgL" local TEMPLATE_IMGHUGE = "ImgH" local REPLACEMENT_FILENAME = "Question_mark.png" ------------------------------------------------------------------------------- -- Main rendering function -- uses ResourceData lookup function, parses the result and handles errors -- with default values, then assembles a final string to return to the wiki ------------------------------------------------------------------------------- function BuildingLink.renderLink(frame) local argBuildingName = frame.args.building local argBuildingIconSize = frame.args.iconsize if not argBuildingName then return "Building_Link Error: no building given" end -- get the data about the building and then adopt the provided name -- in case we need to look it up again local tableData = BuildingData.getData(argBuildingName) if not tableData then return "Building_Link Error: " .. argBuildingName .." not found" end local strPageName = tableData.page -- if looking up the provided resource name returned no page, then render an error if not strPageName then return "Building_Link Error: " .. argBuildingName .." not found" end -- if the icon filename didn't exist, then show a default but subtle question mark icon instead -- this will help people with troubleshooting, instead of just showing no icon then editors -- wonder what went wrong local strIconFilename = tableData.iconfile or REPLACEMENT_FILENAME -- use the established templates for image sizes, with the default being -- no icon for buildings local switchIconSize = { ["med"] = frame:expandTemplate{title=TEMPLATE_IMGMED}, ["large"] = frame:expandTemplate{title=TEMPLATE_IMGLARGE}, ["huge"] = frame:expandTemplate{title=TEMPLATE_IMGHUGE} } -- if it's not in the switch, then skip the file part and just return the link local strIconSize = switchIconSize[argBuildingIconSize] if not strIconSize then return "[[" .. strPageName .. "]]" end -- combine the string parts to return to the page local strFilePart = string.format("[[File:%s|%s|link=%s|alt=%s|%s]]", strIconFilename, strIconSize, strPageName, strPageName, strPageName) -- combine the file part with the link part return strFilePart .. " [[" .. strPageName .. "]]" end return BuildingLink