Module:ResourceLink: Difference between revisions

From Against the Storm Official Wiki
(changed indexing to align with updated to GoodsData)
m (fixing duplicate icon extension)
Line 98: Line 98:
-- combine the size and filename to form the iconPart
-- combine the size and filename to form the iconPart
local iconPart = "[[File:" .. goodIconFilename .. ".png|" .. size .. "|link=" .. goodName .. "|alt=" .. goodName .. "|" .. goodName .. "]]"
local iconPart = "[[File:" .. goodIconFilename .. "|" .. size .. "|link=" .. goodName .. "|alt=" .. goodName .. "|" .. goodName .. "]]"
-- combine the file part with the link part
-- combine the file part with the link part

Revision as of 15:39, 17 November 2023

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

-------------------------------------------------------------------------------
-- This module renders the {{Resource _link}} template.
-- https://hoodedhorse.com/wiki/Against_the_Storm/Template:Resource_link
--
-- This template #invokes ResourceLink.renderLink(frame), below.
--
-- The template requires an argument, the name of the resource to which to link.
-- Optionally, the template accepts a second argument to change the size of the
-- icon that accompanies the text hyperlink. The resource is passed to 
-- Module:GoodsData, which is used to identify the wiki page to link to.
--
-- Using the data returned, this module creates an icon and the name of the 
-- resource, and wraps both in a link to the resource's wiki page. If no icon
-- size is specified, the icon will default to a size appropriate for display
-- in-line with other text.
--
-- The icons are sized consistently by using existing wiki templates, {{ImgS}}, 
-- {{ImgM}}, etc. The sizes of icons are not stored or known by this module.
-- @module ResourceLink
local ResourceLink = {}

---
-- Dependencies
--
local GoodsData = require("Module:GoodsData")



---
-- Constants
--
-- Indexes for the data returned from GoodsData.
local INDEX_GOOD_NAME = "displayName"
local INDEX_GOOD_ICON_FILENAME = "iconName"

-- Parameter options for icon size.
local SIZE_S = "small"
local SIZE_M = "med"
local SIZE_L = "large"
local SIZE_H = "huge"
-- Use size templates for consistency.
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGLARGE = "ImgL"
local TEMPLATE_IMGHUGE = "ImgH"



---
-- Main rendering function
--
-- Renders an icon the name of a resource linked to a wiki page corresponding
-- to the provided resource.
-- 
-- Uses MediaWiki markup to display the name of a wiki page and an icon that has 
-- been uploaded to the wiki. Both must be known by Module:GoodsData, or there 
-- will be a display problem.
-- @param frame the template's calling context, with parameters
-- @return wiki markup
function ResourceLink.renderLink(frame)
	
	-- Extract the template parameters.
	local argResourceName = frame.args.resource
	local argIconsize = frame.args.iconsize
	
	-- Validate that there's a resource name to use
	if not argResourceName or "" == argResourceName then
		return "The Resource Link template a name of a resource."
	end
	
	-- Look up the resource from GoodsData
	local good = GoodsData.getAllDataForGood(argResourceName)
	
	if not good then
		return "Cannot find a resource with that name: " .. argResourceName .."."
	end
	
	local goodName = good[INDEX_GOOD_NAME]
	local goodIconFilename = good[INDEX_GOOD_ICON_FILENAME]
	
	-- the wiki markup for the internal link to the resource's wiki page
	local linkPart = "[[" .. goodName .. "]]"
	
	-- store the requested size for use in a moment
	local size = ""
	
	-- Expand the size template for the requested size.
	if SIZE_M == argIconsize then
		size = frame:expandTemplate{title=TEMPLATE_IMGMED}
	elseif SIZE_L == argIconsize then
		size = frame:expandTemplate{title=TEMPLATE_IMGLARGE}
	elseif SIZE_H == argIconsize then
		size = frame:expandTemplate{title=TEMPLATE_IMGHUGE}
	else
		-- Default to small size if the argument wasn't valid.
		size = frame:expandTemplate{title=TEMPLATE_IMGSMALL}
	end
	
	-- combine the size and filename to form the iconPart
	local iconPart = "[[File:" .. goodIconFilename .. "|" .. size .. "|link=" .. goodName .. "|alt=" .. goodName .. "|" .. goodName .. "]]"
	
	-- combine the file part with the link part
	return iconPart .. " " .. linkPart
end



-- return when required into another module
return ResourceLink