Module:BuildingLink: Difference between revisions

From Against the Storm Official Wiki
m (fixed a couple weird comments)
(Updated to use new WorkshopsData)
Line 7: Line 7:
-- The template requires an argument, the name of the building to which to link.
-- The template requires an argument, the name of the building to which to link.
-- Optionally, the template accepts a second argument to render an icon next to  
-- Optionally, the template accepts a second argument to render an icon next to  
-- the link. The building is passed to Module:BuildingData, which is used to
-- the link. The building is passed to Module:WorkshopsData, which is used to
-- identify the wiki page to link to. It also handles some basic normalization
-- identify the wiki page to link to.
-- to tolerate some small mistakes in spelling, spacing, or punctuation.
--
--
-- 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 15: Line 14:
-- size is specified, only the name of the building will be rendered by the
-- size is specified, only the name of the building will be rendered by the
-- template.
-- template.
--
-- If there is a known building 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 building'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}},
-- The icons are sized consistently by using existing wiki templates, {{ImgS}},
Line 27: Line 20:
local BuildingLink = {}
local BuildingLink = {}


--
---
-- Dependencies
-- Dependencies
--
--
local BuildingData = require("Module:BuildingData") -- lookup table for buildings
local WorkshopsData = require("Module:WorkshopsData")




Line 37: Line 30:
-- Constants
-- Constants
--
--
-- names of the templates used to size the icons consistently
-- Indexes for accessing the data retrieved from WorkshopsData
local INDEX_ICON_PREFIX = 1
local INDEX_WORKSHOP_NAME = 2
 
local ICONFILENAME_SUFFIX = "_icon"
 
-- 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"
Line 43: Line 47:
local TEMPLATE_IMGHUGE = "ImgH"
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
local REPLACEMENT_FILENAME = "Question_mark.png"


 
---
 
--
-- Main rendering function
-- Main rendering function
--
--
-------------------------------------------------------------------------------
-- Renders an icon the name of a building linked to a wiki page corresponding
-- Renders an icon the name of a building linked to a wiki page corresponding
-- to the provided building.
-- to the provided building.
Line 65: Line 58:
-- been uploaded to the wiki. Both must be known by Module:BuildingData, or the
-- been uploaded to the wiki. Both must be known by Module:BuildingData, or the
-- template's behavior may be different than expected.
-- template's behavior may be different than expected.
-- @param frame a table describing the MediaWiki frame; frame['args'] is a
-- @param frame the template's calling context, with parameters
-- table containg the template arguments; frame['args']['building'] is the first
-- @return wiki markup
-- argument, assigned to the key 'building'; 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 BuildingLink.renderLink(frame)
function BuildingLink.renderLink(frame)
-- extract the arguments we care about from the frame
-- Extract the template parameters.
local argBuildingName = frame.args.building
local argBuildingName = frame.args.building
local argIconsize = frame.args.iconsize
local argIconsize = frame.args.iconsize
-- validate that there's a building name to use
-- Validate that there's a name to use
if not argBuildingName or "" == argBuildingName then
if not argBuildingName or "" == argBuildingName then
return "BuildingLink error: no building given"
return "The Building Link template a name of a building."
end
end
-- get the page name to make sure there was one
-- Look it up from the data source
local building = BuildingData.getPagename(argBuildingName)
local workshop = WorkshopsData.getAllDataForWorkshop(argBuildingName)
if not building or "" == building then
return "BuildingLink error: " .. argBuildingName .." not found"
if not workshop then
return "Cannot find a building with that name: " .. argBuildingName .. "."
end
end
-- the wiki markup for the internal link to the building's wiki page
local workshopName = workshop[INDEX_WORKSHOP_NAME]
local linkPart = "[[" .. building .. "]]"
local workshopIconFilename = workshop[INDEX_ICON_PREFIX] .. ICONFILENAME_SUFFIX
-- If there's no icon size specified, or if it's specified to be small
-- (which is too small for building icons), then we can return the wiki link
-- right away.
-- This is the default behavior.
if not argIconsize or "" == argIconsize or "small" == argIconsize then
return linkPart
end
--
-- Wiki link to the building's page just uses the in-game name.
-- At this point, an icon size is requested.
local linkPart = "[[" .. workshopName .. "]]"
--
-- store the requested size for use in a moment
-- store the requested size for use in a moment
Line 108: Line 89:
-- Check the requested size against the allowed options. Expand the  
-- Check the requested size against the allowed options. Expand the  
-- corresponding template.
-- corresponding template.
if S_MED == argIconsize then
if SIZE_M == argIconsize then
size = frame:expandTemplate{title=TEMPLATE_IMGMED}
size = frame:expandTemplate{title=TEMPLATE_IMGMED}
elseif S_LRG == argIconsize then
elseif SIZE_L == argIconsize then
size = frame:expandTemplate{title=TEMPLATE_IMGLARGE}
size = frame:expandTemplate{title=TEMPLATE_IMGLARGE}
elseif S_HUG == argIconsize then
elseif SIZE_H == argIconsize then
size = frame:expandTemplate{title=TEMPLATE_IMGHUGE}
size = frame:expandTemplate{title=TEMPLATE_IMGHUGE}
else
else
-- However, if the argument did not match one of the valid sizes, then
-- However, if the argument did not match one of the valid sizes, or if
-- we just return the link itself, with no icon.
-- it's just SIZE_S, then we just return the link itself, with no icon.
return linkPart
return linkPart
end
-- If the icon filename doesn't exist, we can still continue by substituting
-- a question mark icon instead.
local icon = BuildingData.getIconFilename(argBuildingName)
if not icon or "" == icon then
icon = REPLACEMENT_FILENAME
end
end
-- combine the size and filename to form the iconPart
-- combine the size and filename to form the iconPart
local iconPart = "[[File:" .. icon .. "|" .. size .. "|link=" .. building .. "|alt=" .. building .. "|" .. building .. "]]"
local iconPart = "[[File:" .. workshopIconFilename .. ".png|" .. size .. "|link=" .. workshopName .. "|alt=" .. workshopName .. "|" .. workshopName .. "]]"
-- combine the file part with the link part, separated by one space
-- combine the file part with the link part
return iconPart .. " " .. linkPart
return iconPart .. " " .. linkPart
end
end

