Module:Sandbox/Sarai Blanco

Revision as of 21:38, 19 November 2018 by imported>Sarai Blanco
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Sandbox/Sarai Blanco/doc

-- Sarai Blanco Google Code-in, Introduction to Lua in Wikipedia
-- Sarai Blanco Google Code-in, Working with modules

local p = {} 

function p.hello( frame )
    return "hello"
end

p.Hi = function(frame)
	strName = frame.args.name or "Jimbo"
	return "Hello from Lua to my friend " .. strName .. ".<br>"
end

p.temperature = function(frame)
	cel = tonumber(frame.args.celsius) or 0
	fah = ((cel*9)/5)+32
	msg = cel .. " degrees Celsius is " .. fah .. " degrees Fahrenheit."
	if cel>9 then msg=msg.. " It is warm.<br>" else msg=msg.. " It is cold.<br>" end
	return msg
end

--Task 4

p.times = function(frame)
	local num = tonumber( frame.args.num ) or 2
	local out = num .. " times table<br>"
	for i = 1, 12 do
		out = out .. num.." times ".. i .. " equals " ..(i * num) .. "<br>"
	end
	return out
end

p.mum = function(frame)
	local family = {"Dad", "Mum", "Uncle Stan", "Aunty Elsie", "Brian", "Sandra", "Jhon", "Sonia"}
	local msg = ""
	for i = 1, #family do
		msg = msg .. "Hello " .. family[i] .. "<br>"
	end
	return msg
end

p.sentence = function(frame)
	local str = frame.args.words or ""
	local out = string.upper(string.sub(str, 1, 1))
	local out = out.. string.sub (str, 2)
	return out
end

p.unpackdate = function(frame)
	local dmydate = frame.args.dmydate or ""
	local day, month, year = string.match(dmydate, "(%d+) (%w+) (%d+)")
	return "Year = " .. year .. "<br>Day = " .. day .. "<br>Month = " .. month
end

p.langnames = function(frame)
	local langlist = mw.language.fetchLanguageNames()
	local langs = ""
	local count = 0
	for k, v in pairs(langlist) do
		langs = langs .. k .. " - " .. v .. "<br>"
		count = count + 1
	end
	return langs .. "<br>= " .. count .. " languages"
end

p.fallbacks = function(frame)
	local langcode = frame.args.langcode or ""
	local langlist = mw.language.getFallbacksFor( langcode )
	local langs = ""
	for k, v in pairs(langlist) do
		langs = langs .. k .. " - " .. v .. "<br>"
	end
	return langs 
end

p.pagename = function(frame)
	local ttl = frame.args.title
	local ttlobj = mw.title.new( ttl )
	local txt = ttlobj.text
	return txt
end

p.pageinfo = function(frame)
	local ttl = frame.args.title or ""
	local ttlobj = mw.title.new( ttl ) or ""
	if ttlobj=="" then msg= "Invalid title" return msg end
	local txt = ttlobj.text
	if ttlobj.exists then txt=txt.. " exists and " else txt=txt.. " does not exist and "end
	if ttlobj.redirectTarget then txt=txt.. "is a redirect<br>" else txt=txt.. "is not a redirect<br>" end
	return txt
end

