Jump to content

Module:Sandbox/Tary123: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Tary123 (talk | contribs)
No edit summary
Tary123 (talk | contribs)
No edit summary
Line 192: Line 192:
local monthlist = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
local monthlist = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
local idx = #inputstr
for i,mth in pairs(monthlist) do
for i,mth in pairs(monthlist) do
if inputstr:match(mth) then
if inputstr:match(mth) and inputstr:find(mth)<idx then
month = mth
month = mth
monthno = i
monthno = i
end
end
idx = inputstr:find(mth)
end
end

Revision as of 17:33, 27 December 2017

--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 .. ".<br>"
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.<br>"
	else
		msg = msg.." It is cold.<br>"
	end
		
	return msg
end
	
--Lua task #7 - Repeating code	
	
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	

--Lua task #8 - Tables in Lua

p.mum = function(frame)
	local family = {"Dad", "Mum", "Uncle Stan", "Aunty Elsie", "Brian", "Harriet", "Nicole", "Drew"}
	local msg = ""
	for i = 1, #family do
		msg = msg .. "Hello " .. family[i] .. "<br>"
	end	
	return msg
end

--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 .. "<br>"
		count = count + 1
	end
	return langlist .. "<br>= " .. 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.<br>"

	return txt
end	

--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 "<wbr>&#8203;" or ""
	local text = ""
	
	if frame.args.sortable then
		text = "<span style='display:none; speak:none;'>"
		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.."</span>"
	end	
	
	text = text.."<span style='white-space:nowrap;'>"
	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.."[[Reign|"..r.."eigned]]"
	elseif show == "lcolon" then text = text.."[[Reign|"..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.."[[Reign|"..r..".]]"
	elseif show == "blank" then
	else text = text.."<abbr title='reign'>"..r.."</abbr>."
	end
	
	if show ~= "blank" then text = text.."&nbsp;" 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..",&nbsp;"..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.."&nbsp;–&nbsp;"..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..",&nbsp;"..wbr..middate
	end	
	
	local start2  = frame.args[3]
	local finish2 = frame.args[4]
	if start2 or finish2 then
		text = text..",&nbsp;"..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.."&nbsp;–&nbsp;"..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..",&nbsp;"..wbr end
		postdate = trim(postdate)
		text = text..postdate
	end
	
	local era = frame.args.era
	if era then text = text.."&nbsp;"..era end
	
	text = text.."</span>"
	
	return text
end	
	
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["date"]
	local dateformat = frame.args.dateformat or "dmy"
	local datestr = ""
	
	local dateno = inputstr:match("%D(%d?%d)%D") or inputstr:match("^(%d?%d)%D") or inputstr:match("%D(%d?%d)$") or "invalid"
	local month
	local monthno
	
	local monthlist = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
	
	local idx = #inputstr
	for i,mth in pairs(monthlist) do
		if inputstr:match(mth) and inputstr:find(mth)<idx then
			month = mth
			monthno = i
		end
		idx = inputstr:find(mth)
	end
	
	local year = inputstr:match("%d%d%d%d")
	
	if dateformat == "dmy" then
		datestr = dateno.." "..month.." "..year
	elseif dateformat == "mdy" then
		datestr = month.." "..dateno..", "..year
	end
	
	if datestr == "invalid" or month == "invalid" or monthno == "invalid" or year == "invalid" then
		datestr = "invalid"
	end
	
	return datestr
end

return p