Aller au contenu

Module:Bacasable

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 1 avril 2018 à 15:51 et modifiée en dernier par Hlm Z. (discuter | contributions) (exportation vers Module:Discographie). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.

 Documentation[créer] [purger]
local p = {}

-- * [pairs(args)] itère sur un couple (clé, valeur).
-- * [ipairs(args)] itère sur un couple (index, valeur) et ignore les clés,
--   s'arrète si valeur = nil.
-- * [mw.dumpObject(o)] et [ou mw.logObject(o)] affichent le contenu de o.

-- HtmlBuilder
local function creerEntete(html)
	html
		:tag('div')
			:addClass('entete')
			:wikitext('Entête')
end

function p.creerCorps(args)
	local res = mw.html.create('div'):addClass('infobox')
	creerEntete(res)

	return tostring(res)
end


-- Création d'bjet
local objet = {}

objet.__index = objet

function objet.creerObjet()
	local res = {a = 1, b = false, c}
	return setmetatable(res, objet)
end

function objet:setB(b)
	self.b = b
end

function objet.__tostring(t)
	return t.a .. tostring(t.b) .. tostring(t.c)
end

function p.main2()
	local o = objet.creerObjet()
	o:setB(true)

	return tostring(o)
end


-- Métatable
local mt = {}

mt.__index = mt

function mt:insert(...)
	table.insert(self, ...)
	return self
end

function mt:concat(...)
	return table.concat(self, ...)
end

function p.new(...)
	return setmetatable({...}, mt)
end

-- Test lambda
function p.estOrdonne(args, prefixe)
	local sections, groupes, listes = {}, {}, {}
	local indice
	for cle, _ in pairs(args) do
		indice = tostring(cle):match('^section(%d+)$')
		if indice then
			table.insert(res, tonumber(indice))
		end
	end
	table.sort(res)
	
	indice = 1
	for cle, val in pairs(res) do
		if indice ~= val then
			return false
		end
		indice = indice + 1
	end

	return f(res), #res ~= 0
end

-- Gestion des arguments
local checkType = require('libraryUtil').checkType
	
local function foncDefaut(_, val)
	local res = mw.text.trim(val)
	if res == '' then
		return nil
	end
	return res
end

function p.gestionArgs(frame, options)
	checkType('gestionArgs', 1, frame, 'table', true)
	checkType('gestionArgs', 2, options, 'table', true)
	options = options or {}
	local res = {}

	if type(frame.args) == 'table'
	and type(frame.getParent) == 'function' then
		local argsFrame = frame.args or {}
		local argsParent = frame:getParent().args or {}
		-- Récupération unique des arguments passés via [#invoke].
		if options.argsFrame then
			res = argsFrame
		-- Récupération unique des arguments de la page qui a appelé [#invoke].
		elseif options.argsParent then
			res = argsParent
		-- Récupération de la totalité des arguments reçus via [#invoke].
		else

			for cle, val in pairs(argsParent) do
				res[cle] = val
			end
						for cle, val in pairs(argsFrame) do
				res[cle] = val
			end
		end
	else
		return frame
	end

	local foncIter = foncDefaut
	if type(options.foncIter) == 'function'
	or type(options.foncIter) == 'boolean' and not options.foncIter then
		foncIter = options.foncIter
	end
	if foncIter then
		for cle, val in pairs(res) do
			res[cle] = foncIter(cle, val)
		end
	end

	return res
end


local o = {}
o.__index = o

function o.Palette(args)
	local html = mw.html.create('div')
		:addClass('palette mw-collapsible')
	local etat = args['état']

	-- Configuration du paramètre [état].
	if etat == 'fermé' then
		res:addClass('mw-collapsed')
	elseif etat == 'ouvert' then
		res:addClass('mw-uncollapsed')
	end

	return setmetatable({ html = html, args = args }, o)
end

function o:test()
	local res = mw.html.create('p')
	local args = self.args

	res:wikitext('Lorem : ' .. args.lorem)
	self.html:node(res)
	return self
end

function o:test2()
	local res = mw.html.create('p')
	local args = self.args
	
	res:wikitext('Ipsum : ' .. args.ipsum)
	self.html:node(res)
	return self
end

function o.__tostring(o)
	return tostring(o.html)
end

local function gestionArgs(frame)
	local args = {}
	local argsParent = frame:getParent().args

	-- Paramètres vides interprétés par Lua.
	for cle, val in pairs(argsParent) do
		if val ~= '' then
			args[cle] = mw.text.trim(val)
		end
	end

	return args
end

function p.main(frame)
	local args = p.gestionArgs(frame)
	--[[local palette = o.Palette(args)
	palette
		:test()
		:test2()

	return palette]]--
	return mw.dumpObject(args)
end

return p