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 214: Line 214:
if not dateno or not month or not monthno or not year then
if not dateno or not month or not monthno or not year then
return "Invalid Date"
return "Invalid date"
end
end
Line 224: Line 224:
elseif eraformat == "CE" or eraformat == "BCE" then
elseif eraformat == "CE" or eraformat == "BCE" then
year = year:gsub("AD", "CE"):gsub("BC", "BCE")
year = year:gsub("AD", "CE"):gsub("BC", "BCE")
else
return "Invalid era format"
end
end
Line 234: Line 236:
elseif dateformat == "y" then
elseif dateformat == "y" then
datestr = yearno
datestr = yearno
else
return "Invalid date format"
end
end

Revision as of 16:11, 28 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[1]
	local dateformat = frame.args.dateformat or "dmy"
	local datestr
	
	-- Check for dates from 0-29 before checking 30 and 31
	local dateno = inputstr:match("%D([0-2]?%d)%D") or inputstr:match("^([0-2]?%d)%D") or inputstr:match("%D([0-2]?%d)$") or
				   inputstr:match("%D(3[01])%D") or inputstr:match("^(3[01])%D") or inputstr:match("%D(3[01])$")
	local month
	local monthno
	
	-- Date 0 is not possible
	if tonumber(dateno) == 0 then dateno = nil end
	
	local monthlist = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
	
	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
		if string.lower(inputstr):match(mthabbr) and string.lower(inputstr):find(mthabbr)<idx then
			month = mth
			monthno = i
		end
		idx = string.lower(inputstr):find(mthabbr) or idx
	end
	
	-- 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 not dateno or not month or not monthno or not year then
		return "Invalid date"
	end
	
	local yearno = year:match("%d+")		
	
	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 = yearno
	else
		return "Invalid date format"
	end
	
	return datestr
end

return p