Module:BuildingLink: Difference between revisions

From Against the Storm Official Wiki
(created)
 
(copied over appropriate updates to ResourceLink here)
Line 9: Line 9:
local BuildingLink = {}
local BuildingLink = {}


local IconFilenames = require("Module:IconFilenames") -- lookup table for image filenames
local BuildingData = require("Module:BuildingData") -- lookup table for buildings


-------------------------------------------------------------------------------
-- Constants
-------------------------------------------------------------------------------
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGLARGE = "ImgL"
local TEMPLATE_IMGHUGE = "ImgH"
local REPLACEMENT_FILENAME = "Question_mark.png"
-------------------------------------------------------------------------------
-- Main rendering function
-- uses ResourceData lookup function, parses the result and handles errors
-- with default values, then assembles a final string to return to the wiki
-------------------------------------------------------------------------------
function BuildingLink.renderLink(frame)
function BuildingLink.renderLink(frame)
local strBuildingName = frame.args.image
local argBuildingName = frame.args.building
local strIconSize = frame.args.size
local argBuildingIconSize = frame.args.iconsize
   
   
local strFilename = IconFilenames.getIconFilename(strBuildingName)
if not argBuildingName then
 
return "Building_Link Error: no building given"
-- if looking up the provided image name returned nil, then render an error
end
if not strFilename then
return "renderIcon Error: file " .. strBuildingName .." not found"
-- get the data about the building and then adopt the provided name
-- in case we need to look it up again
local tableData = BuildingData.getData(argBuildingName)
if not tableData then
return "Building_Link Error: " .. argBuildingName .." not found"
end
local strPageName = tableData.page
-- if looking up the provided resource name returned no page, then render an error
if not strPageName then
return "Building_Link Error: " .. argBuildingName .." not found"
end
end


-- render the string to return, starting with the file tag, if any, then the link tag
-- if the icon filename didn't exist, then show a default but subtle question mark icon instead
-- this will help people with troubleshooting, instead of just showing no icon then editors
-- wonder what went wrong
local strIconFilename = tableData.iconfile or REPLACEMENT_FILENAME
-- use the established templates for image sizes, with the default being
-- use the established templates for image sizes, with the default being
-- small, or in-line size.
-- no icon for buildings
local strFilePart = ""
local switchIconSize = {
if "med" == strIconSize then
["med"] = frame:expandTemplate{title=TEMPLATE_IMGMED},
-- don't forget the spaces when not default
["large"] = frame:expandTemplate{title=TEMPLATE_IMGLARGE},
strFilePart = string.format("[[File:%s|%s]] ", strFilename, frame:expandTemplate{title="ImgM"})
["huge"] = frame:expandTemplate{title=TEMPLATE_IMGHUGE}
elseif "large" == strIconSize then
}
strFilePart = string.format("[[File:%s|%s]] ", strFilename, frame:expandTemplate{title="ImgL"})
-- if it's not in the switch, then skip the file part and just return the link
elseif "huge" == strIconSize then
local strIconSize = switchIconSize[argBuildingIconSize]
strFilePart = string.format("[[File:%s|%s]] ", strFilename, frame:expandTemplate{title="ImgH"})
if not strIconSize then
return "[[" .. strPageName .. "]]"
end
end
-- build the link based on the name of the resource passed in
-- combine the string parts to return to the page
local strLinkPart = "[[" .. strBuildingName .. "]]"
local strFilePart = string.format("[[File:%s|%s|link=%s|alt=%s|%s]]", strIconFilename, strIconSize, strPageName, strPageName, strPageName)
return strFilePart .. strLinkPart
-- combine the file part with the link part
return strFilePart .. " [[" .. strPageName .. "]]"
end
end


return BuildingLink
return BuildingLink

Revision as of 19:27, 3 February 2023

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

-------------------------------------------------------------------------------
-- Renders the {{Building_link}} template
--
-- 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
-- used outside of in-line with text.
-------------------------------------------------------------------------------

local BuildingLink = {}

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



-------------------------------------------------------------------------------
-- Constants
-------------------------------------------------------------------------------
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGLARGE = "ImgL"
local TEMPLATE_IMGHUGE = "ImgH"

local REPLACEMENT_FILENAME = "Question_mark.png"



-------------------------------------------------------------------------------
-- Main rendering function
-- uses ResourceData lookup function, parses the result and handles errors 
-- with default values, then assembles a final string to return to the wiki
-------------------------------------------------------------------------------
function BuildingLink.renderLink(frame)
	local argBuildingName = frame.args.building
	local argBuildingIconSize = frame.args.iconsize
 
	if not argBuildingName then
		return "Building_Link Error: no building given"
	end
	
	-- get the data about the building and then adopt the provided name
	-- in case we need to look it up again
	local tableData = BuildingData.getData(argBuildingName)
	if not tableData then
		return "Building_Link Error: " .. argBuildingName .." not found"
	end
	local strPageName = tableData.page
	-- if looking up the provided resource name returned no page, then render an error
	if not strPageName then
		return "Building_Link Error: " .. argBuildingName .." not found"
	end

	-- if the icon filename didn't exist, then show a default but subtle question mark icon instead
	-- this will help people with troubleshooting, instead of just showing no icon then editors
	-- wonder what went wrong
	local strIconFilename = tableData.iconfile or REPLACEMENT_FILENAME
	
	-- 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
	
	-- combine the string parts to return to the page
	local strFilePart = string.format("[[File:%s|%s|link=%s|alt=%s|%s]]", strIconFilename, strIconSize, strPageName, strPageName, strPageName)
	
	-- combine the file part with the link part
	return strFilePart .. " [[" .. strPageName .. "]]"
end

return BuildingLink