Module:ResourceLink: Difference between revisions

From Against the Storm Official Wiki
(added more error handling for invalid input)
m (improved the commenting; streamlined the logic in the main function)
Line 1: Line 1:
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Renders the {{resourceLink}} template
-- This module renders the {{Resource _link}} template.
-- https://hoodedhorse.com/wiki/Against_the_Storm/Template:Resource_link
--
--
-- Takes an argument, the name of the resource. Optionally, accepts a second  
-- This template #invokes ResourceLink.renderLink(frame), below.
-- argument, "med" or "large" to render the icon as larger than the standard
--
-- size of text, {{ImgS}}
-- 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:ResourceData, which is used to identify the wiki page to link to. It
-- 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
-- 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.
--
-- 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}},
-- {{ImgM}}, etc. The sizes of icons are not stored or known by this module.
-- @module ResourceLink
local ResourceLink = {}


local ResourceLink = {}
--
-- Dependencies
--
local ResourceData = require("Module:ResourceData") -- lookup table for resources


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




-------------------------------------------------------------------------------
--
-- Constants
-- Constants
-------------------------------------------------------------------------------
--
-- names of the templates used to size the icons consistently
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGMED = "ImgM"
Line 20: Line 44:
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"
local REPLACEMENT_FILENAME = "Question_mark.png"






-------------------------------------------------------------------------------
--
-- Main rendering function
-- 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
 
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- 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:ResourceData, or the
-- template's behavior may be different than expected.
-- @param frame  a table describing the MediaWiki frame; frame['args'] is a
-- table containg the template arguments; frame['args']['product'] is the first
-- argument, assigned to the key 'product'; 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)
local argResourceName = frame.args.resource
local argResourceIconSize = frame.args.iconsize
-- extract the arguments we care about from the frame
 
local argResourceName = frame.args.product
if not argResourceName then
local argIconsize = frame.args.iconsize
return "resourceLink Error: no resource named"
-- validate that there's a resource name to use
if not argResourceName or "" == argResourceName then
return "ResourceLink error: no resource given"
end
end
 
-- get the data about the resource and then adopt the provided name
-- get the page name to make sure there was one
-- in case we need to look it up again
local resource = ResourceData.getPagename(argResourceName)
local tableData = ResourceData.getData(argResourceName)
if not resource or "" == resource then
if not tableData then
return "ResourceLink error: " .. argResourceName .." not found"
return "resourceLink Error: " .. argResourceName .." not found"
end
end
local strPageName = tableData.page
-- if looking up the provided resource name returned no page, then render an error
-- the wiki markup for the internal link to the resource's wiki page
if not strPageName then
local linkPart = "[[" .. resource .. "]]"
return "resourceLink Error: " .. argResourceName .." not found"
-- store the requested size for use in a moment
local size = ""
-- Check the requested size against the allowed options. Expand the
-- corresponding template.
if S_MED == argIconsize then
size = frame:expandTemplate{title=TEMPLATE_IMGMED}
elseif S_LRG == argIconsize then
size = frame:expandTemplate{title=TEMPLATE_IMGLARGE}
elseif S_HUG == 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
end
-- if the icon filename didn't exist, then show a default but subtle question mark icon instead
-- If the icon filename doesn't exist, we can still continue by substituting
-- this will help people with troubleshooting, instead of just showing no icon then editors
-- a question mark icon instead.
-- wonder what went wrong
local icon = ResourceData.getIconFilename(argResourceName)
local strIconFilename = tableData.iconfile or REPLACEMENT_FILENAME
if not icon or "" == icon then
icon = REPLACEMENT_FILENAME
end
-- use the established templates for image sizes, with the default being
-- combine the size and filename to form the iconPart
-- small, or in-line size, for resources
local iconPart = "[[File:" .. icon .. "|" .. size .. "|link=" .. resource .. "|alt=" .. resource .. "|" .. resource .. "]]"
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
-- combine the file part with the link part
return strFilePart .. " [[" .. strPageName .. "]]"
return iconPart .. " " .. linkPart
end
end






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

Revision as of 03:57, 14 February 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:ResourceData, which is used to identify the wiki page to link to. It 
-- 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 
-- 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.
--
-- 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}}, 
-- {{ImgM}}, etc. The sizes of icons are not stored or known by this module.
-- @module ResourceLink
local ResourceLink = {}

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



--
-- Constants
--
-- names of the templates used to size the icons consistently
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGLARGE = "ImgL"
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
--

-------------------------------------------------------------------------------
-- 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:ResourceData, or the 
-- template's behavior may be different than expected.
-- @param frame  a table describing the MediaWiki frame; frame['args'] is a 
-- table containg the template arguments; frame['args']['product'] is the first
-- argument, assigned to the key 'product'; 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)
	
	-- extract the arguments we care about from the frame
	local argResourceName = frame.args.product
	local argIconsize = frame.args.iconsize
	
	-- validate that there's a resource name to use
	if not argResourceName or "" == argResourceName then
		return "ResourceLink error: no resource given"
	end
	
	-- get the page name to make sure there was one
	local resource = ResourceData.getPagename(argResourceName)
	if not resource or "" == resource then
		return "ResourceLink error: " .. argResourceName .." not found"
	end
	
	-- the wiki markup for the internal link to the resource's wiki page
	local linkPart = "[[" .. resource .. "]]"
	
	-- store the requested size for use in a moment
	local size = ""
	
	-- Check the requested size against the allowed options. Expand the 
	-- corresponding template.
	if S_MED == argIconsize then
		size = frame:expandTemplate{title=TEMPLATE_IMGMED}
	elseif S_LRG == argIconsize then
		size = frame:expandTemplate{title=TEMPLATE_IMGLARGE}
	elseif S_HUG == 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
	
	-- 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
	
	-- combine the size and filename to form the iconPart
	local iconPart = "[[File:" .. icon .. "|" .. size .. "|link=" .. resource .. "|alt=" .. resource .. "|" .. resource .. "]]"
	
	-- combine the file part with the link part
	return iconPart .. " " .. linkPart
end



-- return when required into another module
return ResourceLink