Vés al contingut

Mòdul:TempArgs

De la Viquipèdia, l'enciclopèdia lliure
local p = {}

local TNT = require('Module:TNT')
local TNTT = require('Module:TNTTools')
local SA = require('Module:SimpleArgs')
local SD = require('Module:SimpleDebug')
local yesno = require('Module:YesnoTNT')

local RS_SNotFoundInTab = 'SNotFoundInTab'
local function I18nStr (S, ...)
	SA.I18nStrParams (i18n[S], ...)
end

function p.I18nFNP (frame)
	local doc = SA.MainTemplateName(frame)
	return doc..'/i18n'
end

Module = 'Module'
function LnkI18nFN0 (S)
	return Module..':'..S..'/i18n'
end	

function p.LnkI18nFNP (frame)
	return LnkI18nFN0 (p.I18nFNP(frame))
end

function p.LnkI18nFNT (frame)
	local args = SA.GetArgs (frame)
	local I18nFN = SA.RStr_Par (args, 1)
	return LnkI18nFN0 (I18nFN)
end

function GetI18nFN (frame, args, Inc)
	if SA.Error.yes then return end
	if Inc == 1 then
		return SA.RStr_Par (args, Inc)
	else
		return SA.TemplateName(frame)
	end
end --GetI18nFN

function Ret (TempName, k1, k2)
	TempName = Module..':'..TempName.."/i18n"
	local exist, res = pcall(require, TempName)
	if exist then
		if next(res) == nil then
			error ('Not defined result in '..TempName, 0)
		else
			local any
			if k2 == nil then
				any = res.i18n[k1]
			else
				any = res.i18n[k1][k2]
			end	
			if type(any) == 'table' then
				any = table.concat (any, ', ')
			end
			return any
		end	
	else
		error (TempName..' not found', 0)
	end
end

function Str0 (frame, Inc)
	local args, NArgs = SA.GetArgs (frame, true, true)
	local I18nFN = GetI18nFN (frame, args, Inc)
	local k1 = args [1+Inc]
	local k2 = args [2+Inc]
	return Ret (I18nFN, k1, k2)
end --Str0

function p.StrNoEnd (frame)
	local args = SA.GetArgs (frame, true, true)
	local S = SA.RStr_Par (args, 1)
	return TNT.format (p.I18nFNP(frame), S)
end --StrNoEnd

function p.StrP (frame)
	return Str0 (frame, 0)
end

function p.StrT (frame)
	return Str0 (frame, 1)
end

function StrLnk0 (frame, Inc)
	local args, NArgs = SA.GetArgs (frame, true, true)
	local I18nFN = GetI18nFN (frame, args, Inc)
	local S = SA.RStr_Par (args, 1+Inc)
	if not SA.Error.yes then
		S = TNT.format (TNTT.I18nFNFull(I18nFN), S)
	end	
	if SA.Error.yes then 
		return SA.MsgError()
	else
		local Always = SA.Bool_Par (args, 2+Inc)
		local titleObj = mw.title.new (S)
		if (titleObj and titleObj.exists) or Always then
			S = '[['..S..']]'
		end
		return S
	end	
end --StrLnk0

function p.StrLnkP (frame)
	return StrLnk0 (frame, 0)
end

function p.StrLnkT (frame)
	return StrLnk0 (frame, 1)
end

function ExistsAny (frame, Inc)
	local args, NArgs = SA.GetArgs (frame)
	local I18nFN = GetI18nFN (frame, args, Inc)
	local tab = SA.StrTab_NPar (args, NArgs, 1+Inc, 1)
	local pargs = frame:getParent().args
	for _, w in ipairs(tab) do
		for j, k in pairs(pargs) do
			local num = tonumber(w)
			if not num then
				local tab2 = TNTT.TabTransCS (I18nFN, w)
				for _, m in ipairs(tab2) do
					if j == m then
						return 1
					end
				end
			else	
				if j == num then
					return 1
				end	
			end	
		end
	end
	return 0
end --ExistsAny	

function p.ExistsAnyP (frame)
	return ExistsAny (frame, 0) 
end

function p.ExistsAnyT (frame)
	return ExistsAny (frame, 1) 
end

