Module:BuildingLink: Difference between revisions

From Against the Storm Official Wiki
(copied over appropriate updates to ResourceLink here)
(Changed NBSP to regular space)
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
-------------------------------------------------------------------------------
---
-- Renders the {{Building_link}} template
--- This module renders the {{Building_link}} template.
--
--- https://hoodedhorse.com/wiki/Against_the_Storm/Template:Building_link
-- Takes an argument, the name of the building. Optionally, accepts a second  
---
-- argument, "med" or "large" or "huge" to render an icon when the link is
--- The template #invokes BuildingLink.renderLink(frame), below.
-- used outside of in-line with text.
---
-------------------------------------------------------------------------------
--- 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 BuildingLink = {}


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")
--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
-- Constants
-------------------------------------------------------------------------------
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGLARGE = "ImgL"
local TEMPLATE_IMGHUGE = "ImgH"


local REPLACEMENT_FILENAME = "Question_mark.png"




--region Public methods


-------------------------------------------------------------------------------
---
-- Main rendering function
--- Renders an icon and the name of a building linked to a wiki page
-- uses ResourceData lookup function, parses the result and handles errors
--- corresponding to the provided building.
-- with default values, then assembles a final string to return to the wiki
---
-------------------------------------------------------------------------------
--- 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)
function BuildingLink.renderLink(frame)
-- Extract the template parameters.
local argBuildingName = frame.args.building
local argBuildingName = frame.args.building
local argBuildingIconSize = frame.args.iconsize
local argIconSize = frame.args.iconsize
 
if not argBuildingName then
-- Validate that there's a name to use.
return "Building_Link Error: no building given"
if not argBuildingName or "" == argBuildingName then
return "The Building Link template requires the a name of a building."
end
end
 
-- get the data about the building and then adopt the provided name
-- Look up the icon from the data sources. This validates that the building
-- in case we need to look it up again
-- name that was provided is spelled correctly and, if used, will link into
local tableData = BuildingData.getData(argBuildingName)
-- a valid page.
if not tableData then
local iconFilename
return "Building_Link Error: " .. argBuildingName .." not found"
 
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
local strPageName = tableData.page
 
-- if looking up the provided resource name returned no page, then render an error
-- Wiki link to the building's page just uses the in-game name.
if not strPageName then
local linkPart = "[[" .. argBuildingName .. "]]"
return "Building_Link Error: " .. argBuildingName .." not found"
 
-- If the specified size is small, skip the icon.
if argIconSize == DEFAULT_SIZE_SMALL then
return linkPart
end
end


-- if the icon filename didn't exist, then show a default but subtle question mark icon instead
local size = ICON_SIZES[argIconSize]
-- this will help people with troubleshooting, instead of just showing no icon then editors
 
-- wonder what went wrong
-- If the specified size was invalid, skip the icon.
local strIconFilename = tableData.iconfile or REPLACEMENT_FILENAME
if not size then
return linkPart
-- use the established templates for image sizes, with the default being
-- no icon for buildings
local switchIconSize = {
["med"] = frame:expandTemplate{title=TEMPLATE_IMGMED},
["large"] = frame:expandTemplate{title=TEMPLATE_IMGLARGE},
["huge"] = frame:expandTemplate{title=TEMPLATE_IMGHUGE}
}
-- if it's not in the switch, then skip the file part and just return the link
local strIconSize = switchIconSize[argBuildingIconSize]
if not strIconSize then
return "[[" .. strPageName .. "]]"
end
end
 
-- combine the string parts to return to the page
local iconPart = "[[File:" .. iconFilename .. "|" .. size ..
local strFilePart = string.format("[[File:%s|%s|link=%s|alt=%s|%s]]", strIconFilename, strIconSize, strPageName, strPageName, strPageName)
"|link=" .. argBuildingName .. "|alt=" .. argBuildingName .. "|" .. argBuildingName .. "]]"
 
-- combine the file part with the link part
-- combine the file part with the link part
return strFilePart .. " [[" .. strPageName .. "]]"
return iconPart .. SPACE .. 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