Module:ResourceLink: Difference between revisions

From Against the Storm Official Wiki
(simplified some logic and streamlined variable names. now points to specific resource data file)
m (Now uses the correct default size)
 
(13 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")
 
 
 
--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


local ResourceLink = {}
end
 
return iconPart .. linkPart
end
 
--endregion


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




-------------------------------------------------------------------------------
--region Public methods
-- Constants
-------------------------------------------------------------------------------
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGLARGE = "ImgL"
local TEMPLATE_IMGHUGE = "ImgH"


local REPLACEMENT_FILENAME = "Question_mark.png"
---
--- 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
-- Main rendering function
return "No goods found with that ID: " .. goodID .. "."
-- uses ResourceData lookup function, parses the result and handles errors
-- with default values, then assembles a final string to return to the wiki
-------------------------------------------------------------------------------
function ResourceLink.renderLink(frame)
local argResourceName = frame.args.image
local argResourceIconSize = frame.args.size
 
-- get the data about the resource and then adopt the provided name
-- in case we need to look it up again
local tableData = ResourceData.getData(argResourceName)
local strPageName = tableData.page
-- if looking up the provided resource name returned no page, then render an error
if not strPageName then
return "renderIcon Error: file " .. argResourceName .." not found"
end
end
 
-- if the icon filename didn't exist, then show a default but subtle question mark icon instead
local goodIconFilename = GoodsData.getGoodIconByID(goodID)
-- this will help people with troubleshooting, instead of just showing no icon then editors
 
-- wonder what went wrong
return makeResourceLink(goodName, goodIconFilename, iconSize)
local strIconFilename = tableData.iconfile or REPLACEMENT_FILENAME
-- use the established templates for image sizes, with the default being
-- small, or in-line size, for resources
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
return strFilePart .. " [[" .. strPageName .. "]]"
end
end






-------------------------------------------------------------------------------
---
-- Return when required into another Module.
--- 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
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