Module:BuildingLink: Difference between revisions

From Against the Storm Official Wiki
m (added left adjustment)
(streamlined the code, added better comments)
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  
-- The template #invokes BuildingLink.renderLink(frame), below.
-- argument, "med" or "large" or "huge" to render an icon when the link is
--
-- used outside of in-line with text.
-- The template requires an argument, the name of the building to which to link.
-------------------------------------------------------------------------------
-- Optionally, the template accepts a second argument to render an icon next to
 
-- the link. The building is passed to Module:BuildingData, which is used to
-- identify the wiki page to link to. It also handles some basic normalization
-- to tolerate some small mistakes in spelling, spacing, or punctuation.
--
-- 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.
--  
-- If there is a known building that does not have an icon (might be
-- the case if the data is being updated), then a question-mark icon will take
-- 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
-- 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 BuildingData = require("Module:BuildingData") -- lookup table for buildings






-------------------------------------------------------------------------------
--
-- Constants
-- Constants
-------------------------------------------------------------------------------
--
-- names of the templates used to size the icons consistently
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGMED = "ImgM"
Line 21: Line 43:
local TEMPLATE_IMGHUGE = "ImgH"
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
local REPLACEMENT_FILENAME = "Question_mark.png"
local REPLACEMENT_FILENAME = "Question_mark.png"






-------------------------------------------------------------------------------
--
-- Main rendering function
-- 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
 
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- 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  a table describing the MediaWiki frame; frame['args'] is a table
-- containg the template arguments; frame['args']['product'] is the first
-- argument, assigned to the key 'product'; frame['args']['building'] is the
-- second argument, assigned to the key 'building'
-- @return a string containing the wiki markup for an icon and an internal wiki
-- page link (or an error if a problem occured)
function BuildingLink.renderLink(frame)
function BuildingLink.renderLink(frame)
-- extract the arguments we care about from the frame
local argBuildingName = frame.args.building
local argBuildingName = frame.args.building
local argBuildingIconSize = frame.args.iconsize
local argIconsize = frame.args.iconsize
-- validate that there's a building name to use
if not argBuildingName then
if not argBuildingName then
return "Building_Link Error: no building given"
return "Building_Link Error: no building given"
end
end
-- get the data about the building and then adopt the provided name
-- get the page name to make sure there was one
-- in case we need to look it up again
local building = BuildingData.getPagename(argBuildingName)
local tableData = BuildingData.getData(argBuildingName)
if not building or "" == building then
if not tableData then
return "Building_Link Error: " .. argBuildingName .." not found"
return "Building_Link Error: " .. argBuildingName .." not found"
end
end
local strPageName = tableData.page
-- if looking up the provided resource name returned no page, then render an error
-- the wiki markup for the internal link to the building's wiki page
if not strPageName then
local linkPart = "[[" .. building .. "]]"
return "Building_Link Error: " .. argBuildingName .." not found"
-- If there's no icon size specified, or if it's specified to be small
-- (which is too small for building icons), then we can return the wiki link
-- right away.
-- This is the default behavior.
if not argIconsize or "" == argIconsize or "small" == argIconsize then
return linkPart
end
--
-- At this point, an icon size is requested.
--
-- 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
end
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
-- If the icon filename doesn't exist, we can still continue by substituting
-- no icon for buildings
-- a question mark icon instead.
local switchIconSize = {
local icon = BuildingData.getIconFilename(argBuildingName)
["med"] = frame:expandTemplate{title=TEMPLATE_IMGMED},
if not icon or "" == icon then
["large"] = frame:expandTemplate{title=TEMPLATE_IMGLARGE},
icon = REPLACEMENT_FILENAME
["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
-- combine the size and filename to form the iconPart
local strFilePart = string.format("[[File:%s|left|%s|link=%s|alt=%s|%s]]", strIconFilename, strIconSize, strPageName, strPageName, strPageName)
local iconPart = "[[File:" .. icon .. "|" .. size .. "|link=" .. building .. "|alt=" .. building .. "|" .. building .. "]]"
-- combine the file part with the link part
-- combine the file part with the link part, separated by one space
return strFilePart .. " [[" .. strPageName .. "]]"
return iconPart .. " " .. linkPart
end
end


return BuildingLink
return BuildingLink

Revision as of 22:33, 13 February 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 which to link.
-- Optionally, the template accepts a second argument to render an icon next to 
-- the link. The building is passed to Module:BuildingData, which is used to
-- identify the wiki page to link to. It also handles some basic normalization 
-- to tolerate some small mistakes in spelling, spacing, or punctuation.
--
-- 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.
-- 
-- If there is a known building that does not have an icon (might be
-- the case if the data is being updated), then a question-mark icon will take
-- 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 
-- 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 = {}

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



--
-- 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
local REPLACEMENT_FILENAME = "Question_mark.png"



--
-- Main rendering function
--

-------------------------------------------------------------------------------
-- 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  a table describing the MediaWiki frame; frame['args'] is a table
-- containg the template arguments; frame['args']['product'] is the first
-- argument, assigned to the key 'product'; frame['args']['building'] is the 
-- second argument, assigned to the key 'building'
-- @return a string containing the wiki markup for an icon and an internal wiki 
-- page link (or an error if a problem occured)
function BuildingLink.renderLink(frame)
	
	-- extract the arguments we care about from the frame
	local argBuildingName = frame.args.building
	local argIconsize = frame.args.iconsize
	
	-- validate that there's a building name to use
	if not argBuildingName then
		return "Building_Link Error: no building given"
	end
	
	-- get the page name to make sure there was one
	local building = BuildingData.getPagename(argBuildingName)
	if not building or "" == building then
		return "Building_Link Error: " .. argBuildingName .." not found"
	end
	
	-- the wiki markup for the internal link to the building's wiki page
	local linkPart = "[[" .. building .. "]]"
	
	-- If there's no icon size specified, or if it's specified to be small 
	-- (which is too small for building icons), then we can return the wiki link 
	-- right away.
	-- This is the default behavior.
	if not argIconsize or "" == argIconsize or "small" == argIconsize then
		return linkPart
	end
	
	--
	-- At this point, an icon size is requested.
	--
	
	-- 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
	end
	
	-- If the icon filename doesn't exist, we can still continue by substituting 
	-- a question mark icon instead.
	local icon = BuildingData.getIconFilename(argBuildingName)
	if not icon or "" == icon then
		icon = REPLACEMENT_FILENAME
	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



return BuildingLink