Module:ResourceLink: Difference between revisions
From Against the Storm Official Wiki
m renaming product to resource for consistency |
Updated to use new GoodsData |
||
| Line 8: | Line 8: | ||
-- Optionally, the template accepts a second argument to change the size of the | -- Optionally, the template accepts a second argument to change the size of the | ||
-- icon that accompanies the text hyperlink. The resource is passed to | -- icon that accompanies the text hyperlink. The resource is passed to | ||
-- Module: | -- Module:GoodsData, which is used to identify the wiki page to link to. | ||
-- | -- | ||
-- Using the data returned, this module creates an icon and the name of the | -- Using the data returned, this module creates an icon and the name of the | ||
| Line 16: | Line 14: | ||
-- size is specified, the icon will default to a size appropriate for display | -- size is specified, the icon will default to a size appropriate for display | ||
-- in-line with other text. | -- in-line with other text. | ||
-- | -- | ||
-- The icons are sized consistently by using existing wiki templates, {{ImgS}}, | -- The icons are sized consistently by using existing wiki templates, {{ImgS}}, | ||
| Line 28: | Line 20: | ||
local ResourceLink = {} | local ResourceLink = {} | ||
-- | --- | ||
-- Dependencies | -- Dependencies | ||
-- | -- | ||
local | local GoodsData = require("Module:GoodsData") | ||
-- | --- | ||
-- Constants | -- Constants | ||
-- | -- | ||
-- | -- Indexes for the data returned from GoodsData. | ||
local INDEX_GOOD_NAME = 2 | |||
local INDEX_GOOD_ICON_FILENAME = 10 | |||
-- Parameter options for icon size. | |||
local SIZE_S = "small" | |||
local SIZE_M = "med" | |||
local SIZE_L = "large" | |||
local SIZE_H = "huge" | |||
-- Use size templates for consistency. | |||
local TEMPLATE_IMGSMALL = "ImgS" | local TEMPLATE_IMGSMALL = "ImgS" | ||
local TEMPLATE_IMGMED = "ImgM" | local TEMPLATE_IMGMED = "ImgM" | ||
local TEMPLATE_IMGLARGE = "ImgL" | local TEMPLATE_IMGLARGE = "ImgL" | ||
local TEMPLATE_IMGHUGE = "ImgH" | local TEMPLATE_IMGHUGE = "ImgH" | ||
-- | --- | ||
-- Main rendering function | -- Main rendering function | ||
-- | -- | ||
-- Renders an icon the name of a resource linked to a wiki page corresponding | -- Renders an icon the name of a resource linked to a wiki page corresponding | ||
-- to the provided resource. | -- to the provided resource. | ||
-- | -- | ||
-- Uses MediaWiki markup to display the name of a wiki page and an icon that has | -- 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: | -- been uploaded to the wiki. Both must be known by Module:GoodsData, or there | ||
-- | -- will be a display problem. | ||
-- @param frame | -- @param frame the template's calling context, with parameters | ||
-- @return wiki markup | |||
-- @return | |||
function ResourceLink.renderLink(frame) | function ResourceLink.renderLink(frame) | ||
-- | -- Extract the template parameters. | ||
local argResourceName = frame.args.resource | local argResourceName = frame.args.resource | ||
local argIconsize = frame.args.iconsize | local argIconsize = frame.args.iconsize | ||
-- | -- Validate that there's a resource name to use | ||
if not argResourceName or "" == argResourceName then | if not argResourceName or "" == argResourceName then | ||
return " | return "The Resource Link template a name of a resource." | ||
end | end | ||
-- | -- Look up the resource from GoodsData | ||
local | local good = GoodsData.getAllDataForGood(argResourceName) | ||
if not | |||
return " | if not good then | ||
return "Cannot find a resource with that name: " .. argResourceName .."." | |||
end | end | ||
local goodName = good[INDEX_GOOD_NAME] | |||
local goodIconFilename = good[INDEX_GOOD_ICON_FILENAME] | |||
-- the wiki markup for the internal link to the resource's wiki page | -- the wiki markup for the internal link to the resource's wiki page | ||
local linkPart = "[[" .. | local linkPart = "[[" .. goodName .. "]]" | ||
-- store the requested size for use in a moment | -- store the requested size for use in a moment | ||
local size = "" | local size = "" | ||
-- | -- Expand the size template for the requested size. | ||
if SIZE_M == argIconsize then | |||
if | |||
size = frame:expandTemplate{title=TEMPLATE_IMGMED} | size = frame:expandTemplate{title=TEMPLATE_IMGMED} | ||
elseif | elseif SIZE_L == argIconsize then | ||
size = frame:expandTemplate{title=TEMPLATE_IMGLARGE} | size = frame:expandTemplate{title=TEMPLATE_IMGLARGE} | ||
elseif | elseif SIZE_H == argIconsize then | ||
size = frame:expandTemplate{title=TEMPLATE_IMGHUGE} | size = frame:expandTemplate{title=TEMPLATE_IMGHUGE} | ||
else | else | ||
-- | -- Default to small size if the argument wasn't valid. | ||
size = frame:expandTemplate{title=TEMPLATE_IMGSMALL} | size = frame:expandTemplate{title=TEMPLATE_IMGSMALL} | ||
end | end | ||
-- combine the size and filename to form the iconPart | -- combine the size and filename to form the iconPart | ||
local iconPart = "[[File:" .. | local iconPart = "[[File:" .. goodIconFilename .. "|" .. size .. "|link=" .. goodName .. "|alt=" .. goodName .. "|" .. goodName .. "]]" | ||
-- combine the file part with the link part | -- combine the file part with the link part | ||
Revision as of 20:04, 12 November 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:GoodsData, which is used to identify the wiki page to link to.
--
-- 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.
--
-- 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 GoodsData = require("Module:GoodsData")
---
-- Constants
--
-- Indexes for the data returned from GoodsData.
local INDEX_GOOD_NAME = 2
local INDEX_GOOD_ICON_FILENAME = 10
-- Parameter options for icon size.
local SIZE_S = "small"
local SIZE_M = "med"
local SIZE_L = "large"
local SIZE_H = "huge"
-- Use size templates for consistency.
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGLARGE = "ImgL"
local TEMPLATE_IMGHUGE = "ImgH"
---
-- 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:GoodsData, or there
-- will be a display problem.
-- @param frame the template's calling context, with parameters
-- @return wiki markup
function ResourceLink.renderLink(frame)
-- Extract the template parameters.
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 "The Resource Link template a name of a resource."
end
-- Look up the resource from GoodsData
local good = GoodsData.getAllDataForGood(argResourceName)
if not good then
return "Cannot find a resource with that name: " .. argResourceName .."."
end
local goodName = good[INDEX_GOOD_NAME]
local goodIconFilename = good[INDEX_GOOD_ICON_FILENAME]
-- the wiki markup for the internal link to the resource's wiki page
local linkPart = "[[" .. goodName .. "]]"
-- store the requested size for use in a moment
local size = ""
-- Expand the size template for the requested size.
if SIZE_M == argIconsize then
size = frame:expandTemplate{title=TEMPLATE_IMGMED}
elseif SIZE_L == argIconsize then
size = frame:expandTemplate{title=TEMPLATE_IMGLARGE}
elseif SIZE_H == 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
-- combine the size and filename to form the iconPart
local iconPart = "[[File:" .. goodIconFilename .. "|" .. size .. "|link=" .. goodName .. "|alt=" .. goodName .. "|" .. goodName .. "]]"
-- combine the file part with the link part
return iconPart .. " " .. linkPart
end
-- return when required into another module
return ResourceLink


