Module:Paràmetres

Ón Vicipéid, an chiclipéid shaor.

Documentation for this module may be created at Module:Paràmetres/doc

--[[  
Aquest mòdul està pensat per ajudar a assegurar la compatibilitat entre plantilles, sobretot en casos
quan una plantilla s'actualitza al cap d'uns anys i cal que funcioni tant amb el nou 
plantejament com amb l'original.
]]

local p = {}

--[[ extreuNomArxiu

Paràmetres
  1: cadena de text de la qual s'ha d'extreure el nom d'arxiu
]]

function p.extreuNomArxiu(frame)
	local arg = frame.args[1] or "" -- per evitar nil
	return mw.ustring.match(arg, ":(.+%.%a+)") or arg
end

--[[ extreuCometesSimples

Paràmetres
  1: cadena de text de la qual s'ha d'extreure les cometes dobres de curvisa o triples de negreta
]]

function p.extreuCometesSimples(frame)
	local arg = frame.args[1] or "" -- per evitar nil
	return mw.ustring.gsub(arg, "^'+(.-)'+$", "%1") or arg
end

--[[ separaObraAny

Paràmetres
  1: cadena de text de la qual s'ha de separar l'obra i l'any, i posar cometes a l'obra
]]

function p.separaObraAny(frame)
	local arg = frame.args[1] or "" -- per evitar nil
	local salt = frame.args[2] or "" -- per evitar nil
	local negreta = frame.args[3] or "" -- per evitar nil
	local baixes = frame.args[4] or "" -- per evitar nil
	arg =  mw.ustring.gsub(arg, "<br( -)/->", " ") or arg --[[treu salts]]
	any = mw.ustring.match(arg, "%d+") or ""
	any = mw.ustring.gsub(any, "^'+(.-)'+$", "%1") or any --[[treu cometes]]
	text = mw.ustring.match(arg, "(.*) .*%(") or ""
	text = mw.ustring.gsub(text, "^\"+(.-)\"+$", "%1") or text --[[treu cometes dobles]]
	text = mw.ustring.gsub(text, "^'+(.-)'+$", "%1") or text --[[treu cometes]]
	text = mw.ustring.gsub(text, "^\"+(.-)\"+$", "%1") or text --[[treu cometes dobles]]
	text = mw.ustring.gsub(text, "^«+(.-)»+$", "%1") or text --[[treu cometes baixes]]
	if negreta=="sí" then
		text = "'''"..text.."'''"
	end
	if baixes == "sí" then
		text="«"..text.."»"
	else
		text="''"..text.."''"
	end
	if any~="" then
		if text~="" then
			if salt=="sí" then
				final=text.."<br />("..any..")"
			else
				final=text.." ("..any..")"
			end
		end
	else
		text = mw.ustring.match(arg, "(.*)") or ""
		text = mw.ustring.gsub(text, "^\"+(.-)\"+$", "%1") or text --[[treu cometes dobles]]
		text = mw.ustring.gsub(text, "^'+(.-)'+$", "%1") or text --[[treu cometes]]	
		text = mw.ustring.gsub(text, "^\"+(.-)\"+$", "%1") or text --[[treu cometes dobles]]
		text = mw.ustring.gsub(text, "^«+(.-)»+$", "%1") or text --[[treu cometes baixes]]
		if text~="" then
			if negreta=="sí" then
				text = "'''"..text.."'''"
			end		
			if baixes == "sí" then
				text="«"..text.."»"
			else
				text="''"..text.."''"
			end
		end
		final=text
	end
	return final or "Error"
end

--[[ numPara

Compta el número de paràmetres, passats pel frame superior;
a notar que #frame.args no funciona per ser una metataula
]]

function p.numPara( frame )
	local args = frame.args
	local pargs = frame:getParent().args
	local nobuit = args.buits == "no"
	local comptaArgs = 0
	if pargs == nil then return 0 end
	for _, val in pairs(pargs) do
		if not (nobuit and mw.text.trim(val) == "") then
			comptaArgs = comptaArgs + 1
		end
	end
	return comptaArgs
end

return p