Module:PerkLink: Difference between revisions

From Against the Storm Official Wiki
m (Fixed a concatenation error)
m (moving a space... literally that's it)
 
(2 intermediate revisions by the same user not shown)
Line 55: Line 55:
local size = resolveIconSize(iconSize)
local size = resolveIconSize(iconSize)


return string.format("[[File:%s|%s|link=%s|alt=%s|%s]]", filename, size, pageName, pageName, pageName)
return string.format("[[File:%s|%s|link=%s|alt=%s|%s]] ", filename, size, pageName, pageName, pageName)
end
end


Line 62: Line 62:
local function renderWithID(perkID, iconSize)
local function renderWithID(perkID, iconSize)


local pageName = PerksData.getNameByID(perkID) or ""
local pageName = PerksData.getNameByID(perkID)
 
if not pageName then
return "No perk found with that id: " .. perkID .. "."
end
 
local iconPart = makeIconPart(perkID, pageName, iconSize)
local iconPart = makeIconPart(perkID, pageName, iconSize)


local pagePart = "[[" .. pageName .. "]]"
local pagePart = "[[" .. pageName .. "]]"


return iconPart .. " " .. pagePart
return iconPart .. pagePart
end
end


Line 75: Line 80:


local listOfIDs = PerksData.getAllPerkIDsByName(name)
local listOfIDs = PerksData.getAllPerkIDsByName(name)
if #listOfIDs < 1 then
return "No perk found with that name: " .. name .. "."
end
local perkID = listOfIDs[1]
local perkID = listOfIDs[1]


Line 93: Line 101:


-- Extract the template parameters.
-- Extract the template parameters.
local perkID = frame.args.id or frame.args[1]
local perkID = frame.args.id
local name = frame.args.name
local name = frame.args.name
local iconSize = frame.args.iconsize
local iconSize = frame.args.iconsize

Latest revision as of 00:05, 29 March 2024

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

local PerkLink = {}



--region Dependencies

local PerksData = require("Module:PerksData")
local StyleUtils = require("Module:StyleUtils")

--endregion




--region Private member variables

-- just in case the icon isn't found but the ID and name are good
local REPLACEMENT_FILENAME = "Question_mark.png"

--endregion



--region Private methods

local function resolveIconSize(iconSizeRequested)

	-- default for perk links is small
	local size = StyleUtils.IMG_S()

	if iconSizeRequested == "med" then
		size = StyleUtils.IMG_M()
	else
		if iconSizeRequested == "large" then
			size = StyleUtils.IMG_L()
		else
			if iconSizeRequested == "huge" then
				size = StyleUtils.IMG_H()
			end
		end
	end

	return size
end



local function makeIconPart(perkID, pageName, iconSize)

	if not iconSize or iconSize == "" or iconSize == "none" then
		return ""
	end

	local filename = PerksData.getIconByID(perkID) or REPLACEMENT_FILENAME
	local size = resolveIconSize(iconSize)

	return string.format("[[File:%s|%s|link=%s|alt=%s|%s]] ", filename, size, pageName, pageName, pageName)
end



local function renderWithID(perkID, iconSize)

	local pageName = PerksData.getNameByID(perkID)

	if not pageName then
		return "No perk found with that id: " .. perkID .. "."
	end

	local iconPart = makeIconPart(perkID, pageName, iconSize)

	local pagePart = "[[" .. pageName .. "]]"

	return iconPart .. pagePart
end



local function renderWithName(name, iconSize)

	local listOfIDs = PerksData.getAllPerkIDsByName(name)
	if #listOfIDs < 1 then
		return "No perk found with that name: " .. name .. "."
	end
	local perkID = listOfIDs[1]

	return renderWithID(perkID, iconSize)
end

--endregion



--region Public methods

--- Main rendering method invoked from the template.
---
---@param frame table the MediaWiki calling context
---@return string wiki markup with the results
function PerkLink.renderLink(frame)

	-- Extract the template parameters.
	local perkID = frame.args.id
	local name = frame.args.name
	local iconSize = frame.args.iconsize

	if perkID and perkID ~= "" then
		return renderWithID(perkID, iconSize)
	else
		if name and name ~= "" then
			return renderWithName(name, iconSize)
		else
			return "The Perk_link template requires the ID or name of a perk."
		end
	end

end

--endregion

return PerkLink