p.otra=function(frame)
	local fecha=frame.args.ff or ""
	local tipo=frame.args.format or ""
	if fecha=="" then local msg="Invalid entry" return msg.. "<br>" end
	local cir=0,ll ad=0 bc=0 bce=0 ce=0,lala
	lala=string.match(fecha,"BCE")
	if lala then bce=1 
	else
		lala=string.match(fecha,"BC")
	if lala then bc=1 end
	lala=string.match(fecha,"CE")
	if lala then ce=1 end
	end
	
	lala=string.match(fecha,"AD")
	if lala then ad=1 end

	ll=string.match(fecha,"uncertain")
	if ll then cir=1 end
	ll=string.match(fecha,"around")
	if ll then cir=1 end
	local mesesp = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }
	local months = { "jan", "Jan", "feb","Feb", "mar","Mar", "apr","Apr", "may","May", "jun","Jun","jul","Jul" , "aug","Aug", "sep","Sep", "oct","Oct", "nov","Nov", "dec","Dec" }
	local fechademes = {31,0,31,30,31,30,31,31,30,31,30,31}
	local k=0,g modo="",kk
	for i=1, #months do
		g=string.find(fecha,months[i]) or 0
		if g~=0 then k=i kk=g end
	end
	local mes=0,mesl,anio,dia
	if k~=0 then
		
		if k%2==1 then mes=(k/2)+0.5 else mes=k/2 end
		if mes~=0 then mesl=mesesp[mes] else buscm=1 end
	end
	local reco=fecha num1=0 numeros={} hay="",pos pospala=kk,posnum1
	for i=1,4 do
	num1 = string.match(reco, "(%d+)") or ""
	if i==1 then hay = string.match(reco, "(%w+)") or ""
		posnum1=string.find(reco,num1) end
		if num1~="" then
			pos=string.find(reco,num1)
			if string.len(num1)==1 then pos=pos+1 end
			if string.len(num1)==2 then pos=pos+2 end
			if string.len(num1)==3 then pos=pos+3 end
			if string.len(num1)==4 then pos=pos+4 end
			if string.len(num1)==5 then pos=pos+5 end
			reco=string.sub(reco,pos)
			table.insert(numeros,num1)
		end
	end
	
	if mes==0  then
		if #numeros>1  then
			if #numeros==3 then 
				for i=1,#numeros do
				numeros[i]=tonumber(numeros[i])
				end
		
				mes=numeros[2]
				modo="iso"
				if numeros[1]<numeros[3] then
				dia=numeros[1]
				anio=numeros[3]
				else
				anio=numeros[1]
				dia=numeros[3]
				end
			
			else
				for i=1,#numeros do
				numeros[i]=tonumber(numeros[i])
				end
				if numeros[2]<100 then local msg="Invalid entry" return msg.."<br>" end
				anio=numeros[2]
				modo="year"
			end
		else
			if #numeros==1 then	anio=numeros[1]
			modo="year"
			else local msg="Invalid entry" return msg.."<br>"
			end
		end
	else 
		if #numeros==1 then
			if pospala<posnum1 then 
			anio=numeros[1] 
			dia=0
			modo="month and year"
			else
			dia=numeros[1] 
			anio=0
			modo="month and year"
			end
		else
			if #numeros==2
			then dia=numeros[1]
				anio=numeros[2] 
				if pospala<posnum1 then 
				modo="mdy"
				else modo="dmy"
				end
			else local msg="Invalid entry" return msg.."<br>" 
			end
		end
	end

	anio=tonumber(anio) or 0
	dia=tonumber(dia) or 0
	mes=tonumber(mes) or 0
	local anio2=anio
	if anio2<100 then anio2=anio2+2000 end
	if anio2 % 1000 == 0 then fechademes[2]=29
	else
		if anio2 % 100 == 0 then fechademes[2]=28 
		else
			if anio2% 4 == 0 then fechademes[2]=29 
			else fechademes[2]=28
			end
		end
	end
	if mes~=0 and mes<13 and dia ~=0 and dia>fechademes[mes] then local msg="Invalid entry" return msg.."<br>" end
	if mes>12 then local msg="Invalid entry" return msg.."<br>" end
	local ss=""
	if modo=="" then local msg="Invalid entry" return msg.."<br>" end
	if tipo=="" then tipo=modo end
	if tipo=="dmy" then 
	ss=dia.." "..mesesp[mes].." "..anio
	end
	if tipo=="mdy" then 
	ss=mesesp[mes].." "..dia ..", "..anio
	end
	if tipo=="iso" then 
	if dia<10 then dia="0"..dia end
	if mes<10 then mes="0"..mes end
	ss=anio.."-"..mes.."-"..dia
	end
	if tipo=="year" then 
	ss=anio
	end
	if tipo=="month and year" then 
		if dia~=0 then ss=dia.." "..mesesp[mes]
		else ss=mesesp[mes].." "..anio
		end
	end
	if cir==1 then ss="circa "..ss end
	if ad==1 then ss=ss.." AD" end
	if bc==1 then ss=ss.." BC" end
	if bce==1 then ss=ss.." BCE" end
	if ce==1 then ss=ss.." CE" end
	ss=ss.."<br>"
	return ss
end

return p