function ExistsAll (frame, Inc)
	local args, NArgs = SA.GetArgs (frame)
	local I18nFN = GetI18nFN (frame, args, Inc)
	local tab = SA.StrTab_NPar (args, NArgs, 1+Inc, 1)
	local pargs = frame:getParent().args
	for _, w in ipairs(tab) do
		local found = false
		for j, k in pairs(pargs) do
			local num = tonumber(w)
			if not num then
				local tab2 = TNTT.TabTransCS (I18nFN, w)
				for l, m in ipairs(tab2) do
					if j == m then
						found = true
						break
					end
				end
			else	
				if j == num then
					found = true
					break
				end	
			end	
			if found then
				break
			end	
		end
		if not found then
			return 0
		end	
	end
	return 1
end --ExistsAll	

function p.ExistsAllP (frame)
	return ExistsAll (frame, 0) 
end

function p.ExistsAllT (frame)
	return ExistsAll (frame, 1) 
end

----

function Str_Par00 (frame)
	local args = SA.GetArgs (frame, true, true)
	local S = SA.Str_Par (args, 1)
	return args, S
end --Str_Par00

function Str_Par01 (frame, Inc)
	local args, NArgs = SA.GetArgs (frame, true, true)
	local I18nFN = GetI18nFN (frame, args, Inc)
	local S = SA.RStr_Par (args, 1+Inc)
	local tab = TNTT.TabTransCS (I18nFN, S)
	return args, tab
end --Str_Par01

function Str_ParArgs (frame, tab) 
	local pargs = frame:getParent().args
	for k, v in pairs(pargs) do
		for _, w in ipairs(tab) do
			if w == k then
				return mw.text.trim (v), k
			end	
		end
		if found then
			break
		end	
	end
	return nil, tab
end --Str_ParArgs

function p.Str_Par (frame)
	local args, S = Str_Par00 (frame)
	if (S == nil) or (S == '') then
		local Default = SA.Str_Par (args, 2, '')
		S = Default
	end
	return S
end --Str_Par

function Str_Par0 (frame, Inc)
	local args, tab = Str_Par01 (frame, Inc)
	if SA.Error.yes then 
		return SA.MsgError() 
	else
		local S = Str_ParArgs (frame, tab)
		if (S == nil) or (S == '') then
			local Default = SA.Str_Par (args, 2+Inc, '')
			S = Default
		end
		return S
	end
end --Str_Par0	

function p.Str_ParP (frame)
	return Str_Par0 (frame, 0) 
end

function p.Str_ParT (frame)
	return Str_Par0 (frame, 1) 
end

function p.RStr_Par (frame)
	local args, S = Str_Par00 (frame)
	local OKEmpty = SA.Bool_Par (args, 2, nil)
	if (S == nil) or ((S == '') and ((OKEmpty == nil) or (not OKEmpty))) then
		SA.NotAssignedValue (k)
		return SA.MsgError() 
	else
		return S
	end
end --RStr_Par

function RStr_Par0 (frame, Inc)
	local args, tab = Str_Par01 (frame, Inc)
	if SA.Error.yes then 
		return SA.MsgError() 
	else
		local S, k = Str_ParArgs (frame, tab) 
		local OKEmpty = SA.Bool_Par (args, 2+Inc, nil)
		if (S == nil) or ((S == '') and ((OKEmpty == nil) or (not OKEmpty))) then
			SA.NotAssignedValue (k)
			return SA.MsgError() 
		else
			return S
		end
	end	
end --RStr_Par0

function p.RStr_ParP (frame)
	return RStr_Par0 (frame, 0) 
end

function p.RStr_ParT (frame)
	return RStr_Par0 (frame, 1) 
end

----

function GetTab (args, I18nFN, Pos) 
	local S = SA.RStr_Par (args, Pos)
	return TNTT.TabTransCS (I18nFN, S)
end

function StrChkTab_ (frame, Inc)
	local S, args = SStr_Par00 (frame)
	local I18nFN = GetI18nFN (frame, args, Inc)
	local tab2 = GetTab (args, I18nFN, 2+Inc)
	local CaseSens = SA.Bool_Par (args, 3+Inc)
	return args, S, tab2, CaseSens
end	--StrChkTab_

function StrChkTab (frame, Inc)
	local args = SA.GetArgs (frame, true, true)
	local I18nFN = GetI18nFN (frame, args, Inc)
	local tab1 = GetTab (args, I18nFN, 1+Inc)
	local tab2 = GetTab (args, I18nFN, 2+Inc)
	local CaseSens = SA.Bool_Par (args, 3+Inc)
	return args, tab1, tab2, CaseSens
