Module:Sandbox/User:Agent-008 Explained

-- Google Code-in 2017, Introduction to Lua in Wikipedia-- [Lua task #03] Create your own Lua module on English Wikipedia

local p = -- p stands for package

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

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

p.temperature = function(frame) if frame.args.celsius then cel = frame.args.celsius else cel = 0 end 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

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

p.mum = function(frame) local family = local msg = "" for i=1,#family do msg = msg .. "Hello " .. family[i] .. "
" end return msgend

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) if frame.args.title and #(frame.args.title) ~= 0 then local titleStr = frame.args.title local title = mw.title.new(titleStr) local exists = title.id ~= 0 local isRedirect = title.isRedirect return titleStr .. (exists and " exists " or " does not exist ") .. "and is " .. (isRedirect and "a redirect" or "not a redirect") .. "
" else return "does not exist and is not a redirect
" endend

local processEmptyString = function(s) return s ~= nil and s or ""end

local removeEndSpacesremoveEndSpaces = function(s) if not s or #s

0 then--we know that it is not a space return s elseif #s

1 then return s

" " and "" or s else if string.sub(s,1,1)

" " then return removeEndSpaces(string.sub(s,2,-1))--successively remove spaces from the beginning elseif string.sub(s,-1,-1)

" " then return removeEndSpaces(string.sub(s,1,-2))--successively remove spaces from the end else return s--s has no spaces left at its beginning or its end end endend

local shouldPutSpaces = function(s) return string.find(removeEndSpaces(s)," ") or string.find(removeEndSpaces(s),'%D')end

local formattedR = function(tab)--the table of arguments which format the r (i.e. label, show, cap) if tab.label then return removeEndSpaces(tab.label) .. " " else correctCaseR = (tab.cap and (removeEndSpaces(tab.cap)

"yes" or removeEndSpaces(tab.cap)

"y")) and "R" or "r" if tab.show then sh = removeEndSpaces(tab.show) if sh

"word" then return correctCaseR .. "eigned " elseif sh

"colon" then return correctCaseR .. "eign: " elseif sh

"none" then return correctCaseR .. ". " elseif (sh

"link" or sh

"lk") then return "" .. correctCaseR .. ". " elseif sh

"lword" then return "" .. correctCaseR .. "eigned" .. " " elseif sh

"lcolon" then return "" .. correctCaseR .. "eign" .. " " else--default case - none of the above return "" .. correctCaseR .. ". " end else return "" .. correctCaseR .. ". " end endend

local formattedDash = function(tab)--the table of arguments which format the dash (i.e. the dates which go before and after. The dates do not have their eras yet.) return (shouldPutSpaces(tab[1]) or shouldPutSpaces(tab[2])) and " – " or "–"end

p.reign = function(frame) preString = (frame.args["pre-date"] and #(frame.args["pre-date"]) > 0) and (frame.args["pre-date"] .. ", ") or "" midString = (frame.args["mid-date"] and #(frame.args["mid-date"]) > 0) and (", " .. frame.args["mid-date"] .. ", ") or ", " postString = (frame.args["post-date"] and #(frame.args["post-date"]) > 0) and (", " .. frame.args["post-date"]) or "" s1 = processEmptyString(removeEndSpaces(frame.args[1])) s2 = processEmptyString(removeEndSpaces(frame.args[2])) if not frame.args[3] then if (s1 and #removeEndSpaces(s1) > 0) or (s2 and #removeEndSpaces(s2) > 0) then if (s1 and #removeEndSpaces(s1) > 0) and (s2 and #removeEndSpaces(s2) > 0) then--both arguments are passed return formattedR .. preString .. s1 .. formattedDash .. s2 .. postString .. (frame.args.era and (" " .. removeEndSpaces(frame.args.era)) or "") else if (s1 and #removeEndSpaces(s1) > 0) then--only argument 1 is passed return formattedR .. preString .. s1 .. (shouldPutSpaces(s1) and " – " or "–") .. postString .. (frame.args.era and (" " .. removeEndSpaces(frame.args.era)) or "") else--only argument 2 is passed return formattedR .. preString .. "?" .. (shouldPutSpaces(s2) and " – " or "–") .. s2 .. postString .. (frame.args.era and (" " .. removeEndSpaces(frame.args.era)) or "") end end elseif frame.args.single then return formattedR .. preString .. processEmptyString(frame.args.single) .. postString .. (frame.args.era and (" " .. removeEndSpaces(frame.args.era)) or "") else--no arguments are passed return "reign must have at least 1 date argument" end else s3 = processEmptyString(removeEndSpaces(frame.args[3]))--note that in order for s3 and s4 to work, both must be present, as well as both of s1 and s2. s4 = processEmptyString(removeEndSpaces(frame.args[4]))--otherwise, it is impossible to figure out which reigns are ranges and which are single years. return formattedR .. preString .. s1 .. formattedDash .. s2 .. (frame.args.era and (" " .. removeEndSpaces(frame.args.era)) or "") .. midString .. s3 .. formattedDash .. s4 .. postString .. (frame.args.era and (" " .. removeEndSpaces(frame.args.era)) or "") endend

return p