Module:Schedule

From Coral Island Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Schedule/doc

local p = {}
local lib = require('Module:Feature')
local Tabber = require('Module:Tabber')._main

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = { 'Template:Schedule' }
	})

	args.character = args.character or mw.title.getCurrentTitle().rootText
	
	return p._main(args)
end

function p._main(args)
	local out = mw.html.create()
	local tabs = {alwaystabs=true}
	local Y = '1'
	while lib.isNotEmpty(args[Y .. '_group']) do
		-- initialize table
		local _table = mw.html.create('table'):addClass('fandom-table article-table schedule-table tdc1 tdbg1')
		local tr = _table:tag('tr')
		tr:tag('th'):wikitext('Time')
		tr:tag('th'):wikitext('Activity')
		
		local i = Y .. '_1'
		while true do
			if lib.isNotEmpty(args[i .. '_time']) and lib.isNotEmpty(args[i .. '_info']) then
				local tr = _table:tag('tr')
				if args[i .. '_time']:find('[^:\d]') then
					tr:tag('td'):wikitext(args[i .. '_time'])
				else
					local _time = lib.split(args[i .. '_time'], ':')
					tr:tag('td'):wikitext(
						string.format('%02d',tostring(math.mod(tonumber(_time[1]),12))),
						':',
						string.format('%02d', (_time[2] or '00')),
						' ',
						lib.ternary(tonumber(_time[1])>=12, 'PM', 'AM')
					)
				end
				tr:tag('td'):wikitext(args[i .. '_info'])
			elseif lib.isNotEmpty(args[i .. '_cond']) then
				local tr = _table:tag('tr'):tag('th'):addClass('days-header'):attr('colspan', '2'):wikitext(args[i .. '_cond'])
			else
				break
			end
			i = Y .. '_' ..tostring(tonumber((i:gsub('^%d+_', '')))+1)
		end
		tabs['label' .. Y] = args[Y .. '_group']
		tabs['content' .. Y] = _table
		Y = tostring(tonumber(Y)+1)
	end
	
	if not tabs['content1'] and not args.alwaystabs then
		return 'No information about ' .. args.character .. '\'s schedule.'
	else
		return Tabber(tabs)
	end
end

return p