end	--StrChkTab

function StrChkTabEnd (k, S, tab2, CaseSens, Default)
	S = SA.StrChkTab_Par0 (k, S, tab2, CaseSens, Default)
	if SA.Error.yes then 
		return SA.MsgError()
	else
		return S
	end	
end --StrChkTabEnd

function StrChkTabPreEnd (args, k, S, tab2, CaseSens, Inc)
	local Default = SA.Str_Par (args, 4+Inc, '')
	if (S == nil) or (S == '') then
		return Default
	else
		return StrChkTabEnd (k, S, tab2, CaseSens, Default)
	end
end --StrChkTabPreEnd	

function RStrChkTabPreEnd (args, k, S, tab2, CaseSens, Inc)
	local OKEmpty = SA.Bool_Par (args, 4+Inc, nil)
	if (S == nil) or ((S == '') and ((OKEmpty == nil) or (not OKEmpty))) then
		SA.NotAssignedValue (1)
		return SA.MsgError() 
	else
		return StrChkTabEnd (k, S, tab2, CaseSens)
	end
end	--RStrChkTabPreEnd

function StrChkTab_Par_0 (frame, Inc) 
	local args, S, tab2, CaseSens = StrChkTab_ (frame, Inc)
	if SA.Error.yes then 
		return SA.MsgError() 
	else
		return StrChkTabPreEnd (args, 1, S, tab2, CaseSens, Inc)
	end
end --StrChkTab_Par_0

function RStrChkTab_Par_0 (frame, Inc) 
	local args, S, tab2, CaseSens = StrChkTab_ (frame, Inc)
	if SA.Error.yes then 
		return SA.MsgError() 
	else
		return RStrChkTabPreEnd (args, 1, S, tab2, CaseSens, Inc)
	end	
end --RStrChkTab_Par_0

function p.StrChkTab_Par_P (frame)
	return StrChkTab_Par_0 (frame, 0) 
end

function p.StrChkTab_Par_T (frame)
	return StrChkTab_Par_0 (frame, 1) 
end

function p.RStrChkTab_Par_P (frame)
	return RStrChkTab_Par_0 (frame, 0) 
end

function p.RStrChkTab_Par_T (frame)
	return RStrChkTab_Par_0 (frame, 1) 
end

function StrChkTab_Par0 (frame, Inc) 
	local args, tab1, tab2, CaseSens = StrChkTab (frame, Inc)
	if SA.Error.yes then 
		return SA.MsgError() 
	else
		local S, k = Str_ParArgs (frame, tab1)
		return StrChkTabPreEnd (args, k, S, tab2, CaseSens, Inc)
	end
end --StrChkTab_Par0

function RStrChkTab_Par0 (frame, Inc) 
	local args, tab1, tab2, CaseSens = StrChkTab (frame, Inc)
	if SA.Error.yes then 
		return SA.MsgError() 
	else
		local S, k = Str_ParArgs (frame, tab1) 
		return RStrChkTabPreEnd (args, k, S, tab2, CaseSens, Inc)
	end	
end --RStrChkTab_Par0

function p.StrChkTab_ParP (frame)
	return StrChkTab_Par0 (frame, 0) 
end

function p.StrChkTab_ParT (frame)
	return StrChkTab_Par0 (frame, 1) 
end

function p.RStrChkTab_ParP (frame)
	return RStrChkTab_Par0 (frame, 0) 
end

function p.RStrChkTab_ParT (frame)
	return RStrChkTab_Par0 (frame, 1) 
end

----

function CheckSIsNum (S, args, k, Inc)
	if S == nil then
		return S, false
	else
		LimInf = SA.Num_Par (args, 3+Inc)
		LimSup = SA.Num_Par (args, 4+Inc)
		SA.CheckSIsNum (S, {k}, LimInf, LimSup)
		if SA.Error.yes then 
			return SA.MsgError(), false, nil 
		else	
			return tonumber(S), true, k
		end	
	end
end --CheckSIsNum

function WithNum (args, N, k, Inc)
	if (N == nil) or (N == '') then
		local Default = SA.Num_Par (args, 2+Inc, nil)
		if Default ~= nil then
			return Default, true, k
		else
			return nil, false, k
		end
	else	
		return CheckSIsNum (N, args, k, Inc)
	end
