Module:Extrait

De Les Archives de Vault-Tec

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

local p = {}

--------------------------------------
-----     Fonctions internes     ----- 
--------------------------------------

local errorText = "[[Catégorie:Page avec une erreur d'inclusion]]<Erreur module> Impossible de générer l'extrait."

function getArgs(frame)
	local args = {}
	args.nom = frame.args.nom
	
	local argsParent = frame:getParent().args
	for cle, val in pairs(argsParent) do
		if val then
			args[cle] = mw.text.trim(val)
		end
	end
	
	return args
end

--------------------------------------

function p.build(frame)
	local args = getArgs(frame)
	
	local article = args[1]
	if not article then
		return "<Erreur module> Le paramètre article doit être renseigné."
	end
	
	if not mw.title.new(article).exists then
		return errorText .. " L'article à inclure n'existe pas."
	end
	
	local text, textType
	local section = args['section']
	local heading = args['rubrique']
	
	if section then
		textType = 'section'
		text = frame:callParserFunction{ name = '#lst', args = { article, section } }
	elseif heading then
		textType = 'rubrique'
		text = frame:callParserFunction{ name = '#lsth', args = { article, heading } }
	else
		text = frame:callParserFunction{ name = '#lsth', args = { article } }
	end
	
	if #text == 0 then
		return errorText .. " La " .. textType .. " n'existe pas."
	end
	
	return '\n' .. mw.text.trim(text)
end

return p