Module:BuildingLink: Difference between revisions

From Against the Storm Official Wiki
m (fixing error states)
(Changed NBSP to regular space)
 
(7 intermediate revisions by the same user not shown)
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:BuildingData, which is used to
--- to the link. The building is passed to Module:WorkshopsData, which is used
-- identify the wiki page to link to. It also handles some basic normalization
--- to 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  
--- 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,
-- If there is a known building that does not have an icon (might be
--- {{ImgS}}, {{ImgM}}, etc. The sizes of the icons are not stored or known by
-- the case if the data is being updated), then a question-mark icon will take
--- this module.
-- 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
--- @module BuildingLink
-- efficienty troubleshoot.
--
-- 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 BuildingLink = {}


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




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


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


-------------------------------------------------------------------------------
--endregion
-- 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
--region Public methods
-- been uploaded to the wiki. Both must be known by Module:BuildingData, or
 
-- the template's behavior may be different than expected.
---
-- @param frame a table describing the MediaWiki frame; frame['args'] is a table
--- Renders an icon and the name of a building linked to a wiki page
-- containg the template arguments; frame['args']['product'] is the first
--- corresponding to the provided building.
-- argument, assigned to the key 'product'; frame['args']['building'] is the
---
-- second argument, assigned to the key 'building'
--- Uses MediaWiki markup to display the name of a wiki page and an icon that
-- @return a string containing the wiki markup for an icon and an internal wiki
--- has been uploaded to the wiki. Both must be known by the data model,
-- page link (or an error if a problem occured)
--- 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)
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 "Building_Link Error: no building given"
return "The Building Link template requires the a name of a building."
end
end
 
-- get the page name to make sure there was one
-- Look up the icon from the data sources. This validates that the building
local building = BuildingData.getPagename(argBuildingName)
-- name that was provided is spelled correctly and, if used, will link into
if not building or "" == building then
-- a valid page.
return "Building_Link Error: " .. argBuildingName .." not found"
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
local CollectorIconFilename = CollectorsData.getCollectorIcon(argBuildingName)
if CollectorIconFilename then
iconFilename = CollectorIconFilename
else
return "No building found with provided name: " .. argBuildingName .. "."
end
end
end
end
end
end
 
-- the wiki markup for the internal link to the building's wiki page
-- Wiki link to the building's page just uses the in-game name.
local linkPart = "[[" .. building .. "]]"
local linkPart = "[[" .. argBuildingName .. "]]"
 
-- If there's no icon size specified, or if it's specified to be small
-- If the specified size is small, skip the icon.
-- (which is too small for building icons), then we can return the wiki link
if argIconSize == DEFAULT_SIZE_SMALL then
-- right away.
-- This is the default behavior.
if not argIconsize or "" == argIconsize or "small" == argIconsize then
return linkPart
return linkPart
end
end
 
--
local size = ICON_SIZES[argIconSize]
-- At this point, an icon size is requested.
 
--
-- If the specified size was invalid, skip the icon.
if not size then
-- store the requested size for use in a moment
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
-- However, if the argument did not match one of the valid sizes, then
-- we just return the link itself, with no icon.
return linkPart
return linkPart
end
end
 
-- If the icon filename doesn't exist, we can still continue by substituting
local iconPart = "[[File:" .. iconFilename .. "|" .. size ..
-- a question mark icon instead.
"|link=" .. argBuildingName .. "|alt=" .. argBuildingName .. "|" .. argBuildingName .. "]]"
local icon = BuildingData.getIconFilename(argBuildingName)
 
if not icon or "" == icon then
-- combine the file part with the link part
icon = REPLACEMENT_FILENAME
return iconPart .. SPACE .. linkPart
end
-- combine the size and filename to form the iconPart
local iconPart = "[[File:" .. icon .. "|" .. size .. "|link=" .. building .. "|alt=" .. building .. "|" .. building .. "]]"
-- combine the file part with the link part, separated by one space
return iconPart .. " " .. linkPart
end
end


 
--endregion


return BuildingLink
return BuildingLink

Latest revision as of 22:46, 11 January 2024

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")
local CollectorsData = require("Module:CollectorsData")



--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 SPACE = " "

--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
					local CollectorIconFilename = CollectorsData.getCollectorIcon(argBuildingName)
					if CollectorIconFilename then
						iconFilename = CollectorIconFilename
					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.
	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 .. SPACE .. linkPart
end

--endregion

return BuildingLink