end --WithNum

function Num_ParX (frame)
	local args, N = Str_Par00 (frame)
	local Valid, k
	N, Valid, k = WithNum (args, N, 1, 0)
	return N, Valid, k
end

function p.Num_Par (frame)
	local N, Valid, k = Num_ParX (frame) 
	return N
end 

function Num_Par0 (frame, Inc)
	local args, tab = Str_Par01 (frame, Inc)
	if SA.Error.yes then 
		return SA.MsgError(), false, nil 
	else
		local N, k = Str_ParArgs (frame, tab)
		return WithNum (args, N, k, Inc)
	end
end --Num_Par0	

function p.Num_ParP (frame)
	local N, Valid, k = Num_Par0 (frame, 0) 
	return N
end

function p.Num_ParT (frame)
	local N, Valid, k = Num_Par0 (frame, 1) 
	return N
end

function WithRNum  (args, N, k, Inc)
	local OKEmpty = SA.Bool_Par (args, 2+Inc, nil)
	if (N == nil) or ((N == '') and ((OKEmpty == nil) or (not OKEmpty))) then
		SA.NotAssignedValue (k)
		return SA.MsgError()
	else	
		return CheckSIsNum (N, args, k, Inc)
	end
end	--WithRNum

function RNum_ParX (frame)
	local args, N = Str_Par00 (frame)
	return WithRNum (args, N, 1, 0)
end

function p.RNum_Par (frame)
	local N, Valid, k = RNum_ParX (frame) 
	return N
end --RNum_Par

function RNum_Par0 (frame, Inc)
	local args, tab = Str_Par01 (frame, Inc)
	if SA.Error.yes then 
		return SA.MsgError(), false, nil 
	else
		local N, k = Str_ParArgs (frame, tab)
		return WithRNum (args, N, k, Inc)
	end
end --RNum_Par0	

function p.RNum_ParP (frame)
	local N, Valid, k = RNum_Par0 (frame, 0) 
	return N
end

function p.RNum_ParT (frame)
	local N, Valid, k = RNum_Par0 (frame, 1) 
	return N
end

function WithInt (N, Valid, k)
	if Valid then
		SA.CheckNumIsInt (N, {k})
		if SA.Error.yes then 
			return SA.MsgError()
		else	
			return N
		end	
	else
		return N
	end	
end --WithInt	

function p.Int_Par (frame)
	local N, Valid, k = Num_ParX (frame) 
	return WithInt (N, Valid, k)
end

function Int_Par0 (frame, Inc)
	local N, Valid, k = Num_Par0 (frame, Inc)
	return WithInt (N, Valid, k)
end	

function p.Int_ParP (frame)
	return Int_Par0 (frame, 0) 
end

function p.Int_ParT (frame)
	return Int_Par0 (frame, 1) 
end

function p.RInt_Par (frame)
	local N, Valid, k = RNum_ParX (frame) 
	return WithInt (N, Valid, k)
end

function RInt_Par0 (frame, Inc)
	local N, Valid, k = RNum_Par0 (frame, Inc)
	return WithInt (N, Valid, k)
end

function p.RInt_ParP (frame)
	return RInt_Par0 (frame, 0) 
end

function p.RInt_ParT (frame)
	return RInt_Par0 (frame, 1) 
end

----

function Bool0 (B, Default)
	local Bool = yesno (B, Default)
	if Bool == nil then
		return nil
	elseif Bool or (Bool == 1) then 
		return 1
	else
		return 0
	end
end	--Bool0

function Bool_Par (frame, func)
	local args = SA.GetArgs (frame, true, true)
	local B = func (args)
	if SA.Error.yes then 
		return SA.MsgError()
	else	
		local Default = SA.Int_Par (args, 2)
		return Bool0 (B, Default)
	end	
end --Bool_Par

function Bool_Par0 (frame, Inc)
	local args, tab = Str_Par01 (frame, Inc)
	if SA.Error.yes then 
		return SA.MsgError() 
	else
		local B = Str_ParArgs (frame, tab)
		local Default = SA.Int_Par (args, 2+Inc)
		return Bool0 (B, Default)
	end
end --Bool_Par0	

