Module:ResourceLink: Difference between revisions
From Against the Storm Official Wiki
(forcing transclusion might work?) |
(simplified some logic and streamlined variable names. now points to specific resource data file) |
||
Line 9: | Line 9: | ||
local ResourceLink = {} | local ResourceLink = {} | ||
local | 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) | function ResourceLink.renderLink(frame) | ||
local | local argResourceName = frame.args.image | ||
local | local argResourceIconSize = frame.args.size | ||
-- if looking up the provided | -- get the data about the resource and then adopt the provided name | ||
if not | -- in case we need to look it up again | ||
return "renderIcon Error: file " .. | 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 "renderIcon Error: file " .. argResourceName .." 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 | ||
-- small, or in-line size | -- small, or in-line size, for resources | ||
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 use the default, small size | |||
local strIconSize = switchIconSize[argResourceIconSize] or frame:expandTemplate{title=TEMPLATE_IMGSMALL} | |||
local | |||
-- | -- 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 when required into another Module. | |||
------------------------------------------------------------------------------- | |||
return ResourceLink | return ResourceLink |
Revision as of 18:58, 3 February 2023
Documentation for this module may be created at Module:ResourceLink/doc
------------------------------------------------------------------------------- -- 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.image local argResourceIconSize = frame.args.size -- 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 "renderIcon Error: file " .. 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