--Tary123 Google Code-in 2017, Introduction to Lua in Wikipedia
--Lua task #3 - Create your own Lua module on English Wikipedia
local p =
function p.hello(frame) return "Hello, World!"end
--Lua task #4 - Pass information to your Lua module
p.Hi = function(frame) strName = frame.args.name or "Jimbo" return "Hello from Lua to my friend " .. strName .. ".
"end
--Lua task #5 - Perform calculations in Lua
function p.temperature(frame) cel = frame.args.celsius or 0 fah = cel*9/5+32 msg = cel.." degrees Celsius is "..fah.." degrees Fahrenheit." if tonumber(cel)>9 then msg = msg.." It is warm.
" else msg = msg.." It is cold.
" end return msgend --Lua task #7 - Repeating code p.times = function(frame) local num = tonumber(frame.args.num) or 2 local out = num.." times table
" for i = 1, 12 do out = out .. num .. " times " .. i .. " equals " .. i * num .. "
" end return outend
--Lua task #8 - Tables in Lua
p.mum = function(frame) local family = local msg = "" for i = 1, #family do msg = msg .. "Hello " .. family[i] .. "
" end return msgend
--Lua task #9 - Using MediaWiki Libraries p.langnames = function(frame) local langs = mw.language.fetchLanguageNames local langlist = "" local count = 0 for key, value in pairs(langs) do langlist = langlist .. key .. " - " .. value .. "
" count = count + 1 end return langlist .. "
= " .. count .. " languages"end p.pageinfo = function(frame) local ttl = frame.args.title local ttlobj = mw.title.new(ttl) local txt = ttlobj.prefixedText if ttlobj.exists then txt = txt.." exists and is " else txt = txt.." does not exist and is " end if not ttlobj.isRedirect then txt = txt.."not " end txt = txt.."a redirect.
"
return txtend
--Lua task #10 - Update code of the "Reign" template on English Wikipedia
function p.reign(frame) local label = frame.args.label local show = frame.args.show or frame.args.link or frame.args.lk local r = frame.args.cap and "R" or "r" local wbr = frame.args["wrap"] and "
"colon" then text = text..r.."eign:" elseif show
"lcolon" then text = text..""..r.."eign:" elseif show
"no" or show
"off" or show
"0" then text = text..r.."." elseif show
"yes" or show
"on" or show
"1" then text = text..""..r.."." elseif show
"" then start1 = "?" end if string.find(start1, "%s") or string.find(finish1, "%s") then text = text..start1.." – "..finish1 else text = text..start1.."–"..finish1 end end local middate = frame.args["mid-date"] or frame.args.middate or frame.args.mid_date if middate and start1 then middate = trim(middate) text = text..", "..wbr..middate end local start2 = frame.args[3] local finish2 = frame.args[4] if start2 or finish2 then text = text..", "..wbr start2 = start2 or "?" finish2 = finish2 or "" start2 = trim(start2) finish2 = trim(finish2) if start2
--Lua task #11 - Create a general date-handling function function p.extractdate(frame) local inputstr = frame.args[1] local dateformat = frame.args.dateformat or "dmy" local datestr -- Check for dates from 13-31 first, before 0-12 -- Necessary so as to avoid accidentally catching a month when a date is given local dateno = inputstr:match("%D(3[01])%D") or inputstr:match("^(3[01])%D") or inputstr:match("%D(3[01])$") or inputstr:match("%D(2%d)%D") or inputstr:match("^(2%d)%D") or inputstr:match("%D(2%d)$") or inputstr:match("%D(1[3-9])%D") or inputstr:match("^(1[3-9])%D") or inputstr:match("%D(1[3-9])$") or inputstr:match("%D([01]?%d)%D") or inputstr:match("^([01]?%d)%D") or inputstr:match("%D([01]?%d)$") dateno = tonumber(dateno) local month local monthno -- Date 0 is not possible if (dateno or 0)
-- year gives priority to numbers preceding AD/BC/CE/BCE, followed by 4- or 3-digit numbers, -- followed by numbers from 32 to 99 local year = inputstr:match("%d+%s-[ABC][DCE]") or inputstr:match("%d?%d%d%d") or inputstr:match("[4-9]%d") or inputstr:match("3[2-9]") if (dateformat ~= "y" and (not dateno or not month)) or not year then return "Invalid date" end local yearno = year:match("%d+") year = year:gsub(yearno, tonumber(yearno)) local eraformat = frame.args.eraformat or "CE" if eraformat
"BC" then year = year:gsub("CE", "AD") -- "BCE" is never parsed by year elseif eraformat
"BCE" then year = year:gsub("AD", "CE"):gsub("BC", "BCE") else return "Invalid era format" end if dateformat
"mdy" then datestr = month.." "..dateno..", "..year elseif dateformat
"y" then datestr = year else return "Invalid date format" end return datestrend
return p