Odpre glavni meni

Modul:VitosmoCode

-- (Vitosmo Google Code-in, Introduction to Lua in Wikipedia)


local p = {} --p stands for package
-- Vitosmo task 6

p.pageinfo = function( frame )
	local ttl = frame.args.title
	local ttlobj = mw.title.new( ttl )
	local msg = ttlobj.text -- title of the page
	if ttlobj.exists then
	  msg = msg .. " exists"
	  if ttlobj.isRedirect then
	    msg = msg .. " and is redirect"
	  end
	else
	  msg = msg .. " does not exist"
	end
	return msg
end

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

p.fallbacks = function(frame)
	local langcode = frame.args.langcode
	langlist = mw.language.getFallbacksFor( langcode )
	local langs = ""
	local count = 0
	brake = 0
	for k, v in pairs(langlist) do
	    arg = k .. " - " .. v
		brake = brake + string.len(arg)
		if (brake > 80) then
		  brake = 0
		  langs = langs .. "<br>"
		else
		  langs = langs .. ", "		  
		end
		langs = langs .. arg
		count = count + 1
	end
	retvalue = langcode .. " fallbacks :" .. count .. " language"
        if (count > 1) then
           retvalue = retvalue .. "s<br>" .. langs
        else
           retvalue = retvalue .. "<br>" .. langs 
        end
        return retvalue
end

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

function p.pagename2( 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 5

p.unpackdateSep = function(frame)
	local dmydate = frame.args.dmydate or ""
	local month, day, year = string.match(dmydate, "(%w+)[%s%p]+ (%d+)[%s%p]+ (%d+)")
    return "Year = " .. year .. "<br>Day = " .. day .. "<br>Month = " .. month
end
p.unpackdateUSA = function(frame)
	local dmydate = frame.args.dmydateUSA or ""
	local month, day, year = string.match(dmydate, "(%w+) (%d+)%, (%d+)")
    return "Year = " .. year .. "<br>Day = " .. day .. "<br>Month = " .. month
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
-- Vitosmo - task 5


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

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

-- Vitosmo - task 4
family = {"Dad", "Mum", "Uncle Stan", "Aunty Elsie", "Brian", "Don Jr.", "Eric"}
-- Joneses = {"Jack", "Jill", "Jack jr.", "Jillete"}
-- Smiths = {"Blacky", "Doni", "Victoria", "Aunt Jill"}
p.MyFamily = function(frame)
    if (frame.args.mode == "single") then
        msg = "<br>"
    	for i=1,#family do
    	  msg = msg .. "Hello, " ..  family[i] .. "<br>"
    	end
    	return msg
    end
    if (frame.args.mode == "double") then
        msg = "Hello, "
        k = false
    	for i=1,#family do
    	  msg = msg ..  family[i]
          if k then
            msg = msg .. "<br>Hello, "
          else
            if i < #family-1 then
              msg = msg .. ", "
            end
          end
          k = not k
    	end
    	return msg
    else
    	local msg = "<br>Hello "
    	for i=1,#family do
    	  msg = msg .. ", " ..  family[i]
    	end
    	return msg
    end
end
p.mum = function(frame)
	local num = tonumber( frame.args.num ) or 2
	local msg = ""
	msg = msg .. "Hello " .. family[num]
	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
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.PageSize(fname)
    local pagename = fname.args[1]
    local pTitle = mw.title.new(pagename)
    if pTitle.exists then 
        str = pTitle:getContent()
        -- get the pagesize
        str = string.len(str)
    else
     	str = "0"
    end
    return str
end

--____________ end of older code __

return p