Module:Sandbox/BrandonXLF/1

This is an old revision of this page, as edited by BrandonXLF (talk | contribs) at 14:54, 18 April 2023. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Sandbox, do not delete

require('strict')

local p = {}

local platformLabels = {
	island = 'Island platform',
	top = 'Side platform',
	buttom = 'Side platform'
}

local function displayService(service, dest, dir)
	if not service then return '' end
	
	return '<div class="service">' ..
		'<div class="' .. (dir == 'left' and 'left' or 'left hidden') .. '">←</div>' ..
		service .. ' toward ' .. dest ..
		'<div class="' .. (dir == 'right' and 'right' or 'right hidden') .. '">→</div>' ..
	'</div>'
end

local function displayTrack(floorPart)
	local out = displayService(floorPart.service, floorPart.dest, floorPart.dir)
	
	for i = 1, floorPart.max do
		out = out .. displayService(floorPart[i].service, floorPart[i].dest, floorPart.dir)
	end
	
	return out
end

local function displayFloorPart(floorPart, floorNum, partNum)
	local out = ''
	
	if floorPart.name then
		return '<div class="track"><div>' .. floorPart.name .. '</div></div><div class="track"><div class="desc"><div>' .. (floorPart.desc or '') .. '</div>' .. displayTrack(floorPart)	.. '</div></div>'
	end
	
	if floorPart.platform then
		if not platformLabels[floorPart.platform] then
			return '<div class="part-error">Invalid platform type!</div>'
		end
		
		return '<div class="platform ' .. floorPart.platform .. '">' ..
			platformLabels[floorPart.platform] ..
			(floorPart.acessible and ' ' .. mw.getCurrentFrame():expandTemplate{ title = 'access icon' } or '') ..
		'</div>'
	end 

	local prefix = floorNum .. '_' .. partNum .. '_'
	
	return '<div class="part-error">Invalid floor part! Must have either <code>' .. prefix .. 'name</code> or <code>' .. prefix .. 'platform</code></div>.'
end

local function displayFloor(stationFloor, num)
	local out = '<div class="floor" style="grid-row: span ' .. stationFloor.count .. ';">' .. stationFloor.letter .. '</div>'

	for i = 1, stationFloor.max do
		out = out .. displayFloorPart(stationFloor[i], num, i)
	end
	
	return out
end

local function createArgTable()
	return {
		max = 0,
		count = 0
	}
end

local function addArg(tbl, num)
	if num > tbl.max then
		tbl.max = num
	end
	
	if not tbl[num] then
		tbl[num] = createArgTable()
		tbl.count = tbl.count + 1
	end
end
	
local function processArg(out, level, part, service, param, value)
	level = tonumber(level)
	part = tonumber(part)
	service = service and tonumber(service) or nil
	
	addArg(out, level)
	addArg(out[level], part)
	
	if service then
		addArg(out[level][part], service)
		out[level][part][service][param] = value
	else
		out[level][part][param] = value
	end
end
	
local function processArgs(args)
	local out = createArgTable()
	
	for i, v in pairs(args) do
		if type(i) == 'number' then
			local level = tonumber(i)
			
			if not out[level] then
				out[level] = createArgTable()
			end
			
			out[level].letter = v
		else
			local level, part, service, param = i:match('(%d+)_(%d+)_?(%d*)_(.+)')
			
			if level then
				processArg(out, level, part, service, param, v)
			end
		end
	end
	
	return out
end


function p.main(frame)
	local sortedArgs = processArgs(frame.args)
	local out = frame:extensionTag{
		name = 'templatestyles',
		args = { src = 'User:BrandonXLF/styles.css' }
	}
	
	out = out .. '<div class="station-layout">'
	
	for i = 1, sortedArgs.max do
		out = out .. displayFloor(sortedArgs[i], i)
	end
	
	return out .. '</div>'
end

return p