Home
Random
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:Sandbox/User:Agent-008
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
-- 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 .. ".<br>" 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 msg end 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", "Agent 007", "Agent 008", "World"} local msg = "" for i=1,#family do msg = msg .. "Hello " .. family[i] .. "<br>" end return msg end 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 .. "<br>" count = count + 1 end return langlist .. "<br>= " .. 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") .. "<br>" else return "does not exist and is not a redirect<br>" end end local processEmptyString = function(s) return s ~= nil and s or "" end local removeEndSpaces removeEndSpaces = 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 end end 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 "[[Reign|" .. correctCaseR .. "]]. " elseif sh == "lword" then return "[[Reign|" .. correctCaseR .. "eigned" .. "]] " elseif sh == "lcolon" then return "[[Reign|" .. correctCaseR .. "eign" .. "]] " else--default case - none of the above return "<abbr title=\"reign\">" .. correctCaseR .. "</abbr>. " end else return "<abbr title=\"reign\">" .. correctCaseR .. "</abbr>. " end end end 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({label=frame.args.label,show=frame.args.show,cap=frame.args.cap}) .. preString .. s1 .. formattedDash({s1,s2}) .. 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({label=frame.args.label,show=frame.args.show,cap=frame.args.cap}) .. preString .. s1 .. (shouldPutSpaces(s1) and " β " or "β") .. postString .. (frame.args.era and (" " .. removeEndSpaces(frame.args.era)) or "") else--only argument 2 is passed return formattedR({label=frame.args.label,show=frame.args.show,cap=frame.args.cap}) .. 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({label=frame.args.label,show=frame.args.show,cap=frame.args.cap}) .. 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({label=frame.args.label,show=frame.args.show,cap=frame.args.cap}) .. preString .. s1 .. formattedDash({s1,s2}) .. s2 .. (frame.args.era and (" " .. removeEndSpaces(frame.args.era)) or "") .. midString .. s3 .. formattedDash({s3,s4}) .. s4 .. postString .. (frame.args.era and (" " .. removeEndSpaces(frame.args.era)) or "") end end return p
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Module:Sandbox/User:Agent-008/doc
(
edit
)