Module:Adjacent stations

-- Function allowing for consistent treatment of boolean-like wikitext input. -- It works similarly to the template.

local function yesno(val, default) val = type(val) == 'string' and val:lower or val if val == nil then return nil elseif val == true or val == 'yes' or val == 'y'		or val == 'true' or val == 't'		or val == 'on' or tonumber(val) == 1 then return true elseif val == false or val == 'no' or val == 'n'		or val == 'false' or val == 'f'		or val == 'off' or tonumber(val) == 0 then return false else return default end end

local _style = { ['3'] = 'style="width: 30%;', ['c'] = 'style="text-align: center;' , ['v'] = 'vertical-align: middle;' , ['bd'] = 'border:' , ['t'] = 'border-top:' , ['bb'] = 'border-bottom:' , ['L'] = 'border-left:' , ['r'] = 'border-right:' , ['n'] = 'none;' , ['0'] = '0px none;' , ['1'] = '1px solid #aaa;' } local style = { ['table'] = table.concat({'style="width: 55%; margin:0.5em auto; font-size:95%; clear:both;'	, _style['t'], _style['0']		, 'border-collapse: separate;" cellspacing="0" cellpadding="-1"'	}) , ['header leftcell'] = table.concat({_style['3'], _style['v']	, _style['bd'], _style['1']		, _style['L'], _style['n']		, _style['bb'], _style['n']		, '"|'	})	, ['header banner'] = table.concat({'style="'		, _style['bd'], _style['n']		, _style['t'], _style['1']		, '"|'	})	, ['header midcell'] = table.concat({'colspan="3" style="', _style['v']		, _style['bd'], _style['n']		, _style['t'], _style['1']		, '"|'	})	, ['header rightcell'] = table.concat({_style['3'], _style['v']		, _style['bd'], _style['1']		, _style['r'], _style['n']		, _style['bb'], _style['n']		, '"|'	}) , ['body leftcell'] = table.concat({_style['c'], _style['v']	, _style['L'], _style['0']		, _style['bb'], _style['0']		, _style['r'], _style['1']		, _style['t'], _style['1']		, '"|'	})	, ['body banner'] = table.concat({_style['c']		, _style['L'], _style['0']		, _style['bb'], _style['0']		, _style['r'], _style['0']		, _style['t'], _style['1']		, 'width: 8px; background-color: #'	})	, ['body midcell'] = table.concat({_style['c'], _style['v']		, _style['bb'], _style['0']		, '"|'	}) , ['body rightcell'] = table.concat({_style['c'], _style['v']	, _style['L'], _style['1']		, _style['bb'], _style['0']		, _style['r'], _style['0']		, _style['t'], _style['1']		, '"|'	}) }

local function subFormat(s) return ' ' .. s .. ' ' end

local p = {}

function p.top return table.concat({'{| class="wikitable" ', style['table']}) end

function p.rail(data) return table.concat({'\n|-'	, '\n!', style['header leftcell'], 'Preceding station'		, '\n!', style['header midcell'], data['system title']		, '\n!', style['header rightcell'], 'Following station'	}) end

function p.line(data, args) local lineTitle = {} local colour = {} local left = {} local right = {} local note = {} local leftTerminus = {} local rightTerminus = {} local t = {} for i = 1, #args.line do		local _line = data[args.line[i]] lineTitle[i] = _line['line title'] colour[i] = _line.colour note[i] = args.note[i] or '' if type(_line['left terminus']) == 'string' then _leftTerminus = _line['left terminus'] elseif type(_line['left terminus']) == 'table' then _leftTerminus = _line['left terminus'][args.TypeL[i] or 1] end if type(_line['left terminus']) == 'string' then _rightTerminus = _line['right terminus'] elseif type(_line['left terminus']) == 'table' then _rightTerminus = _line['right terminus'][args.TypeR[i] or 1] end if not args.left[i] then left[i] = "Terminus" leftTerminus[i] = '' elseif args.left[i] == _leftTerminus then left[i] = _line['station link'](args.left[i]) leftTerminus[i] = subFormat('Terminus') else left[i] = _line['station link'](args.left[i]) if args.onewayL[i] == true then leftTerminus[i] = subFormat('One-way operation') else leftTerminus[i] = subFormat('towards ' .. _line['station link'](_leftTerminus)) end end if not args.right[i] then right[i] = "Terminus" rightTerminus[i] = '' elseif args.right[i] == _rightTerminus then right[i] = _line['station link'](args.right[i]) rightTerminus[i] = subFormat('Terminus') else right[i] = _line['station link'](args.right[i]) if args.onewayR[i] == true then rightTerminus[i] = subFormat('One-way operation') else rightTerminus[i] = subFormat('towards ' .. _line['station link'](_rightTerminus)) end end table.insert(t, table.concat({'\n|-' , '\n|', style['body leftcell'], left[i], leftTerminus[i] , '\n|', style['body banner'], colour[i], '"| '		, '\n|', style['body midcell'], lineTitle[i], ' ', note[i], ' ' 			, '\n|', style['body banner'], colour[i], '"| ' , '\n|', style['body rightcell'], right[i], rightTerminus[i] }))	end return table.concat(t) end

function p.bottom return '\n|}' end

function p.main(frame) local args = { ['system'] = frame:getParent.args.system , ['line'] = {} , ['left'] = {} , ['right'] = {} , ['TypeL'] = {} , ['TypeR'] = {} , ['note'] = {} , ['onewayL'] = {} , ['onewayR'] = {} , ['circular'] = {} , ['circularL'] = {} , ['circularR'] = {} }	local data = require('Module:L-rail/' .. args.system)

for k, v in pairs(frame:getParent.args) do		local a = string.match(k, '(%l+)%d+') local b = tonumber(string.match(k, '%l+(%d+)')) if a == 'line' then args.line[b] = v		elseif a == 'left' then args.left[b] = v		elseif a == 'right' then args.right[b] = v		elseif a == 'typeL' then args.TypeL[b] = v		elseif a == 'typeR' then args.TypeR[b] = v		elseif a == 'note' then args.note[b] = v		elseif a == 'onewayL' then args.onewayL[b] = yesno(v) elseif a == 'onewayR' then args.onewayR[b] = yesno(v) elseif a == 'circular' then args.circular[b] = yesno(v) elseif a == 'circularL' then args.circularL[b] = yesno(v) elseif a == 'circularR' then args.circularR[b] = yesno(v) elseif a == 'throughL' then args.throughL = yesno(v) elseif a == 'throughR' then args.throughR = yesno(v) end end local _rows = frame:getParent.args.rows or 'all' local rows = { ['all'] = table.concat({p.top, p.rail(data), p.line(data, args), p.bottom}) , ['top'] = table.concat({p.top, p.rail(data), p.line(data, args)}) , ['mid'] = table.concat({p.rail(data), p.line(data, args)}) , ['bottom'] = table.concat({p.rail(data), p.line(data, args), p.bottom}) }	return rows[_rows] end

return p