Module:Frame

Une page de Wikiversité, la communauté pédagogique libre.

Ce module sert d'exemple dans la leçon Initiation au Lua avec Scribunto. son utilisation est décrite en détail dans celle-ci et, par conséquent, ce module ne doit pas être modifié sans tenir compte de la leçon.


local p = {}

function p.visualisation(frame)
	reponse = ""
	for index, objet in pairs(mw.ext) do
		reponse = reponse.."<br />À la clé "..index..", on trouve un objet de type : "..type(objet)
	end
	return reponse
end

function p.visualise(frame)
	reponse = ""
	for index, objet in pairs(frame) do
		reponse = reponse.."<br />À la clé "..index..", on trouve un objet de type : "..type(objet)
	end
	return reponse
end

function p.evalue(frame)
	local reponse = ""
	local nombre,chaine = 0,0
	for index, objet in ipairs(frame.args) do
		if tonumber(objet) == nil then
			chaine = chaine + 1
		else
			nombre = nombre + 1
		end
	end
	reponse = reponse.."Il y a "..nombre.." nombres et "..chaine.." chaines de caractères."
	return reponse
end

function p.renvoie(frame)
	local reponse = ""
	local nombre,chaine = 0,0
	for index, objet in pairs(frame.args) do
		reponse = reponse.."<br />À la clé "..index..", il y a l’objet : "..objet
	end
	return reponse
end

function p.parent(frame)
	local reponse = ""
	local model = frame:getParent()
	for index, objet in pairs(model.args) do
		reponse = reponse.."<br />À la clé "..index..", il y a l’objet : "..objet
	end
	return reponse
end

function p.replique(frame)
	return "Je renvoie "..frame.args[1].." et "..frame.args[2]
end

function p.child(frame)
	newFrame =  frame:newChild{ args = {"Jeudi",frame.args[2]}}
	return p.replique(newFrame)
end

function p.process(frame)
	local reponse = ""
	reponse = frame:preprocess("{{Attention|Essai avec le modèle Attention}}")
	return "<br />La fonction a retourné : "..reponse
end

function p.template(frame)
	reponse = frame:expandTemplate{ title = "Attention", args = { "Essai avec le modèle Attention" } }
	return reponse
end

function p.pair(frame)
	local reponse = ""
	local nombre,chaine = 0,0
	for index, objet in frame:argumentPairs() do
		reponse = reponse.."<br />À la clé "..index..", il y a l’objet : "..objet
	end
	return reponse
end

function p.parser(frame)
	local reponse = ""
	reponse = frame:callParserFunction('#time', 'Y-m-d H:i:s')
	if reponse == nil then
		return "Il y a un problème, la fonction a retourné : nil"
	else
		return "La fonction a retourné : "..reponse.." et son type est : "..type(reponse)
	end
end

function p.tag(frame)
	local reponse = ""
	reponse = frame:extensionTag('nowiki', '[[texte]]', {})
	if reponse == nil then
		return "Il y a un problème, la fonction a retourné : nil"
	else
		return "La fonction a retourné : "..reponse.." et son type est : "..type(reponse)
	end
end

function p.notag(frame)
	local reponse = ""
	reponse = '[[texte]]'
	if reponse == nil then
		return "Il y a un problème, la fonction a retourné : nil"
	else
		return "La fonction a retourné : "..reponse.." et son type est : "..type(reponse)
	end
end

function p.argument(frame)
	local reponse = ""
	reponse = frame:getArgument(1)
	if reponse == nil then
		return "Il y a un problème, la fonction a retourné : nil"
	else
		return "La fonction a retourné : "..reponse:expand().." et son type est : "..type(reponse)
	end
end

function p.namedArgument(frame)
	local reponse = ""
	reponse = frame:getArgument("fleur")
	if reponse == nil then
		return "Il y a un problème, la fonction a retourné : nil"
	else
		return "La fonction a retourné : "..reponse:expand().." et son type est : "..type(reponse)
	end
end

function p.parval(frame)
	local reponse = ""
	reponse = frame:newParserValue()
	if reponse == nil then
		return "Il y a un problème, la fonction a retourné : nil"
	else
		return "La fonction a retourné une variable de type : "..type(reponse).." qui contient : <br />"..table.concat(reponse, " ---- ")
	end
end

function p.temparval(frame)
	local reponse = ""
	local tab = {}
	reponse = frame:newTemplateParserValue(tab)
	if reponse == nil then
		return "Il y a un problème, la fonction a retourné : nil"
	else
		return "La fonction a retourné une variable de type : "..type(reponse).." qui contient : <br />"..table.concat(reponse, " ---- ")
	end
end

function p.title(frame)
	local reponse = ""
	reponse = frame:getTitle()
	if reponse == nil then
		return "Il y a un problème, la fonction a retourné : nil"
	else
		return "La fonction a retourné : "..reponse.." et son type est : "..type(reponse)
	end
end

return p