Aller au contenu

Module:Fonctions

De Wikiquestia

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

local p = {}

local function genererBloc(args, i, charte)
	local titre = args["Titre" .. i]
	local mandat = args["Mandat" .. i]
	local predecesseur = args["Prédécesseur" .. i]
	local successeur = args["Successeur" .. i]

	if not (titre or predecesseur or successeur) then
		return ""
	end

	local couleurFond = mw.getCurrentFrame():callParserFunction("#invoke:Charte|couleur", "charte=" .. charte .. "|type=titre")
	local couleurTexte = mw.getCurrentFrame():callParserFunction("#invoke:Charte|couleur", "charte=" .. charte .. "|type=texte")
	local couleurHr = mw.getCurrentFrame():callParserFunction("#invoke:Charte|couleur", "charte=" .. charte .. "|type=hr")

	local html = {}
	table.insert(html, '<div style="margin: 0.5em 0; overflow: hidden; font-size: 90%;">')
	table.insert(html, '<div style="background-color: ' .. couleurFond .. ';color: ' .. couleurTexte .. ';text-align: center;padding: 0.5em;font-weight: bold;">')
	table.insert(html, titre or "")
	if mandat then
		table.insert(html, '<div style="font-weight: normal; font-size: 90%;">' .. mandat .. '</div>')
	end
	table.insert(html, '</div>')
	table.insert(html, '<div style="display: flex; justify-content: space-between; align-items: center; padding: 0.5em;">')
	table.insert(html, '<div style="text-align: left; flex: 1;">')
	if predecesseur then
		table.insert(html, '← [[' .. predecesseur .. ']]')
	end
	table.insert(html, '</div><div style="flex: 0 0 auto; padding: 0 1em;">·</div><div style="text-align: right; flex: 1;">')
	if successeur then
		table.insert(html, '[[' .. successeur .. ']] →')
	end
	table.insert(html, '</div></div>')
	table.insert(html, '<hr style="background-color: ' .. couleurHr .. ';">')
	table.insert(html, '</div>')

	return table.concat(html)
end

function p.afficher(frame)
	local args = frame:getParent().args
	local charte = args["charte"] or "defaut"
	local resultat = {}

	for i = 1, 5 do
		table.insert(resultat, genererBloc(args, i, charte))
	end

	return table.concat(resultat)
end

return p