Module:BuildingLink: Difference between revisions

From Against the Storm Official Wiki
m (Now properly not duplicating the file extension)
(Updated to allow any buildings: service buildings, camps, and farms now)
Line 1: Line 1:
-------------------------------------------------------------------------------
---
-- This module renders the {{Building_link}} template.
--- This module renders the {{Building_link}} template.
-- https://hoodedhorse.com/wiki/Against_the_Storm/Template:Building_link
--- https://hoodedhorse.com/wiki/Against_the_Storm/Template:Building_link
--
---
-- The template #invokes BuildingLink.renderLink(frame), below.
--- The template #invokes BuildingLink.renderLink(frame), below.
--
---
-- 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 link to.
-- 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
-- the link. The building is passed to Module:WorkshopsData, which is used to
--- to the link. The building is passed to Module:WorkshopsData, which is used
-- identify the wiki page to link to.
--- 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
-- building, and wraps both in a link to the building's wiki page. If no icon
--- 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
--- size is specified, only the name of the building will be rendered by the
-- template.
--- template.
--
---
-- The icons are sized consistently by using existing wiki templates, {{ImgS}},
--- The icons are sized consistently by using existing wiki templates,
-- {{ImgM}}, etc. The sizes of the icons are not stored or known by this module.
--- {{ImgS}}, {{ImgM}}, etc. The sizes of the icons are not stored or known by
-- @module BuildingLink
--- this module.
---
--- @module BuildingLink
local BuildingLink = {}
local BuildingLink = {}


---
 
-- Dependencies
 
--
local WorkshopsData = require("Module:WorkshopsData")
local WorkshopsData = require("Module:WorkshopsData")
local InstitutionsData = require("Module:InstitutionsData")
local FarmsData = require("Module:FarmsData")
local CampsData = require("Module:CampsData")


--region Private constants


local DEFAULT_SIZE_SMALL = "small"


--
local ICON_SIZES = {
-- Constants
[DEFAULT_SIZE_SMALL] = mw.getCurrentFrame():expandTemplate{ title = "ImgS" },
--
["med"] = mw.getCurrentFrame():expandTemplate{ title = "ImgM" },
["large"] = mw.getCurrentFrame():expandTemplate{ title = "ImgL" },
["huge"] = mw.getCurrentFrame():expandTemplate{ title = "ImgH" }
}


local NBSP = " "


-- Parameter options for icon size.
--endregion
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"






--region Public methods
---
--- Renders an icon and 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 the data model,
--- or the template's behavior may be different than expected.
---
---
-- Main rendering function
--- @param frame table the template's calling context, with parameters
--
--- @return string wiki markup
-- 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)
function BuildingLink.renderLink(frame)
 
-- Extract the template parameters.
-- 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 name to use
-- Validate that there's a name to use.
if not argBuildingName or "" == argBuildingName then
if not argBuildingName or "" == argBuildingName then
return "The Building Link template requires the a name of a building."
return "The Building Link template requires the a name of a building."
end
end


local workshopName = argBuildingName
-- Look up the icon from the data sources. This validates that the building
-- name that was provided is spelled correctly and, if used, will link into
-- a valid page.
local iconFilename


-- Look it up from the data source
local workshopIconFilename = WorkshopsData.getWorkshopIcon(argBuildingName)
local workshopIconFilename = WorkshopsData.getWorkshopIcon(argBuildingName)
if not workshopIconFilename then
if workshopIconFilename then
return "Cannot make a link to building: " .. argBuildingName .. "."
iconFilename = workshopIconFilename
else
local InstitutionIconFilename = InstitutionsData.getInstitutionIcon(argBuildingName)
if InstitutionIconFilename then
iconFilename = InstitutionIconFilename
else
local FarmIconFilename = FarmsData.getFarmIcon(argBuildingName)
if FarmIconFilename then
iconFilename = FarmIconFilename
else
local CampIconFilename = CampsData.getCampIcon(argBuildingName)
if CampIconFilename then
iconFilename = CampIconFilename
else
return "No building found with provided name: " .. argBuildingName .. "."
end
end
end
end
end


