Module:Icône

De Les Archives de Vault-Tec
Révision datée du 18 juin 2021 à 09:33 par Kharmitch (discussion | contributions) (Ajout de la fonction doc)

La documentation pour ce module peut être créée à Module:Icône/doc

local p = {}

local icons = require('Module:Icône/Données')

local DEFAULT_ICON_NAME = '_DEFAULT'
local DEFAULT_SIZE = 'normal'
local STANDARD_SIZE = {
	normal = '14',
	grand  = '20',
	big    = '20'
}

function buildIcon(args)
	local name = args[1] or DEFAULT_ICON_NAME
	local link = args['lien'] or args['link'] or ''
	
	local icon = icons[name:lower()] or icons[DEFAULT_ICON_NAME]
	local filename = icon.filename
	local tooltip = args['infobulle'] or args['tooltip'] or icon.tooltip
	
	local size = args[2] or DEFAULT_SIZE
	size = STANDARD_SIZE[size] or string.gsub(size, '[px]', '')
	
	return tostring(mw.html.create('span')
		:css('cursor', 'help')
		:attr('title', tooltip)
		:wikitext('[[Fichier:'..filename..'|x'..size..'px|link='..link..']]')
		:allDone())
end

--[[
	Fonction utilisée dans la doc du modèle pour présenter
	les cônes disponibles aux contributeurs
]]
function p.doc()
	local tabDefinition = '{| class="va-table va-table-center-col2" style="float:left;margin-right:4px;"\n!style="width:70%;"|mot-clé!!style="width:30%;"|icône\n'
	local doc = tabDefinition

	keywords = {}
    for k in pairs(icons) do
        table.insert(keywords, k)
    end
    table.sort(keywords)
    
    local split = math.ceil(#keywords / 3)
    local n = 0
    
    for k, v in ipairs(keywords) do
    	if n > split then
    		doc = doc..'\n|}'
    		doc = doc..'\n'..tabDefinition
    		n = 0
    	end
    	
    	n = n + 1
    	local icon = icons[v]
    	
    	doc = doc..'|-\n|'..v..'||[[Fichier:'..icon.filename..'|x20px|link=]]\n'
    end
    
    doc = doc..'|}'

	return doc
end

function p.build(frame)
	return buildIcon(require('Module:Outils').extractArgs(frame))
end

return p