Revision as of 21:03, 12 November 2023

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

-------------------------------------------------------------------------------
-- This module renders the {{Building_link}} template.
-- https://hoodedhorse.com/wiki/Against_the_Storm/Template:Building_link
--
-- The template #invokes BuildingLink.renderLink(frame), below.
--
-- The template requires an argument, the name of the building to which to link.
-- Optionally, the template accepts a second argument to render an icon next to 
-- the link. The building is passed to Module:WorkshopsData, 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 
-- building, and wraps both in a link to the building's wiki page. If no icon
-- size is specified, only the name of the building will be rendered by the
-- template.
--
-- The icons are sized consistently by using existing wiki templates, {{ImgS}},
-- {{ImgM}}, etc. The sizes of the icons are not stored or known by this module.
-- @module BuildingLink
local BuildingLink = {}

---
-- Dependencies
--
local WorkshopsData = require("Module:WorkshopsData")



--
-- Constants
--
-- Indexes for accessing the data retrieved from WorkshopsData
local INDEX_ICON_PREFIX = 1
local INDEX_WORKSHOP_NAME = 2

local ICONFILENAME_SUFFIX = "_icon"

-- 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 building linked to a wiki page corresponding
-- to the provided building.
--
-- 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:BuildingData, or the
-- template's behavior may be different than expected.
-- @param frame the template's calling context, with parameters
-- @return wiki markup
function BuildingLink.renderLink(frame)
	
	-- Extract the template parameters.
	local argBuildingName = frame.args.building
	local argIconsize = frame.args.iconsize
	
	-- Validate that there's a name to use
	if not argBuildingName or "" == argBuildingName then
		return "The Building Link template a name of a building."
	end
	
	-- Look it up from the data source
	local workshop = WorkshopsData.getAllDataForWorkshop(argBuildingName)
	
	if not workshop then
		return "Cannot find a building with that name: " .. argBuildingName .. "."
	end
	
	local workshopName = workshop[INDEX_WORKSHOP_NAME]
	local workshopIconFilename = workshop[INDEX_ICON_PREFIX] .. ICONFILENAME_SUFFIX
	
	-- Wiki link to the building's page just uses the in-game name.
	local linkPart = "[[" .. workshopName .. "]]"
	
	-- store the requested size for use in a moment
	local size = ""
	
	-- Check the requested size against the allowed options. Expand the 
	-- corresponding template.
	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
		-- However, if the argument did not match one of the valid sizes, or if
		-- it's just SIZE_S, then we just return the link itself, with no icon.
		return linkPart
	end
	
	-- combine the size and filename to form the iconPart
	local iconPart = "[[File:" .. workshopIconFilename .. ".png|" .. size .. "|link=" .. workshopName .. "|alt=" .. workshopName .. "|" .. workshopName .. "]]"
	
	-- combine the file part with the link part
	return iconPart .. " " .. linkPart
end



-- return when required into another module
return BuildingLink