Module:ResourceLink: Difference between revisions

From Against the Storm Official Wiki
(created)
 
m (Now uses the correct default size)
 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
-------------------------------------------------------------------------------
---
-- Renders the {{resourceLink}} template
--- This module renders the Resource_link template to draw an icon and link in
--
--- wiki markup.
-- 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
--- The template requires an argument, the name of the resource to which
-- size of text, {{ImgS}}
--- to link. Optionally, the template accepts a second argument to change the
-------------------------------------------------------------------------------
--- size of the icon that accompanies the text hyperlink.
---
--- By default, the icon will default to a small size appropriate for display
--- in-line with other text. For display in tables to help with recognition,
--- use iconsize=med.
---
--- See the template documentation for more information about parameters and
--- errors and to see examples.
---
--- @module ResourceLink
local ResourceLink = {}
 
 
 
local GoodsData = require("Module:GoodsData")


local ResourceLink = {}


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


function p.resourceLink(frame)
--region Private constants
local strImageName = frame.args.image
 
local strImageSize = frame.args.size
local FLAG_NO_ICON = "none"
 
 
local strFilename = IconFilenames.getIconFilename(strImageName)
local DEFAULT_ICON_SIZE_TEMPLATE = "ImgS"
 
local MAP_ICON_SIZE_TEMPLATES = {
-- if looking up the provided image name returned nil, then render an error
["small"] = DEFAULT_ICON_SIZE_TEMPLATE,
if not strFilename then
["med"] = "ImgM",
return "renderIcon Error: file " .. strImageName .." not found"
["large"] = "ImgL",
["huge"] = "ImgH"
}
 
local NBSP = " "
 
--endregion
 
 
 
--region Private methods
 
---
--- Writes the wiki markup for the icon and the link. Uses the private constant
--- map to look up the size template and expands that template to conform to
--- standard size for the wiki.
---
---@param goodName string the name of the goods' page to link to
---@param goodIconFilename string the complete filename to write
---@param iconSize string "none", "small", "med", "large", or "huge"
---@return string wiki markup of icon and hyperlink
local function makeResourceLink(goodName, goodIconFilename, iconSize)
 
local linkPart = "[[" .. goodName .. "]]"
local iconPart = ""
 
if iconSize and iconSize ~= FLAG_NO_ICON then
 
local iconTemplate = MAP_ICON_SIZE_TEMPLATES[iconSize] or DEFAULT_ICON_SIZE_TEMPLATE
local pxSize = mw.getCurrentFrame():expandTemplate{ title=iconTemplate }
 
if goodIconFilename and goodIconFilename ~= "" then
iconPart = "[[File: " .. goodIconFilename .. "|" .. pxSize .. "|link=" .. goodName
.. "|alt=" .. goodName .. "|" .. goodName .. "]]"
.. NBSP
end
 
end
 
return iconPart .. linkPart
end
 
--endregion
 
 
 
--region Public methods
 
---
--- Create a resource link by passing the ID of the resource rather than
--- through the template. Useful for calling from other modules.
---
---@param goodID string the ID of the good to link to
---@param iconSize string "small", "med", "large", or "huge"
---@return string wiki markup of icon and link
function ResourceLink.resourceLinkWithID(goodID, iconSize)
 
-- At runtime, this should never be nil or empty.
if not goodID or goodID == "" then
error("No good specified to which to make a link.")
end
 
local goodName = GoodsData.getGoodNameByID(goodID)
 
if not goodName then
return "No goods found with that ID: " .. goodID .. "."
end
 
local goodIconFilename = GoodsData.getGoodIconByID(goodID)
 
return makeResourceLink(goodName, goodIconFilename, iconSize)
end
 
 
 
---
--- Called from Template:Resource_link to create a link, with an icon, to the
--- page for the specified goods.
---
---@param frame table the Mediawiki context from the template
---@return string wikimarkup representing an icon and link
function ResourceLink.resourceLink(frame)
 
-- Extract template parameters.
local argResource = frame.args["resource"]
local argIconSize = frame.args["iconsize"]
 
if not argResource or argResource == "" then
return "The Resource_link template requires the name of a resource."
end
end


