Jump to content

Module:Creo/NameCell

From Creopedia

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

require( 'strict' )

--- The icon-and-name cell shared by the Creo page's Moves, Traits and Abilities
--- tables.
---
--- Module:Move, Module:Trait and Module:Ability each build a file name out of
--- the subject's own name and none of them check whether that file was ever
--- uploaded, so the guard lives here. EvoCreo 1 ships no trait or ability art
--- at all, and a couple of EvoCreo 2 subjects have none either, so those rows
--- keep the plain link instead of gaining a red image link.

local p = {}

-- The size Module:BadgeLua renders its icons at, so the Name cell lines up with
-- the Rarity and Element badges sitting beside it in the same row.
local ICON_SIZE = '16px'

--- @param file string|nil
--- @return boolean
local function iconExists( file )
	if type( file ) ~= 'string' or file == '' then
		return false
	end

	local title = mw.title.new( 'File:' .. file )

	return title ~= nil and title.file ~= nil and title.file.exists
end

--- Build a table cell that leads with the subject's icon.
---
--- @param file string|nil file name, without the File: prefix
--- @param link string article to link to
--- @param label string text to show, which may differ from the article
--- @return string
function p.render( file, link, label )
	local text = string.format( '[[%s|%s]]', link, label )

	if not iconExists( file ) then
		return text
	end

	-- `metadata` keeps the icon out of MultimediaViewer and `link=` stops it
	-- competing with the article link beside it, both as Module:BadgeLua does.
	local icon = string.format( '[[File:%s|%s|class=metadata|link=]]', file, ICON_SIZE )

	return icon .. ' ' .. text
end

return p