Jump to content

Module:Adjacent stations

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Szqecs (talk | contribs) at 03:35, 21 April 2018 (Created page with '-- Function allowing for consistent treatment of boolean-like wikitext input. -- It works similarly to the template {{yesno}}. local function yesno(val, default...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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

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({'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 '<div style="font-size: smaller; font-style: italic">' .. s .. '</div>'
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 banner']
		, '\n!', style['header midcell'], data['system title']
		, '\n!', style['header banner']
		, '\n!', style['header rightcell'], 'Following station'
	})
end

function p.line(data, args)
	local lineTitle = {}
	local colour = {}
	local left = {}
	local right = {}
	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
		
		if not args.left[i] then
			left[i] = "''Terminus''"
			leftTerminus[i] = ''
		elseif args.left[i] == _line['left terminus']['default'] 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
				local s = _line['left terminus'][args.Type[i] or 'default']
				if _line['station link'](s) then
					leftTerminus[i] = subFormat('towards ' .. _line['station link'](s))
				end
			end
		end
		
		if not args.right[i] then
			right[i] = "''Terminus''"
			rightTerminus[i] = ''
		elseif args.right[i] == _line['right terminus'][args.Type[i] or 'default'] 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
				local s = _line['right terminus'][args.Type[i] or 'default']
				if _line['station link'](s) then
					rightTerminus[i] = subFormat('towards ' .. _line['station link'](s))
				end
			end
		end
		table.insert(t, table.concat({'\n|-'
			, '\n|', style['body leftcell'], left[i], leftTerminus[i]
			, '\n|', style['body banner'], colour[i], '"|&nbsp;'
			, '\n|', style['body midcell'], lineTitle[i]
			, '\n|', style['body banner'], colour[i], '"|&nbsp;'
			, '\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.args.system
		, ['line'] = {}
		, ['Type'] = {}
		, ['left'] = {}
		, ['right'] = {}
		, ['onewayL'] = {}
		, ['onewayR'] = {}
		, ['circular'] = {}
		, ['circularL'] = {}
		, ['circularR'] = {}
	}
	local data = require('Module:L-rail/' .. args.system)

	for k, v in pairs(frame.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 == 'type' then
			args.Type[b] = v
		elseif a == 'left' then
			args.left[b] = v
		elseif a == 'right' then
			args.right[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.args.rows or 'all'
	local row = {
		['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 row[rows]
end

return p