Module:ResourceLink: Difference between revisions

From Against the Storm Official Wiki
m (renaming product to resource for consistency)
(now with error checking!)
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
-------------------------------------------------------------------------------
--- @module ResourceLink
-- 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 = {}
local ResourceLink = {}


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


--region Dependencies


local GoodsData = require("Module:GoodsData")


--
local VIEW_TEMPLATE = "Resource_link/view"
-- 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
--endregion
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"




--region Private constants


--
local VALID_SIZE = {
-- Main rendering function
["none"] = true,
--
["small"] = true,
["medium"] = true,
["large"] = true,
["huge"] = true,
}


-------------------------------------------------------------------------------
--endregion
-- 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
--region Private methods
-- been uploaded to the wiki. Both must be known by Module:ResourceData, or the
 
-- template's behavior may be different than expected.
---validateResourceName retrieves the icon from the data module.
-- @param frame  a table describing the MediaWiki frame; frame['args'] is a  
---
-- table containg the template arguments; frame['args']['resource'] is the first
---@param name string the display name of a resource
-- argument, assigned to the key 'resource'; frame['args']['iconsize'] is the
---@return string the filename of the icon
-- second argument, assigned to the key 'iconsize'
local function validateResourceName(name)
-- @return a string containing the wiki markup for an icon and an internal wiki
 
-- page link (or an error if a problem occured)
local goodID = GoodsData.getGoodID(name)
function ResourceLink.renderLink(frame)
local validatedIcon = GoodsData.getIcon(goodID)
 
-- extract the arguments we care about from the frame
if not validatedIcon then
local argResourceName = frame.args.resource
error("No resource found with name: " .. name .. ". Please see the template documentation for how to use the parameters")
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
end
 
-- get the page name to make sure there was one
return validatedIcon
local resource = ResourceData.getPagename(argResourceName)
end
if not resource or "" == resource then
 
return "ResourceLink error: " .. argResourceName .." not found"
--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
end
 
-- the wiki markup for the internal link to the resource's wiki page
-- Validate the name.
local linkPart = "[[" .. resource .. "]]"
validatedIcon = validateResourceName(name)
 
-- store the requested size for use in a moment
-- Handle default icon size.
local size = ""
if not iconSize or iconSize == "" or not VALID_SIZE[iconSize] then
iconSize = "small"
-- 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 doesn't exist, we can still continue by substituting
-- The args to pass to the view.
-- a question mark icon instead.
viewParameters = {
local icon = ResourceData.getIconFilename(argResourceName)
["name"] = name,
if not icon or "" == icon then
["iconfilename"] = validatedIcon,
icon = REPLACEMENT_FILENAME
["iconsize"] = iconSize,
end
["display"] = display,
}
-- combine the size and filename to form the iconPart
 
local iconPart = "[[File:" .. icon .. "|" .. size .. "|link=" .. resource .. "|alt=" .. resource .. "|" .. resource .. "]]"
return frame:expandTemplate{
title = VIEW_TEMPLATE,
-- combine the file part with the link part
args = viewParameters,
return iconPart .. " " .. linkPart
}
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