-- render the string to return, starting with the file tag, then the link tag
local goodIconFilename = GoodsData.getGoodIcon(argResource)
if not goodIconFilename then
-- use the established templates for image sizes, with the default being
return "No goods found with that name: " .. argResource .. "."
-- small, or in-line size.
local strFileSize = "{{ImgS}}"
if "med" == strImageSize then
strFileSize = "{{ImgM}}"
elseif "large" == strImageSize then
strFileSize = "{{ImgL}}"
elseif "huge" == strImageSize then
strFileSize = "{{ImgH}}"
end
end
local strFilePart = string.format("[[File:%s|%s]]", strFilename, strImageSize)
 
return makeResourceLink(argResource, goodIconFilename, argIconSize)
-- build the link based on the name of the resource passed in
local strLinkPart = "[[" .. strImageName .. "]]"
return strFilePart .. strLinkPart
end
end
--endregion


return ResourceLink
return ResourceLink

Latest revision as of 22:36, 18 November 2023

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

---
--- This module renders the Resource_link template to draw an icon and link in
--- wiki markup.
---
--- 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.
---
--- By default, the icon will default to a small size appropriate for display
--- in-line with other text. For display in tables to help with recognition,
--- use iconsize=med.
---
--- See the template documentation for more information about parameters and
--- errors and to see examples.
---
--- @module ResourceLink
local ResourceLink = {}



local GoodsData = require("Module:GoodsData")



--region Private constants

local FLAG_NO_ICON = "none"

local DEFAULT_ICON_SIZE_TEMPLATE = "ImgS"
local MAP_ICON_SIZE_TEMPLATES = {
	["small"] = DEFAULT_ICON_SIZE_TEMPLATE,
	["med"] = "ImgM",
	["large"] = "ImgL",
	["huge"] = "ImgH"
}

local NBSP = " "

--endregion



--region Private methods

---
--- Writes the wiki markup for the icon and the link. Uses the private constant
--- map to look up the size template and expands that template to conform to
--- standard size for the wiki.
---
---@param goodName string the name of the goods' page to link to
---@param goodIconFilename string the complete filename to write
---@param iconSize string "none", "small", "med", "large", or "huge"
---@return string wiki markup of icon and hyperlink
local function makeResourceLink(goodName, goodIconFilename, iconSize)

	local linkPart = "[[" .. goodName .. "]]"
	local iconPart = ""

	if iconSize and iconSize ~= FLAG_NO_ICON then

		local iconTemplate = MAP_ICON_SIZE_TEMPLATES[iconSize] or DEFAULT_ICON_SIZE_TEMPLATE
		local pxSize = mw.getCurrentFrame():expandTemplate{ title=iconTemplate }

		if goodIconFilename and goodIconFilename ~= "" then
			iconPart = "[[File: " .. goodIconFilename .. "|" .. pxSize .. "|link=" .. goodName
					.. "|alt=" .. goodName .. "|" .. goodName .. "]]"
					.. NBSP
		end

	end

	return iconPart .. linkPart
end

--endregion



--region Public methods

---
--- Create a resource link by passing the ID of the resource rather than
--- through the template. Useful for calling from other modules.
---
---@param goodID string the ID of the good to link to
---@param iconSize string "small", "med", "large", or "huge"
---@return string wiki markup of icon and link
function ResourceLink.resourceLinkWithID(goodID, iconSize)

	-- At runtime, this should never be nil or empty.
	if not goodID or goodID == "" then
		error("No good specified to which to make a link.")
	end

	local goodName = GoodsData.getGoodNameByID(goodID)

	if not goodName then
		return "No goods found with that ID: " .. goodID .. "."
	end

	local goodIconFilename = GoodsData.getGoodIconByID(goodID)

	return makeResourceLink(goodName, goodIconFilename, iconSize)
end



---
--- Called from Template:Resource_link to create a link, with an icon, to the
--- page for the specified goods.
---
---@param frame table the Mediawiki context from the template
---@return string wikimarkup representing an icon and link
function ResourceLink.resourceLink(frame)

	-- Extract template parameters.
	local argResource = frame.args["resource"]
	local argIconSize = frame.args["iconsize"]

	if not argResource or argResource == "" then
		return "The Resource_link template requires the name of a resource."
	end

	local goodIconFilename = GoodsData.getGoodIcon(argResource)
	if not goodIconFilename then
		return "No goods found with that name: " .. argResource .. "."
	end

	return makeResourceLink(argResource, goodIconFilename, argIconSize)
end

--endregion

return ResourceLink