Module:ResourceLink: Difference between revisions

From Against the Storm Official Wiki
m (renaming product to resource for consistency)
(Updated to use new GoodsData)
Line 8: Line 8:
-- Optionally, the template accepts a second argument to change the size of the
-- Optionally, the template accepts a second argument to change the size of the
-- icon that accompanies the text hyperlink. The resource is passed to  
-- icon that accompanies the text hyperlink. The resource is passed to  
-- Module:ResourceData, which is used to identify the wiki page to link to. It
-- Module:GoodsData, which is used to identify the wiki page to link to.
-- also handles some basic normalization to tolerate small mistakes in spelling,
-- spacing, or punctuation.
--
--
-- Using the data returned, this module creates an icon and the name of the  
-- Using the data returned, this module creates an icon and the name of the  
Line 16: Line 14:
-- size is specified, the icon will default to a size appropriate for display
-- size is specified, the icon will default to a size appropriate for display
-- in-line with other text.
-- in-line with other text.
--
-- IF there is a known resource that does not have an icon (might be the case if
-- the data is being updated), then a question-mark icon will take the place of
-- the resource's icon. This is so that people using this template will not
-- mistake a missing icon for an issue with this template and can efficienty
-- troubleshoot.
--
--
-- The icons are sized consistently by using existing wiki templates, {{ImgS}},  
-- The icons are sized consistently by using existing wiki templates, {{ImgS}},  
Line 28: Line 20:
local ResourceLink = {}
local ResourceLink = {}


--
---
-- Dependencies
-- Dependencies
--
--
local ResourceData = require("Module:ResourceData") -- lookup table for resources
local GoodsData = require("Module:GoodsData")






--
---
-- Constants
-- Constants
--
--
-- names of the templates used to size the icons consistently
-- Indexes for the data returned from GoodsData.
local INDEX_GOOD_NAME = 2
local INDEX_GOOD_ICON_FILENAME = 10
 
-- 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_IMGSMALL = "ImgS"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGLARGE = "ImgL"
local TEMPLATE_IMGLARGE = "ImgL"
local TEMPLATE_IMGHUGE = "ImgH"
local TEMPLATE_IMGHUGE = "ImgH"
-- string options for icon size arguments
local S_SML = "small"
local S_MED = "med"
local S_LRG = "large"
local S_HUG = "huge"
-- filename to use if there is no known icon for a known building
local REPLACEMENT_FILENAME = "Question_mark.png"






--
---
-- Main rendering function
-- Main rendering function
--
--
-------------------------------------------------------------------------------
-- Renders an icon the name of a resource linked to a wiki page corresponding
-- Renders an icon the name of a resource linked to a wiki page corresponding
-- to the provided resource.
-- to the provided resource.
--  
--  
-- Uses MediaWiki markup to display the name of a wiki page and an icon that has  
-- 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:ResourceData, or the
-- been uploaded to the wiki. Both must be known by Module:GoodsData, or there
-- template's behavior may be different than expected.
-- will be a display problem.
-- @param frame a table describing the MediaWiki frame; frame['args'] is a
-- @param frame the template's calling context, with parameters
-- table containg the template arguments; frame['args']['resource'] is the first
-- @return wiki markup
-- argument, assigned to the key 'resource'; frame['args']['iconsize'] is the
-- second argument, assigned to the key 'iconsize'
-- @return a string containing the wiki markup for an icon and an internal wiki
-- page link (or an error if a problem occured)
function ResourceLink.renderLink(frame)
function ResourceLink.renderLink(frame)
-- extract the arguments we care about from the frame
-- Extract the template parameters.
local argResourceName = frame.args.resource
local argResourceName = frame.args.resource
local argIconsize = frame.args.iconsize
local argIconsize = frame.args.iconsize
-- validate that there's a resource name to use
-- Validate that there's a resource name to use
if not argResourceName or "" == argResourceName then
if not argResourceName or "" == argResourceName then
return "ResourceLink error: no resource given"
return "The Resource Link template a name of a resource."
end
end
-- get the page name to make sure there was one
-- Look up the resource from GoodsData
local resource = ResourceData.getPagename(argResourceName)
local good = GoodsData.getAllDataForGood(argResourceName)
if not resource or "" == resource then
return "ResourceLink error: " .. argResourceName .." not found"
if not good then
return "Cannot find a resource with that name: " .. argResourceName .."."
end
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
-- the wiki markup for the internal link to the resource's wiki page
local linkPart = "[[" .. resource .. "]]"
local linkPart = "[[" .. goodName .. "]]"
-- store the requested size for use in a moment
-- store the requested size for use in a moment
local size = ""
local size = ""
-- Check the requested size against the allowed options. Expand the
-- Expand the size template for the requested size.
-- corresponding template.
if SIZE_M == argIconsize then
if S_MED == argIconsize then
size = frame:expandTemplate{title=TEMPLATE_IMGMED}
size = frame:expandTemplate{title=TEMPLATE_IMGMED}
elseif S_LRG == argIconsize then
elseif SIZE_L == argIconsize then
size = frame:expandTemplate{title=TEMPLATE_IMGLARGE}
size = frame:expandTemplate{title=TEMPLATE_IMGLARGE}
elseif S_HUG == argIconsize then
elseif SIZE_H == argIconsize then
size = frame:expandTemplate{title=TEMPLATE_IMGHUGE}
size = frame:expandTemplate{title=TEMPLATE_IMGHUGE}
else
else
-- default to small size if the argument wasn't valid
-- Default to small size if the argument wasn't valid.
size = frame:expandTemplate{title=TEMPLATE_IMGSMALL}
size = frame:expandTemplate{title=TEMPLATE_IMGSMALL}
end
-- If the icon filename doesn't exist, we can still continue by substituting
-- a question mark icon instead.
local icon = ResourceData.getIconFilename(argResourceName)
if not icon or "" == icon then
icon = REPLACEMENT_FILENAME
end
end
-- combine the size and filename to form the iconPart
-- combine the size and filename to form the iconPart
local iconPart = "[[File:" .. icon .. "|" .. size .. "|link=" .. resource .. "|alt=" .. resource .. "|" .. resource .. "]]"
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 20:04, 12 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 = 2
local INDEX_GOOD_ICON_FILENAME = 10

-- 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