« Module:Infobox commune » : différence entre les versions

De WikiPerche
Aucun résumé des modifications
Aucun résumé des modifications
 
(3 versions intermédiaires par le même utilisateur non affichées)
Ligne 2 : Ligne 2 :


local p = {}
local p = {}
local function clean(v)
if v == nil then return nil end
v = mw.text.trim(tostring(v))
if v == '' then return nil end
return v
end
local function addRow(box, label, value)
value = clean(value)
if value then
box:addRow(label, value)
end
return box
end
local function addSection(box, title, rows)
local hasContent = false
for _, r in ipairs(rows) do
if clean(r[2]) then
hasContent = true
break
end
end
if not hasContent then
return box
end
box:addHeader(title)
for _, r in ipairs(rows) do
addRow(box, r[1], r[2])
end
return box
end


function p.main(frame)
function p.main(frame)
local args = frame:getParent().args
local args = frame:getParent().args
local headerStyle
 
if args.headerstyle and args.headerstyle ~= '' then
local headerStyle = 'background-color:' .. (clean(args.headerstyle) or 'grey') .. ';'
headerStyle = string.format('background-color:%s;', args.headerstyle)
local titleStyle  = 'background-color:' .. (clean(args.titlestyle) or 'grey') .. ';'
else
 
headerStyle = 'background-color:grey;'
local box = capiunto.create{
title = clean(args.title),
titleStyle = titleStyle,
headerStyle = headerStyle,
}
 
if clean(args.image) then
box:addImage(args.image, clean(args.caption))
end
end
local retval = capiunto.create( {
title = args.title,
headerStyle = headerStyle,
} )
:addImage( args.image, args.caption )
:addRow( 'Nombre d\'habitants', args.habitants )
:addRow( 'Gentilé', args.gentile )
    :addRow( 'Site web', args.siteweb )
:addHeader( 'Géographie' )
    :addRow( 'Superficie', args.superficie )
:addRow( 'Densité de population', args.densite )
    :addRow( 'Altitude', args.altitude )
    :addRow( 'Bassin de vie', args.bassin )
:addHeader( 'Informations administratives' )
    :addRow( 'Maire', args.maire )
    :addRow( 'Intercommunalité', args.cdc )
:addRow( 'Département', args.departement )
:addRow( 'Région', args.region )
    :addRow( 'Code postal', args.codepostal )
    :addRow( 'Code INSEE', args.codeinsee )


return retval
addRow(box, "Nombre d'habitants", args.habitants)
addRow(box, 'Gentilé', args.gentile)
addRow(box, 'Site web', args.siteweb)
 
addSection(box, 'Géographie', {
{ 'Superficie', args.superficie },
{ 'Densité de population', args.densite },
{ 'Altitude', args.altitude },
{ 'Bassin de vie', args.bassin },
})
 
addSection(box, 'Informations administratives', {
{ 'Maire', args.maire },
{ 'Intercommunalité', args.cdc },
{ 'Département', args.departement },
{ 'Région', args.region },
{ 'Code postal', args.codepostal },
{ 'Code INSEE', args.codeinsee },
})
 
return box
end
end


return p
return p

Dernière version du 8 octobre 2026 à 15:45

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

local capiunto = require 'capiunto'

local p = {}

local function clean(v)
	if v == nil then return nil end
	v = mw.text.trim(tostring(v))
	if v == '' then return nil end
	return v
end

local function addRow(box, label, value)
	value = clean(value)
	if value then
		box:addRow(label, value)
	end
	return box
end

local function addSection(box, title, rows)
	local hasContent = false
	for _, r in ipairs(rows) do
		if clean(r[2]) then
			hasContent = true
			break
		end
	end
	if not hasContent then
		return box
	end

	box:addHeader(title)
	for _, r in ipairs(rows) do
		addRow(box, r[1], r[2])
	end
	return box
end

function p.main(frame)
	local args = frame:getParent().args

	local headerStyle = 'background-color:' .. (clean(args.headerstyle) or 'grey') .. ';'
	local titleStyle  = 'background-color:' .. (clean(args.titlestyle) or 'grey') .. ';'

	local box = capiunto.create{
		title = clean(args.title),
		titleStyle = titleStyle,
		headerStyle = headerStyle,
	}

	if clean(args.image) then
		box:addImage(args.image, clean(args.caption))
	end

	addRow(box, "Nombre d'habitants", args.habitants)
	addRow(box, 'Gentilé', args.gentile)
	addRow(box, 'Site web', args.siteweb)

	addSection(box, 'Géographie', {
		{ 'Superficie', args.superficie },
		{ 'Densité de population', args.densite },
		{ 'Altitude', args.altitude },
		{ 'Bassin de vie', args.bassin },
	})

	addSection(box, 'Informations administratives', {
		{ 'Maire', args.maire },
		{ 'Intercommunalité', args.cdc },
		{ 'Département', args.departement },
		{ 'Région', args.region },
		{ 'Code postal', args.codepostal },
		{ 'Code INSEE', args.codeinsee },
	})

	return box
end

return p