function RBool_Par0 (frame, Inc)
	local args, tab = Str_Par01 (frame, Inc)
	if SA.Error.yes then 
		return SA.MsgError() 
	else
		local B, k = Str_ParArgs (frame, tab)
		local OKEmpty = SA.Bool_Par (args, 2+Inc, nil)
		B = Bool0 (B)
		if (B == nil) and ((OKEmpty == nil) or (not OKEmpty)) then
			SA.NotAssignedValue (k)
			return SA.MsgError()
		else
			return B
		end	
	end
end --RBool_Par0	

function BoolFromArg (args)
	return SA.Str_Par (args, 1)
end	

function RBoolFromArg (args)
	return SA.RStr_Par (args, 1)
end	

function p.Bool_Par (frame)
	return Bool_Par (frame, BoolFromArg)
end

function p.Bool_ParP (frame)
	return Bool_Par0 (frame, 0)
end

function p.Bool_ParT (frame)
	return Bool_Par0 (frame, 1)
end

function p.RBool_Par (frame)
	return Bool_Par (frame, RBoolFromArg)
end

function p.RBool_ParP (frame)
	return RBool_Par0 (frame, 0)
end

function p.RBool_ParT (frame)
	return RBool_Par0 (frame, 1)
end

----

local function ArgsI18nFNWd (frame, Inc)
	local args, NArgs = SA.GetArgs (frame, true, true)
	local I18nFN = GetI18nFN (frame, args, Inc)
	local Wd = SA.RStr_Par (args, 1+Inc)
	local CaseSens = SA.RBool_Par (args,2+Inc)
	return args, NArgs, I18nFN, Wd, CaseSens
end --ArgsI18nFNWd

function FoundStrBool (frame, Inc)
	local args, NArgs, I18nFN, Wd, CaseSens = ArgsI18nFNWd (frame, Inc)
	local Bool = false
	if not SA.Error.yes then 
		Bool = TNTT.SFoundInTNTArr (I18nFN, Wd, CaseSens, args[4])
	end	
	if SA.Error.yes then 
		return SA.MsgError()
	elseif Bool then 
		return 1
	else
		return 0
	end
end --FoundStrBool
	
function p.FoundStrBoolP (frame)
	return FoundStrBool (frame, 0)  
end

function p.FoundStrBoolT (frame)
	return FoundStrBool (frame, 1)  
end

function StrIdxChkTab (frame, Inc)
	local args, NArgs, I18nFN, Wd, CaseSens = ArgsI18nFNWd (frame, Inc)
	if SA.Error.yes then 
		return SA.MsgError() 
	end
	local tab = SA.StrTab_NPar (args, NArgs, 3+Inc, 1, nil, 0)
	local Idx = 0
	if not SA.Error.yes then 
		Idx = TNTT.IdxFromTabTrans (I18nFN, Wd, CaseSens, tab)
	end	
	if SA.Error.yes then 
		return SA.MsgError()
	else
		return Idx
	end	
end --StrIdxChkTab

function p.StrIdxChkTabP (frame)
	return StrIdxChkTab (frame, 0)  
end

function p.StrIdxChkTabT (frame)
	return StrIdxChkTab (frame, 1)  
end
	
function StrIdxChkTabE (frame, Inc)
	local args, NArgs, I18nFN, Wd, CaseSens = ArgsI18nFNWd (frame, Inc)
	if SA.Error.yes then 
		return SA.MsgError() 
	end
	local tab = SA.StrTab_NPar (args, NArgs, 3+Inc, 1, nil, 0)
	local Val = 0
	if not SA.Error.yes then 
		Val = TNTT.IdxFromTabTrans (I18nFN, Wd, CaseSens, tab)
	end	
	if SA.Error.yes then 
		return SA.MsgError()
	else
		if Val == 0 then
			local S = ''
			for _, W in ipairs(tab) do
				if S ~='' then
					S = S..', '
				end	
				S = S..table.concat (TNTT.TabTransCS(I18nFN,W), SA.ParaSep)
			end
			SA.MsgError(I18nStr (RS_SNotFoundInTab, Wd, S))
			return SA.MsgError()
		else	
			return Val
		end	
	end	
end --StrIdxChkTabE

function p.StrIdxChkTabEP (frame)
	return StrIdxChkTabE (frame, 0)  
end

function p.StrIdxChkTabET (frame)
	return StrIdxChkTabE (frame, 1)  
end

return p