Module:ResourceLink: Difference between revisions

From Against the Storm Official Wiki
m (renaming product to resource for consistency)
m (Now uses the correct default size)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
-------------------------------------------------------------------------------
---
-- This module renders the {{Resource _link}} template.
--- This module renders the Resource_link template to draw an icon and link in
-- https://hoodedhorse.com/wiki/Against_the_Storm/Template:Resource_link
--- wiki markup.
--
---
-- 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
-- The template requires an argument, the name of the resource to which to link.
--- size of the icon that accompanies the text hyperlink.
-- Optionally, the template accepts a second argument to change the size of the
---
-- icon that accompanies the text hyperlink. The resource is passed to
--- By default, the icon will default to a small size appropriate for display
-- Module:ResourceData, which is used to identify the wiki page to link to. It
--- in-line with other text. For display in tables to help with recognition,
-- also handles some basic normalization to tolerate small mistakes in spelling,
--- use iconsize=med.
-- spacing, or punctuation.
---
--
--- See the template documentation for more information about parameters and
-- Using the data returned, this module creates an icon and the name of the
--- errors and to see examples.
-- 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
--- @module ResourceLink
-- 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 GoodsData = require("Module:GoodsData")


--
-- 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
--region Private constants
local REPLACEMENT_FILENAME = "Question_mark.png"


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 = " "
-- Main rendering function
 
--
--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


-------------------------------------------------------------------------------
-- 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']['resource'] is the first
-- 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)
-- extract the arguments we care about from the frame
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 "ResourceLink error: no resource given"
end
end
 
-- get the page name to make sure there was one
return iconPart .. linkPart
local resource = ResourceData.getPagename(argResourceName)
end
if not resource or "" == resource then
 
return "ResourceLink error: " .. argResourceName .." not found"
--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
end
 
-- the wiki markup for the internal link to the resource's wiki page
local goodName = GoodsData.getGoodNameByID(goodID)
local linkPart = "[[" .. resource .. "]]"
 
if not goodName then
-- store the requested size for use in a moment
return "No goods found with that ID: " .. goodID .. "."
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 doesn't exist, we can still continue by substituting
local goodIconFilename = GoodsData.getGoodIconByID(goodID)
-- a question mark icon instead.
 
local icon = ResourceData.getIconFilename(argResourceName)
return makeResourceLink(goodName, goodIconFilename, iconSize)
if not icon or "" == icon then
end
icon = REPLACEMENT_FILENAME
 
 
 
---
--- 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
 
-- combine the size and filename to form the iconPart
local goodIconFilename = GoodsData.getGoodIcon(argResource)
local iconPart = "[[File:" .. icon .. "|" .. size .. "|link=" .. resource .. "|alt=" .. resource .. "|" .. resource .. "]]"
if not goodIconFilename then
return "No goods found with that name: " .. argResource .. "."
-- combine the file part with the link part
end
return iconPart .. " " .. linkPart
 
return makeResourceLink(argResource, goodIconFilename, argIconSize)
end
end


--endregion


-- return when required into another module
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