Module:ResourceLink: Difference between revisions

From Against the Storm Official Wiki
(added error handling when nothing specified)
(added more error handling for invalid input)
Tag: Replaced
Line 1: Line 1:
-------------------------------------------------------------------------------
return "resourceLink Error: " .. argResourceName .." not found"
-- Renders the {{resourceLink}} template
--
-- Takes an argument, the name of the resource. Optionally, accepts a second
-- argument, "med" or "large" to render the icon as larger than the standard
-- size of text, {{ImgS}}
-------------------------------------------------------------------------------
 
local ResourceLink = {}
 
local ResourceData = require("Module:ResourceData") -- lookup table for image filenames
 
 
-------------------------------------------------------------------------------
-- 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 ResourceLink.renderLink(frame)
local argResourceName = frame.args.resource
local argResourceIconSize = frame.args.iconsize
 
if not argResourceName then
return "resourceLink Error: no resource named"
end
 
-- get the data about the resource and then adopt the provided name
-- in case we need to look it up again
local tableData = ResourceData.getData(argResourceName)
local strPageName = tableData.page
-- if looking up the provided resource name returned no page, then render an error
if not strPageName then
return "resourceLink Error: " .. argResourceName .." 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
-- small, or in-line size, for resources
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 use the default, small size
local strIconSize = switchIconSize[argResourceIconSize] or frame:expandTemplate{title=TEMPLATE_IMGSMALL}
-- 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 when required into another Module.
-------------------------------------------------------------------------------
return ResourceLink

Revision as of 19:14, 3 February 2023

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

return "resourceLink Error: " .. argResourceName .." not found"