-- Wiki link to the building's page just uses the in-game name.
-- Wiki link to the building's page just uses the in-game name.
local linkPart = "[[" .. workshopName .. "]]"
local linkPart = "[[" .. argBuildingName .. "]]"
 
-- store the requested size for use in a moment
-- If the specified size is small, skip the icon.
local size = ""
if argIconSize == DEFAULT_SIZE_SMALL then
return linkPart
-- Check the requested size against the allowed options. Expand the
end
-- corresponding template.
 
if SIZE_M == argIconsize then
local size = ICON_SIZES[argIconSize]
size = frame:expandTemplate{title=TEMPLATE_IMGMED}
 
elseif SIZE_L == argIconsize then
-- If the specified size was invalid, skip the icon.
size = frame:expandTemplate{title=TEMPLATE_IMGLARGE}
if not size then
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
return linkPart
end
end
 
-- combine the size and filename to form the iconPart
local iconPart = "[[File:" .. iconFilename .. "|" .. size ..
local iconPart = "[[File:" .. workshopIconFilename .. "|" .. size .. "|link=" .. workshopName .. "|alt=" .. workshopName .. "|" .. workshopName .. "]]"
"|link=" .. argBuildingName .. "|alt=" .. argBuildingName .. "|" .. argBuildingName .. "]]"
 
-- combine the file part with the link part
-- combine the file part with the link part
return iconPart .. " " .. linkPart
return iconPart .. NBSP .. linkPart
end
end


--endregion


-- return when required into another module
return BuildingLink
return BuildingLink

Revision as of 04:48, 27 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 link to.
--- 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 = {}



local WorkshopsData = require("Module:WorkshopsData")
local InstitutionsData = require("Module:InstitutionsData")
local FarmsData = require("Module:FarmsData")
local CampsData = require("Module:CampsData")



--region Private constants

local DEFAULT_SIZE_SMALL = "small"

local ICON_SIZES = {
	[DEFAULT_SIZE_SMALL] = mw.getCurrentFrame():expandTemplate{ title = "ImgS" },
	["med"] = mw.getCurrentFrame():expandTemplate{ title = "ImgM" },
	["large"] = mw.getCurrentFrame():expandTemplate{ title = "ImgL" },
	["huge"] = mw.getCurrentFrame():expandTemplate{ title = "ImgH" }
}

local NBSP = " "

--endregion



--region Public methods

---
--- Renders an icon and 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 the data model,
--- or the template's behavior may be different than expected.
---
--- @param frame table the template's calling context, with parameters
--- @return string 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 requires the a name of a building."
	end

	-- Look up the icon from the data sources. This validates that the building
	-- name that was provided is spelled correctly and, if used, will link into
	-- a valid page.
	local iconFilename

	local workshopIconFilename = WorkshopsData.getWorkshopIcon(argBuildingName)
	if workshopIconFilename then
		iconFilename = workshopIconFilename
	else
		local InstitutionIconFilename = InstitutionsData.getInstitutionIcon(argBuildingName)
		if InstitutionIconFilename then
			iconFilename = InstitutionIconFilename
		else
			local FarmIconFilename = FarmsData.getFarmIcon(argBuildingName)
			if FarmIconFilename then
				iconFilename = FarmIconFilename
			else
				local CampIconFilename = CampsData.getCampIcon(argBuildingName)
				if CampIconFilename then
					iconFilename = CampIconFilename
				else
					return "No building found with provided name: " .. argBuildingName .. "."
				end
			end
		end
	end

	-- Wiki link to the building's page just uses the in-game name.
	local linkPart = "[[" .. argBuildingName .. "]]"

	-- If the specified size is small, skip the icon.
	if argIconSize == DEFAULT_SIZE_SMALL then
		return linkPart
	end

	local size = ICON_SIZES[argIconSize]

	-- If the specified size was invalid, skip the icon.
	if not size then
		return linkPart
	end

	local iconPart = "[[File:" .. iconFilename .. "|" .. size ..
			"|link=" .. argBuildingName .. "|alt=" .. argBuildingName .. "|" .. argBuildingName .. "]]"

	-- combine the file part with the link part
	return iconPart .. NBSP .. linkPart
end

--endregion

return BuildingLink