Documentation for this module may be created at Module:Sandbox/xiaowan/doc
-- xiaowan Google Code-in, Introduction to Lua in Wikipedia
-- xiaowan Google Code-in, Working with modules
-- xiaowan Google Code-in, Calculations and testsEdit
-- xiaowan Google Code-in, Loops and tables
-- xiaowan Google Code-in, Lua librarys
-- xiaowan Google Code-in, MediaWiki libraries
local p = {}
function p.hello( frame )
return "Hello, world!"
end
p.Hi = function(frame)
local strName = frame.args.name or "Jimbo"
return "Hello from Lua to my friend " .. strName .. ".<br>"
end
p.temperature = function(frame)
local cel = tonumber(frame.args.celsius) or 0
local cab = cel*9/5+32
local msg = cel.." degrees Celsius is "..cab.." degrees Fahrenheit."
if cel > 9
then msg = msg.." It is warm."
else msg = msg.." It is cold."
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", "Neil", "Penny", "Xuan yu"}
local msg = ""
for i = 1, #family
do msg = msg .. "Hello " .. family[i] .. "<br>"
end
return msg
end
-- Task 5
p.sentence = function(frame)
local str = frame.args.words or ""
local out = string.sub(str, 2)
out = str.upper(string.sub(str, 1, 1))..out
return out
end
p.unpackdate = function(frame)
local dmydate = frame.args.dmydate or "" --dmydate=31 October 2018
local day, month, year = string.match(dmydate, "(%d+) (%w+) (%d+)")
return "Year = " .. year .. "<br>Day = " .. day .. "<br>Month = " .. month
end
p.unpackdatec = function(frame)
local mdydate = frame.args.mdydate or "" --October 31, 2018
local month, day, year = string.match(mdydate, "(%w+) (%d+), (%d+)")
return "Year = " .. year .. "<br>Day = " .. day .. "<br>Month = " .. month
end
-- Task 6
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 langs = ""
local table1 = mw.language.getFallbacksFor( langcode )
for i, j in pairs(table1) do
langs = langs .. i .. " - " .. j .. ", "
end
langs = string.sub(langs, 1, string.len(langs)-2)
return "Select "..langcode.." for defult languange will show "..langs.." if it's not availble."
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
local output1 = ""
local redirect = ""
local ttlobj = mw.title.new( ttl )
local txt = ttlobj.text
if ttl =="" then
output1 = ttl.." exists and is "
if redirect == "" then
output1 = output1.."a redirect"
else output1 = output1.."not a redirect"
end
else output1 = ttl.." does not exist and is not a redirect"
end
return output1
end
return p