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 IconFilenames = require("Module:IconFilenames") -- lookup table for image filenames
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 strResourceName = frame.args.image
local argResourceName = frame.args.image
local strIconSize = frame.args.size
local argResourceIconSize = frame.args.size
local strFilename = IconFilenames.getIconFilename(strResourceName)
    
    
-- if looking up the provided image name returned nil, then render an error
-- get the data about the resource and then adopt the provided name
if not strFilename then
-- in case we need to look it up again
return "renderIcon Error: file " .. strResourceName .." not found"
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
 
-- render the string to return, starting with the file tag, then the link tag
-- 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 strFileSize = frame:expandTemplate{title="ImgS"}
local switchIconSize = {
if "med" == strIconSize then
["med"] = frame:expandTemplate{title=TEMPLATE_IMGMED},
strFileSize = frame:expandTemplate{title="ImgM"}
["large"] = frame:expandTemplate{title=TEMPLATE_IMGLARGE},
elseif "large" == strIconSize then
["huge"] = frame:expandTemplate{title=TEMPLATE_IMGHUGE}
strFileSize = frame:expandTemplate{title="ImgL"}
}
elseif "huge" == strIconSize then
-- if it's not in the switch, then use the default, small size
strFileSize = frame:expandTemplate{title="ImgH"}
local strIconSize = switchIconSize[argResourceIconSize] or frame:expandTemplate{title=TEMPLATE_IMGSMALL}
end
local strFilePart = string.format("[[File:%s|%s]]", strFilename, strFileSize)
-- build the link based on the name of the resource passed in
-- combine the string parts to return to the page
local strLinkPart = "[[" .. strResourceName .. "]]"
local strFilePart = string.format("[[File:%s|%s|link=%s|alt=%s|%s]]", strIconFilename, strIconSize, strPageName, strPageName, strPageName)
return strFilePart .. " " .. strLinkPart
-- 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