Module:OpeningTimes: Difference between revisions

From TwogPedia
(Created page with "local getArgs = require('Module:Arguments').getArgs local Times = {} function Times.main(frame) local args = getArgs(frame) local tableContainer = mw.html.create('table'):addClass('wikitable') local headerRow = mw.html.create('tr') :node(mw.html.create('th'):wikitext('Day')) :node(mw.html.create('th'):wikitext('Opens')) :node(mw.html.create('th'):wikitext('Closes')) tableContainer:node(headerRow) local monday = Times.createRow('Monday') local tuesday = Ti...")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 80: Line 80:
end
end
-- Check if no times have been added to a day
if string.find(tostring(monday), 'td') == nil then monday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end
if string.find(tostring(tuesday), 'td') == nil then tuesday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end
if string.find(tostring(wednesday), 'td') == nil then wednesday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end
if string.find(tostring(thursday), 'td') == nil then thursday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end
if string.find(tostring(friday), 'td') == nil then friday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end
if string.find(tostring(saturday), 'td') == nil then saturday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end
if string.find(tostring(sunday), 'td') == nil then sunday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end
return tableContainer:node(monday):node(tuesday):node(wednesday):node(thursday):node(friday):node(saturday):node(sunday)
return tableContainer:node(monday):node(tuesday):node(wednesday):node(thursday):node(friday):node(saturday):node(sunday)
end
end
Line 92: Line 102:


function Times.editTime(str)
function Times.editTime(str)
if str == 'Late' or str == 'TBD' then return str end
-- If string already has : in it
-- If string already has : in it
if string.find(str, ':') then
if string.find(str, ':') then

Latest revision as of 18:09, 13 October 2022

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

local getArgs = require('Module:Arguments').getArgs

local Times = {}

function Times.main(frame)
	local args = getArgs(frame)
	local tableContainer = mw.html.create('table'):addClass('wikitable')
	local headerRow = mw.html.create('tr')
		:node(mw.html.create('th'):wikitext('Day'))
		:node(mw.html.create('th'):wikitext('Opens'))
		:node(mw.html.create('th'):wikitext('Closes'))
	tableContainer:node(headerRow)
	
	local monday = Times.createRow('Monday')
	local tuesday = Times.createRow('Tuesday')
	local wednesday = Times.createRow('Wednesday')
	local thursday = Times.createRow('Thursday')
	local friday = Times.createRow('Friday')
	local saturday = Times.createRow('Saturday')
	local sunday = Times.createRow('Sunday')

	if args[247] or args.everyday then
		local openingTime, closingTime = '00:00', '00:00'
		if args.everyday then
			local splitTime = mw.text.split(args.everyday, '-')
			openingTime = splitTime[1]
			closingTime = splitTime[2]
		end
		if args.monday == nil then monday:node(Times.setTime(openingTime)):node(Times.setTime(closingTime)) end
		if args.tuesday == nil then tuesday:node(Times.setTime(openingTime)):node(Times.setTime(closingTime)) end
		if args.wednesday == nil then wednesday:node(Times.setTime(openingTime)):node(Times.setTime(closingTime)) end
		if args.thursday == nil then thursday:node(Times.setTime(openingTime)):node(Times.setTime(closingTime)) end
		if args.friday == nil then friday:node(Times.setTime(openingTime)):node(Times.setTime(closingTime)) end
		if args.saturday == nil then saturday:node(Times.setTime(openingTime)):node(Times.setTime(closingTime)) end
		if args.sunday == nil then sunday:node(Times.setTime(openingTime)):node(Times.setTime(closingTime)) end
	else 
		if args.weekdays then
			local splitTime = mw.text.split(args.weekdays, '-')
			if args.monday == nil then monday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2])) end
			if args.tuesday == nil then tuesday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2])) end
			if args.wednesday == nil then wednesday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2])) end
			if args.thursday == nil then thursday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2])) end
			if args.friday == nil then friday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2])) end
		end
		
		if args.weekend then
			local splitTime = mw.text.split(args.weekend, '-')
			if args.saturday == nil then saturday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2])) end
			if args.sunday == nil then sunday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2])) end
		end
	end
	
	if args.monday then
		local splitTime = mw.text.split(args.monday, '-')
		monday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2]))
	end
	if args.tuesday then
		local splitTime = mw.text.split(args.tuesday, '-')
		tuesday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2]))
	end
	if args.wednesday then
		local splitTime = mw.text.split(args.wednesday, '-')
		wednesday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2]))
	end
	if args.thursday then
		local splitTime = mw.text.split(args.thursday, '-')
		thursday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2]))
	end
	if args.friday then
		local splitTime = mw.text.split(args.friday, '-')
		friday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2]))
	end
	if args.saturday then
		local splitTime = mw.text.split(args.saturday, '-')
		saturday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2]))
	end
	if args.sunday then
		local splitTime = mw.text.split(args.sunday, '-')
		sunday:node(Times.setTime(splitTime[1])):node(Times.setTime(splitTime[2]))
	end
	
	-- Check if no times have been added to a day
	
	if string.find(tostring(monday), 'td') == nil then monday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end
	if string.find(tostring(tuesday), 'td') == nil then tuesday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end
	if string.find(tostring(wednesday), 'td') == nil then wednesday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end
	if string.find(tostring(thursday), 'td') == nil then thursday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end
	if string.find(tostring(friday), 'td') == nil then friday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end
	if string.find(tostring(saturday), 'td') == nil then saturday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end
	if string.find(tostring(sunday), 'td') == nil then sunday:node(mw.html.create('td'):addClass('tc'):attr('colspan', 2):wikitext('Closed')) end

	return tableContainer:node(monday):node(tuesday):node(wednesday):node(thursday):node(friday):node(saturday):node(sunday)
end

function Times.createRow(text)
	return mw.html.create('tr'):node(mw.html.create('th'):attr('scope', 'row'):wikitext(text))
end

function Times.setTime(timeStr)
	return mw.html.create('td'):wikitext(Times.editTime(timeStr))
end

function Times.editTime(str)
	if str == 'Late' or str == 'TBD' then return str end
	-- If string already has : in it
	if string.find(str, ':') then
		if #mw.text.split(str, ':')[1] == 1 then str = '0' .. str end
		return str
	else 
		if #str == 1 then str = '0' .. str end
		return str .. ':00'
	end
end

return Times