Module:Bandeau : Différence entre versions

De Lagny-sur-Marne Wiki
Aller à : navigation, rechercher
m (Fonction ébauche : correction du nom de fonction)
m (Fonction ébauche : mauvaise initialisation)
Ligne 65 : Ligne 65 :
 
}
 
}
 
 
local index = 0
+
local index = 1
 
local inconnu = {false, ""}
 
local inconnu = {false, ""}
 
local newTheme = 0
 
local newTheme = 0
local nomTheme = ""
+
local nomTheme = arguments[1]
 
local icones = {}
 
local icones = {}
 
local sujets = {}
 
local sujets = {}
Ligne 75 : Ligne 75 :
 
-- Récupération des données sur tous les thèmes
 
-- Récupération des données sur tous les thèmes
 
while(arguments[index] ~= nil) do
 
while(arguments[index] ~= nil) do
if(newTheme > 2) then
 
-- Prévention des boucles infinies
 
newTheme = 0
 
end
 
if(newTheme == 0) then
 
-- Nouveau thème (non issu d'une redirection)
 
index = index + 1
 
nomTheme = arguments[index]
 
end
 
 
-- Récupération des données relatives au thème
 
-- Récupération des données relatives au thème
 
theme = data[nomTheme]
 
theme = data[nomTheme]
Ligne 123 : Ligne 114 :
 
inconnu[1] = true
 
inconnu[1] = true
 
inconnu[2] = inconnu[2] .. arguments[index] .. " "
 
inconnu[2] = inconnu[2] .. arguments[index] .. " "
 +
end
 +
        -- Passage éventuel au thème suivant
 +
        if(newTheme > 2) then
 +
-- Prévention des boucles infinies
 +
newTheme = 0
 +
end
 +
if(newTheme == 0) then
 +
-- Nouveau thème (non issu d'une redirection)
 +
index = index + 1
 +
nomTheme = arguments[index]
 
end
 
end
 
end
 
end

Version du 12 mars 2013 à 10:49

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

-- Module dédié à la création de bandeaux en tous genres

local fun = {}

function fun.metaBandeauAvertissement(frame)
    local pframe = frame:getParent()
    local arguments = pframe.args
    return fun.do_metaBandeauAvertissement(arguments)
end

-- Squelette d'un bandeau d'avertissement
function fun.do_metaBandeauAvertissement(arguments)
	local result = ""
	local niveau = arguments.niveau or ""
    local bClass = mw.ustring.gsub(niveau,"é","e")
	local icone = ""
    local texte = ""
    
	-- Icônes prédéfinies
	local iconeTable = {
		grave = "Fairytale no.svg",
		["modéré"] = "Emblem-important.svg",
		information = "Information_icon.svg",
		["ébauche"] = "Nuvola_apps_kedit.svg"
		}
	
	-- Utiliser l'icône fournie s'il y a lieu, ou prendre parmi celles de la table
	if(arguments["icône-complexe"] ~= nil and arguments["icône-complexe"] ~= "") then
		icone = arguments["icône-complexe"]
	else
		icone = "<div style=\"width:45px; text-align:center\">[[Fichier:" .. (arguments["icône"] or iconeTable[arguments.niveau] or "Icon apps query.svg") .. "|45x35px|alt=|link=]]</div>"
	end
	
	-- Texte du bandeau
	if(arguments.texte ~= nil) then
		texte = "<div class=\"bandeau-texte\">" .. arguments.texte .. "</div>"
	end
	
	-- Mise en boîte
	result = "<div " .. (arguments.id or "") .. "class=\"plainlinks bandeau-niveau-" .. bClass .. " bandeau js-no-interprojets\"><table style=\"background-color:transparent\"><tr><td class=\"bandeau-icone\">" .. icone .. "</td><td><div class=\"bandeau-titre\"><strong>" .. (arguments.titre or "Pas de titre") .. "</strong>" .. (arguments.date or "") .. "</div>" .. texte .. "</td></tr></table>" .. (arguments["supplément"] or "") .. "</div>"

	return result
end

function fun.ebauche(frame)
    local pframe = frame:getParent()
    local arguments = pframe.args
    return fun.do_ebauche(arguments)
end

function fun.do_ebauche(arguments)
	-- Données relatives aux différents paramètres possibles du modèle
	-- Seront déplacées en sous-page dès que loadData sera activé
	-- local data = mw.loadData("Module:Bandeau/ébauche")
	local data = {
		football = {"Soccer.svg", "le [[football]]"},
		footballeur = {"football", "un [[Portail:Football|footballeur]]"},
		["hip hop"] = {"redirection", "hip-hop"},
		["hip-hop"] = {"BandeauPortailHipHopSmall-fr.svg", "le [[Portail:Hip-hop|hip-hop]]"},
		histoire = {"Pierre_Mignard_001.jpg", "l’[[histoire]]"},
		Italie = {"Italy looking like the flag.svg", "l’[[Portail:Italie|Italie]]"},
		jardinage = {"Extracted pink rose.png", "le [[jardinage]] et l’[[horticulture]]"},
		lac = {"Icon river reservoir.svg", "un [[lac]]"},
		lapin = {"Lapin01.svg", "la [[cuniculture]]", "Cuniculture"}
	}
	
	local index = 1
	local inconnu = {false, ""}
	local newTheme = 0
	local nomTheme = arguments[1]
	local icones = {}
	local sujets = {}
	local categories = {}
	
	-- Récupération des données sur tous les thèmes
	while(arguments[index] ~= nil) do
		-- Récupération des données relatives au thème
		theme = data[nomTheme]
		if(theme ~= nil) then
			if(theme[1] == "alias" or theme[1] == "redirection") then
				-- Cas de redirection
				newTheme = newTheme + 1
				nomTheme = theme[2]
			else
				-- Cas normal
				newTheme = 0
				-- Gestion de l'icône
				if(theme[1] ~= nil) then
					local cibleIcone = data[theme[1]]
					-- Le nom de l'icône est-il une référence directe à un autre thème ?
					if(cibleIcone == nil or cibleIcone[1] == "alias" or cibleIcone[1] == "redirection") then
						-- Non : on récupère le nom du fichier
						icones[#icones+1] = theme[1]
					else
						-- Oui : on récupère l'icône de l'autre thème
						icones[#icones+1] = cibleIcone[1]
					end
				end
				-- Gestion du sujet
				if(theme[2] ~= nil) then
					sujets[#sujets+1] = theme[2]
				end
				-- Gestion de la catégorie
				if(theme[3] ~= nil) then
					categories[#categories+1] = theme[3]
				else
					-- Par défaut, la catégorie correspond au thème
					categories[#categories+1] = nomTheme
				end
			end
		else
			-- Thème inconnu : on le mémorise et on passe au suivant
			newTheme = 0
			inconnu[1] = true
			inconnu[2] = inconnu[2] .. arguments[index] .. " "
		end
        -- Passage éventuel au thème suivant
        if(newTheme > 2) then
			-- Prévention des boucles infinies
			newTheme = 0
		end
		if(newTheme == 0) then
			-- Nouveau thème (non issu d'une redirection)
			index = index + 1
			nomTheme = arguments[index]
		end
	end
	
	-- Génération du bandeau
	local multiIcone = ""
	local taille = "45x35px"
	-- Réduire les icônes si elles sont trop nombreuses
	if(#icones > 3) then
		taille = "35x25px"
	end
	if(#icones > 0) then
		for i = 1, #icones do
			multiIcone = multiIcone .. "[[Image:" .. icones[i] .. "|" .. taille .. "|alt=|link=]]"
			-- Passage sur la deuxième ligne
			if(#icones > 3 and i == math.floor((#icones+1)/2)) then
				multiIcone = multiIcone .. "</span><br /><span style=\"white-space:nowrap;word-spacing:5px\">"
			elseif(i ~= #icone) then
				multiIcone = multiIcone .. " "
			end
		end
		multiIcone = "<div style=\"text-align:center;white-space:nowrap\">" .. multiIcone .. "</div>"
	end
	
	-- Texte du bandeau, contenant les sujets
	local texteSuj = "Cet article est une [[Aide:Ébauche|ébauche]]"
	if(#sujets > 0) then
		texteSuj = texteSuj .. " concernant "
		for i = 1, #sujets do
			texteSuj = texteSuj .. sujets[i]
			if(i < #sujets-1) then
				texteSuj = texteSuj .. ", "
			elseif(i == #sujets-1) then
				texteSuj = texteSuj .. " et "
			end
		end
	end
	texteSuj = texteSuj .. ".\n"
	
	local texteCat = ""
	-- Ne pas catégoriser si nocat est présent
	if(arguments.nocat == nil) then
		if(#categories > 0) then
			for i = 1, #categories do
				texteCat = texteCat .. "[[Catégorie:Wikipédia:ébauche " .. categories[i] .. "]] "
			end
		else
			texteCat = "[[Catégorie:Wikipédia:ébauche]]"
		end
	end
	
	-- Préparation de l'appel du modèle de méta-bandeau
	local metaArgs = {
		niveau = "ébauche",
		["icône-complexe"] = multiIcone,
		titre = texteSuj
		}
	
	return fun.do_metaBandeauAvertissement(metaArgs) .. texteCat
		
end

return fun