Module:Sandbox/Tary123 Explained

--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 "​" or "" local text = "" if frame.args.sortable then text = "

" local sortdate = frame.args.sort_date or frame.args.sortdate or frame.args["sort-date"] or frame.args.single or frame.args["pre-date"] or frame.args.predate or frame.args.pre_date or frame.args[1] or frame.args[2] or frame.args["post-date"] or frame.args.postdate or frame.args.post_date sortdate = string.format("%04d", sortdate) text = text..sortdate.."" end text = text.."" if label then text = text..label.."" elseif show

"word" then text = text..r.."eigned" elseif show

"colon" then text = text..r.."eign:" elseif show

"lword" then text = text..""..r.."eigned" elseif show

"lcolon" then text = text..""..r.."eign:" elseif show

"none" or show

"no" or show

"n" or show

"off" or show

"false" or show

"0" then text = text..r.."." elseif show

"link" or show

"yes" or show

"y" or show

"on" or show

"true" or show

"1" then text = text..""..r.."." elseif show

"blank" then else text = text..""..r.."." end if show ~= "blank" then text = text.." " end local predate = frame.args["pre-date"] or frame.args.predate or frame.args.pre_date if predate then predate = trim(predate) text = text..predate..", "..wbr end local start1 = frame.args[1] local finish1 = frame.args[2] if start1 or finish1 then start1 = start1 or "?" finish1 = finish1 or "" start1 = trim(start1) finish1 = trim(finish1) if start1

"" 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

"" then start2 = "?" end if string.find(start2, "%s") or string.find(finish2, "%s") then text = text..start2.." – "..finish2 else text = text..start2.."–"..finish2 end end local postdate = frame.args.single or frame.args["post-date"] or frame.args.postdate or frame.args.post_date if postdate then if finish1 or finish2 then text = text..", "..wbr end postdate = trim(postdate) text = text..postdate end local era = frame.args.era if era then text = text.." "..era end text = text.."

" return textend function trim(s) return s:gsub("^%s+", ""):gsub("%s+$", "")end

--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)

0 then dateno = nil end local monthlist = local maxdays = local idx = #inputstr for i,mth in pairs(monthlist) do local mthabbr = mth:lower:sub(1,3) -- matches first month found in inputstr, regardless of case, but only if date is valid for that month if inputstr:lower:match(mthabbr) and inputstr:lower:find(mthabbr)

-- 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

"AD" or eraformat

"BC" then year = year:gsub("CE", "AD") -- "BCE" is never parsed by year elseif eraformat

"CE" or eraformat

"BCE" then year = year:gsub("AD", "CE"):gsub("BC", "BCE") else return "Invalid era format" end if dateformat

"dmy" then datestr = dateno.." "..month.." "..year elseif dateformat

"mdy" then datestr = month.." "..dateno..", "..year elseif dateformat

"iso" then datestr = string.format("%04d", yearno).."-"..string.format("%02d", monthno).."-"..string.format("%02d", dateno) elseif dateformat

"y" then datestr = year else return "Invalid date format" end return datestrend

return p