Module:ResourceLink: Difference between revisions

From Against the Storm Official Wiki
(simplified some logic and streamlined variable names. now points to specific resource data file)
(now with error checking!)
 
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
-------------------------------------------------------------------------------
--- @module ResourceLink
-- Renders the {{resourceLink}} template
local ResourceLink = {}
--
 
-- 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
--region Dependencies
-- size of text, {{ImgS}}
 
-------------------------------------------------------------------------------
local GoodsData = require("Module:GoodsData")
 
local VIEW_TEMPLATE = "Resource_link/view"
 
--endregion
 
 
 
--region Private constants
 
local VALID_SIZE = {
["none"] = true,
["small"] = true,
["medium"] = true,
["large"] = true,
["huge"] = true,
}
 
--endregion
 
 
 
--region Private methods
 
---validateResourceName retrieves the icon from the data module.
---
---@param name string the display name of a resource
---@return string the filename of the icon
local function validateResourceName(name)
 
local goodID = GoodsData.getGoodID(name)
local validatedIcon = GoodsData.getIcon(goodID)
 
if not validatedIcon then
error("No resource found with name: " .. name .. ". Please see the template documentation for how to use the parameters")
end
 
return validatedIcon
end
 
--endregion


local ResourceLink = {}


local ResourceData = require("Module:ResourceData") -- lookup table for image filenames


--region Public methods


-------------------------------------------------------------------------------
---main is called from the template to create a link, with an icon, to the page for the specified goods.
-- Constants
---
-------------------------------------------------------------------------------
---@param frame table the Mediawiki template context
local TEMPLATE_IMGSMALL = "ImgS"
---@return string wikimarkup generated by the view template
local TEMPLATE_IMGMED = "ImgM"
function ResourceLink.main(frame)
local TEMPLATE_IMGLARGE = "ImgL"
local TEMPLATE_IMGHUGE = "ImgH"


local REPLACEMENT_FILENAME = "Question_mark.png"
local name = frame.args.name
local iconSize = frame.args.size
local display = frame.args.display


if not name or name == "" then
error("You must specify a resource. Please see the template documentation for how to use the parameters")
end


-- Validate the name.
validatedIcon = validateResourceName(name)


-------------------------------------------------------------------------------
-- Handle default icon size.
-- Main rendering function
if not iconSize or iconSize == "" or not VALID_SIZE[iconSize] then
-- uses ResourceData lookup function, parses the result and handles errors
iconSize = "small"
-- 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
end
 
-- if the icon filename didn't exist, then show a default but subtle question mark icon instead
-- The args to pass to the view.
-- this will help people with troubleshooting, instead of just showing no icon then editors
viewParameters = {
-- wonder what went wrong
["name"] = name,
local strIconFilename = tableData.iconfile or REPLACEMENT_FILENAME
["iconfilename"] = validatedIcon,
["iconsize"] = iconSize,
-- use the established templates for image sizes, with the default being
["display"] = display,
-- small, or in-line size, for resources
}
local switchIconSize = {
 
["med"] = frame:expandTemplate{title=TEMPLATE_IMGMED},
return frame:expandTemplate{
["large"] = frame:expandTemplate{title=TEMPLATE_IMGLARGE},
title = VIEW_TEMPLATE,
["huge"] = frame:expandTemplate{title=TEMPLATE_IMGHUGE}
args = viewParameters,
}
}
-- 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
end


--endregion


-------------------------------------------------------------------------------
-- Return when required into another Module.
-------------------------------------------------------------------------------
return ResourceLink
return ResourceLink

Latest revision as of 02:44, 19 October 2024

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

--- @module ResourceLink
local ResourceLink = {}


--region Dependencies

local GoodsData = require("Module:GoodsData")

local VIEW_TEMPLATE = "Resource_link/view"

--endregion



--region Private constants

local VALID_SIZE = {
	["none"] = true,
	["small"] = true,
	["medium"] = true,
	["large"] = true,
	["huge"] = true,
}

--endregion



--region Private methods

---validateResourceName retrieves the icon from the data module.
---
---@param name string the display name of a resource
---@return string the filename of the icon
local function validateResourceName(name)

	local goodID = GoodsData.getGoodID(name)
	local validatedIcon = GoodsData.getIcon(goodID)

	if not validatedIcon then
		error("No resource found with name: " .. name .. ". Please see the template documentation for how to use the parameters")
	end

	return validatedIcon
end

--endregion



--region Public methods

---main is called from the template to create a link, with an icon, to the page for the specified goods.
---
---@param frame table the Mediawiki template context
---@return string wikimarkup generated by the view template
function ResourceLink.main(frame)

	local name = frame.args.name
	local iconSize = frame.args.size
	local display = frame.args.display

	if not name or name == "" then
		error("You must specify a resource. Please see the template documentation for how to use the parameters")
	end

	-- Validate the name.
	validatedIcon = validateResourceName(name)

	-- Handle default icon size.
	if not iconSize or iconSize == "" or not VALID_SIZE[iconSize] then
		iconSize = "small"
	end

	-- The args to pass to the view.
	viewParameters = {
		["name"] = name,
		["iconfilename"] = validatedIcon,
		["iconsize"] = iconSize,
		["display"] = display,
	}

	return frame:expandTemplate{
		title = VIEW_TEMPLATE,
		args = viewParameters,
	}
end

--endregion

return ResourceLink