Module:BuildingLink

From Against the Storm Official Wiki
Revision as of 23:00, 2 February 2023 by Aeredor (talk | contribs) (created)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 IconFilenames = require("Module:IconFilenames") -- lookup table for image filenames

function BuildingLink.renderLink(frame)
	local strBuildingName = frame.args.image
	local strIconSize = frame.args.size
 
	local strFilename = IconFilenames.getIconFilename(strBuildingName)
  
	-- if looking up the provided image name returned nil, then render an error
	if not strFilename then
		return "renderIcon Error: file " .. strBuildingName .." not found"
	end

	-- render the string to return, starting with the file tag, if any, then the link tag
	
	-- use the established templates for image sizes, with the default being
	-- small, or in-line size.
	local strFilePart = ""
	if "med" == strIconSize then
		-- don't forget the spaces when not default
		strFilePart = string.format("[[File:%s|%s]] ", strFilename, frame:expandTemplate{title="ImgM"})
	elseif "large" == strIconSize then
		strFilePart = string.format("[[File:%s|%s]] ", strFilename, frame:expandTemplate{title="ImgL"})
	elseif "huge" == strIconSize then
		strFilePart = string.format("[[File:%s|%s]] ", strFilename, frame:expandTemplate{title="ImgH"})
	end
	
	-- build the link based on the name of the resource passed in
	local strLinkPart = "[[" .. strBuildingName .. "]]"
	
	return strFilePart .. strLinkPart
	
end

return BuildingLink