Modul:VitosmoCode

Iz Wikipedije, proste enciklopedije
Jump to navigation Jump to search
-- (Vitosmo Google Code-in, Introduction to Lua in Wikipedia)

local p = {} --p stands for package
-- jumping ahead to task 6

function p.pagename( frame )
       -- pTitle = fname.args[1].getCurrentTitle()

    -- local pTitle = mw.title.getCurrentTitle()

    -- return pTitle:getContent()
	local ttl = frame.args.title
	local ttlobj = mw.title.new( ttl )
	local txt = ttlobj.text
    local contents=ttlobj:getContent()
	return ttl  .. contents
end

-- Vitosmo - task 4
p.MyFamily = function(frame)
	local family = {"Dad", "Mum", "Uncle Stan", "Aunty Elsie", "Brian"}
	local names = ""
	for i=1,5 do
	  names = names .. ", " .. family(i)
	end
	local msg = ""
	msg = msg .. "Hello " .. names .. "<br>"
	return msg
end
p.mum = function(frame)
	local num = tonumber( frame.args.num ) or 2
	local family = {"Dad", "Mum", "Uncle Stan", "Aunty Elsie", "Brian"}
	local msg = ""
	msg = msg .. "Hello " .. family[num] .. "<br>"
	return msg
end
function p.times(frame)
	local num = tonumber( frame.args.num ) or 2
	local out = "<br><br>Times table "..num.."<br>"
	local k = 0
	for i = 1, 12 do
		out = out .. i.." times "..num.." equals "..i * num 
	    k = k + 1
	    if k == 4 then
	      out = out .. "<br>"
	      k = 0
	     else 
	      out = out .. ";"
	    end
	end
	return out
end

-- Vitosmo Google Code-in, Task 3.

function p.temperature(frame)
	local cel = frame.args.celsius or 0
	local fah = cel*9 / 5 + 32
    msg = cel.." degrees Celsius is "..fah.." degrees Fahrenheit"
    return msg
 end

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


function p.hello( frame )
    return "Hello, world!"
end

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


-- ________________  older code _________________________________

-- For unit tests, see [[Modul:VitoSmoCode/testniprimeri]]

local i = {};

function p.helloAlt( frame )
    return "Hello, world!"
end

function p.zivjo( frame )
    return "zivjo, svet!"
end

function p.HelloArg( frame)
    return frame.args[1]
end

function p.getTitle(fname)

    pTitle = fname.args[1].getCurrentTitle()

    -- local pTitle = mw.title.getCurrentTitle()

    -- return pTitle:getContent()

    -- local pTitle = mw.title.getCurrentTitle()

    return pTitle
end

--____________ end of older code __

return p