Module:Event List

local p = {} local lang = mw.language.getContentLanguage local lib = require("Module:Feature")

local headings = { "Current", "Upcoming", "Permanent", "Past" }

local headingArg = { ["Current"]  = "hidecurrent", ["Upcoming"] = "hideupcoming", ["Permanent"] = "indefinite", ["Past"]     = "hidepast" }

-- get timestamp from datetime and offset function p.parseDateTime(str, offset) if lib.isEmpty(str) then return error("Input Date Time is empty or nil") end if (str:lower == "none" or str:lower == "unknown" or str:lower == "tba") then return os.time({ year="2050", month="01", day="01" }) end if offset == nil then offset = "GMT+0" end local Y, M, D, h, m, s = str:match("(%d%d%d%d)-(%d?%d)-(%d?%d)%s?(%d?%d?):?(%d?%d?):?(%d?%d?)") if (Y == nil or M == nil or D == nil) then error (str .. " is invalid" .. ". (Y=" .. (Y ~= nil and Y or "nil") .. ", M=" .. (M ~= nil and M or "nil") .. ", D=" .. (D ~= nil and D or "nil") .. ")")	end if h == nil or h == "" then h = 0 end if m == nil or m == "" then m = 0 end if s == nil or s == "" then s = 0 end local o = offset:match("GMT[+-]?(%d)") or 0 local o_dir = offset:match("GMT([+-]).*") if o_dir == "+" then o = o * -1 end return os.time({year=Y, month=M, day=D, hour=(h+o), min=m, sec=s}) end

-- format event table row display function formatEvent(event) local result = "" result = result .. " "	if lib.isNotEmpty(event.image) then result = result .. " " end if lib.isNotEmpty(event.page) then result = result .. "" .. event.name end	if lib.isNotEmpty(event.page) then result = result .. "" end result = result .. " "	result = result .. "" if event.time_known == "no" then result = result .. " TBA " else if lib.isNotEmpty(event.time_start) then if event.type == "Indefinite" or event.type2 == "Indefinite" or event.type3 == "Indefinite" then result = result .. " Since " .. lang:formatDate("F d, Y", event.time_start) else result = result .. lang:formatDate("F d, Y", event.time_start) end end if lib.isNotEmpty(event.time_end) then if event.time_end == "unknown" then result = result .. " – " .. " TBA " elseif event.time_end == "none" then result = result else result = result .. " – " .. lang:formatDate("F d, Y", event.time_end) end end end result = result .. " "	result = result .. " "	if lib.isNotEmpty(event.type) then result = result .. event.type end if lib.isNotEmpty(event.type2) then result = result .. ", " .. event.type2 end if lib.isNotEmpty(event.type3) then result = result .. ", " .. event.type3 end result = result .. " "	return result end

function p.main (frame) local args = require('Module:Arguments').getArgs(frame, {		parentFirst = true,		wrapper = { 'Template:Event_List' }	}) if frame.args.events then args.events = frame.args.events:gsub("[\n\r]", "") end return p._main(args) end

function p._main (args) if args.events == nil then error("events is nil") end --get all events local eventsTable = lib.parseTemplateFormat(args.events or "")

--group events local group = { Past = {}, Current = {}, Permanent = {}, Upcoming = {} }	local unknown = {}

local currentTime = os.time for i, event in pairs(eventsTable) do		if (lib.isEmpty(event)) then -- continue elseif (event.time_known == "no") then table.insert(group.Upcoming, event) elseif (event.time_known == "yes" and lib.isNotEmpty(event.time_start) and lib.isNotEmpty(event.time_end)) then local startTime = p.parseDateTime(event.time_start, lib.isNotEmpty(event.time_start_offset) and event.time_start_offset or "GMT+8") local endTime = p.parseDateTime(event.time_end, lib.isNotEmpty(event.time_end_offset) and event.time_end_offset or "GMT-5") if (startTime < currentTime and event.time_end == "none") then table.insert(group.Permanent, event) elseif (endTime < currentTime) then table.insert(group.Past, event) elseif (startTime < currentTime and endTime > currentTime) then table.insert(group.Current, event) elseif (startTime > currentTime) then table.insert(group.Upcoming, event) else table.insert(unknown, event) end else table.insert(group.Upcoming, event) end end

--output events local output = "" for i, heading in ipairs(headings) do		if args[headingArg[heading]] ~= "1" then output = output .. " " .. heading .. " \n" local currentTable = mw.html.create("table"):addClass("wikitable"):addClass("sortable"):css("width", "100%"):css("text-align", "center") currentTable:tag("tr"):tag("th"):css("width", "40%"):wikitext("Event"):tag("th"):css("width", "43%"):wikitext("Duration"):tag("th"):css("width", "17%"):wikitext("Type(s)") for i, event in                lib.spairs(group[heading], function(tVal,a,b) return                     (lib.isNotEmpty(tVal[a].time_start) and tVal[a].time_start or "2050-01-01 00:00:00") .. (lib.isNotEmpty(tVal[a].time_end) and tVal[a].time_end or "2050-01-01 00:00:00") < (lib.isNotEmpty(tVal[b].time_start) and tVal[b].time_start or "2050-01-01 00:00:00") .. (lib.isNotEmpty(tVal[b].time_end) and tVal[b].time_end or "2050-01-01 00:00:00")                 end) do				currentTable:tag("tr"):wikitext(formatEvent(event)) end output = output .. tostring(currentTable) end end

if next(unknown) ~= nil then output = output .. " Unknown \n" for i, event in pairs(unknown) do output = output .. event.name .. "\n" end end return output end

return p