Module:Adjacent stations

require('Module:No globals')

local p = {}

local lang = 'en-GB' -- local default language

--	Below these comments: Internationalization table --	How to translate this module (for languages without variants): --	• Characters inside single and double quotation marks are called strings. --	 The strings in this i18n table are used as output. --	• Strings within square brackets are keys. --	• Strings are concatenated (joined) with two dots. --	• Set the string after «local lang =» to your language's code. --	 Change the first key after "i18n" (usually "en-GB") to the same thing. --	• For each string which is not inside a function, translate it directly. --	• Strings with keys named "format" are Lua regular expressions. --	 «» is a match; «.+» means all characters; «%s+» means all spaces. --	• For each string which is concatenated to the variable «var», --	 translate the phrase assuming that «var» will be a noun. --	• Remove any unnecessary translations.

local i18n = { ['en-GB'] = { --		['word_space'] = ' ', ['preceding'] = function(var) return 'Preceding ' .. var end, ['following'] = function(var) return 'Following ' .. var end, ['stop_noun'] = 'station', ['nonstop_past'] = function(var) return var .. ' did not stop here' end, ['nonstop_present'] = function(var) return var .. ' does not stop here' end, ['comma'] = function(var) return ', ' .. var end, ['or'] = function(var) return ' or ' .. var end, ['via-first'] = false, -- If the «via» text comes before termini, change to «true» ['via'] = function(var) return ' via ' .. var end, ['comma-format'] = ',%s+', ['or-format'] = '%s+or%s+', ['via-format'] = '%s+via%s+(.+)$', -- first match is station name ['towards'] = function(var) return 'towards ' .. var end, ['through'] = function(var) return 'through to ' .. var end, ['reverse'] = 'Reverses direction', ['oneway'] = 'One-way operation', ['terminus'] = 'Terminus', ['error_duplicate'] = function(var) return 'Same row number used multiple times for ' .. var end, ['error_format'] = 'Station format table missing in data page', ['error_line'] = 'Lines table missing in data module', ['error_missing'] = function(var) return '"' .. var .. '" is missing from the data page' end, ['error_unknown'] = function(var) return 'Unknown line name "' .. var .. '"; change the input or add the line to the data page' end },	['en-US'] = { --		['word_space'] = ' ', ['preceding'] = function(var) return 'Preceding ' .. var end, ['following'] = function(var) return 'Following ' .. var end, ['stop_noun'] = 'station', ['nonstop_past'] = function(var) return var .. ' did not stop here' end, ['nonstop_present'] = function(var) return var .. ' does not stop here' end, ['comma'] = function(var) return ', ' .. var end, ['or'] = function(var) return ' or ' .. var end, ['via-first'] = false, -- If the «via» text comes before termini, change to «true» ['via'] = function(var) return ' via ' .. var end, ['comma-format'] = ',%s+', ['or-format'] = '%s+or%s+', ['via-format'] = '%s+via%s+(.+)$', -- first match is station name ['towards'] = function(var) return 'toward ' .. var end, ['through'] = function(var) return 'through to ' .. var end, ['reverse'] = 'Reverses direction', ['oneway'] = 'One-way operation', ['terminus'] = 'Terminus', ['error_duplicate'] = function(var) return 'Same row number used multiple times for ' .. var end, ['error_format'] = 'Station format table missing in data module', ['error_line'] = 'Lines table missing in data module', ['error_missing'] = function(var) return '"' .. var .. '" is missing from the data page' end, ['error_unknown'] = function(var) return 'Unknown line name "' .. var .. '"; change the input or add the line to the data page' end } }

local require = require

local function getData(system, verify) if verify then local title = mw.title.new('Module:Adjacent stations/' .. system -- .. '/sandbox'			) if not (title and title.exists) then return nil end end return require('Module:Adjacent stations/' .. system -- .. '/sandbox'		) end

local lower = mw.ustring.lower

local function getLine(data, line) if line then return data['aliases'] and data['lines'][data['aliases'][lower(line)]] or data['lines'][line] end end

local function getColor(data, system, line, branch, frame) if system then if line then return frame:expandTemplate{ title = system .. ' color', args = {line, ['branch'] = branch} } end return frame:expandTemplate{ title = system .. ' color' } else line = getLine(data, line) if line then local color = line['color'] or line['background color'] or data['color'] local branch_value = branch and line['branches'] and (line['branches'][branch] and line['branches'][branch]['color']) if branch_value then color = branch_value end return color end return data['color'] or '' end end

local gsub = mw.ustring.gsub local match = mw.ustring.match local concat = table.concat local _line, _branch

local function getStation(station, _Format) if type(_Format) == 'table' then _Format = _Format[_line] or _Format[1] if type(_Format) == 'table' then _Format = _Format[_branch] or _Format[1] end end if _branch then _Format = gsub(_Format, '%%3', _branch) end if _line then _Format = gsub(_Format, '%%2', _line) end return match(_Format, '%[%[') and gsub(_Format, '%%1', station) or concat({, station, }) end

function p._main(_args) -- Arguments are processed here instead of the main function local error = error local insert = table.insert local ipairs = ipairs local lower = mw.ustring.lower local pairs = pairs local tonumber = tonumber local type = type

local yesno = require("Module:Yesno") local boolean = { ['oneway-left'] = true, ['oneway-right'] = true, ['reverse'] = true, ['reverse-left'] = true, ['reverse-right'] = true }

local args = {} -- Processed arguments local index = {} -- A list of addresses corresponding to number suffixes in the arguments

for k, v in pairs(_args) do -- Maps each raw argument to processed arguments by string matching _args[k] = v:match('^%s*(.-)%s*$') if _args[k] and _args[k] ~= '' then local a = match(k, '^(.*%D)%d+$') or k -- The parameter; address 1 can be omitted local b = tonumber(match(k, '^.*%D(%d+)$')) or 1 -- The address for a given argument; address 1 can be omitted

if boolean[a] then v = yesno(v) end

if not args[b] then args[b] = {[a] = v}				insert(index, b)			elseif args[b][a] then return error(i18n[lang]['error_duplicate'](a .. b)) else args[b][a] = v			end end end table.sort(index)

local function small(s, italic) return italic and ' ' .. s .. ' '			or ' ' .. s .. ' '	end

local style = { -- Style for each cell type ['header cell'] = 'class="hcA"|', ['header midcell'] = 'colspan="3" class="hmA"|', ['body cell'] = 'class="bcA"|', ['body banner'] = 'class="bbA" style="background-color:#',	}

local Format local function subst(var1, var2) -- var1 is the terminus or table of termini; var2 is the key for the table of termini return type(var1) == 'string' and getStation(var1, (Format[var1] or Format[1])) or type(var1) == 'table' and #var1 > 0 and getStation(var1[var2], (Format[var1[var2]] or Format[1])) or '' end

local function station(var) if Format then if type(var) == 'string' then return subst(var) elseif type(var) == 'table' and #var > 0 then local t = {subst(var, 1)}

for i = 2, #var - 1 do					t[i] = i18n[lang]['comma'](subst(var, i)) end

if #var > 1 then t[#var] = i18n[lang]['or'](subst(var, #var)) end if var['via'] then if i18n[lang]['via-first'] then table.insert(t, 1, i18n[lang]['via'](subst(var, 'via'))) else table.insert(t, i18n[lang]['via'](subst(var, 'via'))) end end

return concat(t) else return '' end else return var or '' end end

local function rgb(var) if var:len == 3 then return {tonumber(var:sub(1, 1), 16) * 17, tonumber(var:sub(2, 2), 16) * 17, tonumber(var:sub(2, 2), 16) * 17} elseif var:len == 6 then return {tonumber(var:sub(1, 2), 16), tonumber(var:sub(3, 4), 16), tonumber(var:sub(5, 6), 16)} end return {} end

local data = {} -- A table of data modules for each address local wikitable = {'{| class="wikitable adjacent-stations"'}

for i, v in ipairs(index) do		-- If an address has a system argument, indexes the data module data[v] = args[v]['system'] and getData(args[v]['system']) -- If an address has no system, the row uses data from the previous address. or data[index[i - 1]]

local lang = data[v]['lang'] or lang

if args[v]['system'] then -- Header row local stop_noun = data[v]['header stop noun'] or i18n[lang]['stop_noun'] insert(wikitable, concat({'\n|-', '\n!', style['header cell'], i18n[lang]['preceding'](stop_noun), '\n!', style['header midcell'], (data[v]['system title'] or (.. args[v]['system'] ..)), '\n!', style['header cell'], i18n[lang]['following'](stop_noun) }))			insert(wikitable, '') insert(wikitable, '') insert(wikitable, '') end

if args[v]['header'] then -- Subheader insert(wikitable, '\n|-\n!colspan="5" class="hmA"|'.. args[v]['header']) insert(wikitable, '') insert(wikitable, '') insert(wikitable, '') end

if args[v]['line'] or args[v]['left'] or args[v]['right'] or args[v]['nonstop'] then if not args[v]['line'] and i > 1 then args[v]['line'] = args[index[i - 1]]['line'] end

_line = args[v]['line'] _branch = args[v]['branch'] if data[v]['aliases'] then _line = data[v]['aliases'][lower(_line)] or _line if _branch then _branch = data[v]['aliases'][lower(_branch)] or _branch end end

local line = data[v]['lines'] and (data[v]['lines'][_line] or error(i18n[lang]['error_unknown'](_line))) or error(i18n[lang]['error_line'])

if args[v]['nonstop'] then insert(wikitable,					concat({'\n|-\n|colspan="5" ', style['body cell'], ((args[v]['nonstop'] == 'former') and i18n[lang]['nonstop_past'] or i18n[lang]['nonstop_present'])(p._box({data = data[v], line = _line, branch = _branch, inline = 'yes'})) })				)				insert(wikitable, '') insert(wikitable, '') insert(wikitable, '') else Format = data[v]['station format'] or i18n[lang]['error_format']

local color, background_color

if line['branches'] and line['branches'][_branch] then if line['branches'][_branch]['color'] then background_color = line['branches'][_branch]['background color'] or line['color'] color = line['branches'][_branch]['color'] elseif line['branches'][_branch]['background color'] then background_color = line['branches'][_branch]['background color'] color = line['color'] or data[v]['color'] or '' end else background_color = line['background color'] color = line['color'] or data[v]['color'] or '' end

-- Alternate termini can be specified based on type local sideCell = {true, true} for i, b in ipairs({'left', 'right'}) do					if not args[v][b] then -- If no station is given on one side, the station is assumed to be the terminus on that side local _through = args[v]['through-' .. b] or args[v]['through'] sideCell[i] = _through and "''" .. i18n[lang]['through'](data[v]['lines'][_through]['line title']) .. ""							or "" .. ((args[v]['reverse-' .. b]							or args[v]['reverse']) and i18n[lang]['reverse']							or i18n[lang]['terminus']) .. "''"					else local terminus local _terminus = line[b .. ' terminus']

if type(_terminus) == 'string' or (type(_terminus) == 'table' and (_terminus[2] or _terminus['via'])) then -- If the terminus table has more than one numbered key or has the via key then the table shows only the default termini, since _terminus[2] cannot be used and _terminus[via] is reserved if args[v]['type-' .. b] then terminus = args[v]['type-' .. b] local _or = match(args[v]['type-' .. b], i18n[lang]['or-format']) if _or then terminus = gsub(terminus, i18n[lang]['or-format'], '\127_OR_\127') terminus = gsub(terminus, i18n[lang]['comma-format'], '\127_OR_\127') end local _via = (match(terminus, i18n[lang]['via-format'])) if _via then terminus = gsub(terminus, i18n[lang]['via-format'], '') terminus = mw.text.split(terminus, '\127_OR_\127') terminus['via'] = _via elseif _or then terminus = mw.text.split(terminus, '\127_OR_\127') end else terminus = _terminus end elseif type(_terminus) == 'table' then terminus = _terminus[args[v]['type-' .. b]] or _terminus[args[v]['type']] or _terminus[1] end

local mainText = args[v]['note-' .. b] and station(args[v][b]) .. small(args[v]['note-' .. b]) or station(args[v][b])

local subText = (args[v]['oneway-' .. b] or line['oneway-' .. b]) and i18n[lang]['oneway'] or args[v][b] == terminus and i18n[lang]['terminus'] or line['circular'] and terminus or i18n[lang]['towards'](station(terminus)) subText = small(subText, true)

sideCell[i] = mainText .. subText end end

insert(wikitable, '\n|-') insert(wikitable, '\n|' .. style['body cell'] .. sideCell[1]) insert(wikitable, concat({'\n|', style['body banner'], color, '"|',					'\n|', (background_color and 'class="bcA" style="background-color:rgba(' .. concat(rgb(background_color), ',') .. ',.2)"|' or style['body cell']), line['line title'],

-- Branch; table key 'branches' in subpages (data type table, with strings as keys). If table does not exist then the input is displayed as the text (_branch and ' ' .. (line['branches'] and line['branches'][_branch] and line['branches'][_branch]['branch title'] or _branch) .. ' ' or ''),

-- Note-mid; no table key. The input is displayed as the text (args[v]['note-mid'] and small(args[v]['note-mid']) or ''),

-- Transfer; uses system's station link table (args[v]['transfer'] and small('transfer at ' .. station(args[v]['transfer']), true) or ''),

'\n|', style['body banner'], color, '"|'}))				insert(wikitable, '\n|' .. style['body cell'] .. sideCell[2])			end		end

if args[v]['note-row'] then -- Note insert(wikitable, '\n|-\n|colspan="5" ' .. style['body cell'] .. args[v]['note-row']) insert(wikitable, '') insert(wikitable, '') insert(wikitable, '') end end

local function combine(t, n)		if t[n + 4] ~= '' and t[n + 4] == t[n] then t[n + 4] = '' -- The cell in the next row is deleted local rowspan = 2 while t[n + rowspan * 4] == t[n] do				t[n + rowspan * 4] = '' rowspan = rowspan + 1 end t[n] = gsub(t[n], '\n|class="', '\n|rowspan="' .. rowspan .. '" class="') end end

local M = #wikitable for i = 3, M, 4 do combine(wikitable, i) end for i = 4, M, 4 do combine(wikitable, i) end for i = 5, M, 4 do combine(wikitable, i) end

insert(wikitable, '\n|}')

return concat(wikitable) end

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

local function makeInvokeFunction(funcName) -- makes a function that can be returned from #invoke, using -- Module:Arguments. return function (frame) local args = getArgs(frame, {parentOnly = true}) return p[funcName](args, frame) end end

p.main = makeInvokeFunction('_main')

function p._color(args, frame) local data = args.data if args[1] or data then data = data or getData(args[1], true) if not data then return getColor(nil, args[1], args[2], args[3], frame) end return getColor(data, nil, args[2], args[3]) end end

p.color = makeInvokeFunction('_color')

function p._box(args, frame) local system = args[1] or args.system local _line = args[2] or args.line if not (system or _line) then return '' end local line, branch, line_data local inline = args[3] or args.inline local _branch = args.branch local data = args.data if system or data then data = data or getData(system, true) local color if data then _line = data['aliases'] and data['aliases'][lower(_line)] or _line if _branch then _branch = data['aliases'] and data['aliases'][lower(_branch)] or _branch line = data['lines'][_line] branch = line['branches'] and line['branches'][_branch] and line['branches'][_branch]['branch title'] or _branch end color = getColor(data, nil, _line, _branch) if inline ~= 'box' then line_data = data['lines'][_line] or error(i18n[lang]['error_unknown'](_line)) line = line_data['line title'] or error(i18n[lang]['error_missing']('line title')) end else color = getColor(nil, system, _line, args.branch, frame) if inline ~= 'box' then line = frame:expandTemplate{ title = system .. ' lines', args = {_line, ['branch'] = branch} } if mw.text.trim(line) == '' then return error(i18n[lang]['error_unknown'](_line)) end end end

local function square(var) -- Template:Color box return '      ' end

local result

if branch and inline ~= 'box' then if line == '' then line = branch else result = ' – ' .. branch end end if args.note then result = (result or '') .. ' ' .. args.note end result = result or ''

if not inline then -- Template:Legend result = '   ' .. line .. result .. ' '		elseif inline == 'yes' then result = square(color) .. ' ' .. line .. result elseif inline == 'box' then result = square(color) .. result elseif inline == 'link' then local link = mw.ustring.match(line, '%[%[([^%[:|%]]+)[|%]]') if link then result =  .. square(color) ..  .. result else result = square(color) .. result end elseif inline == 'small' then result = '   ' .. ' ' .. line .. result else local yesno = require("Module:Yesno") local link = mw.ustring.match(line, '%[%[([^%[:|%]]+)[|%]]') local border_color, text_color if line_data then if line_data['branches'] and line_data['branches'][_branch] then local branch_data = line_data['branches'][_branch] border_color = branch_data['border color'] or line_data['border color'] or color text_color = branch_data['text color'] or line_data['text color'] _line = branch_data['short name'] or line_data['short name'] or _line else border_color = line_data['border color'] or color text_color = line_data['text color'] _line = line_data['short name'] or _line end else border_color = color end local greatercontrast = require('Module:Color contrast')._greatercontrast text_color = text_color and '#' .. text_color or greatercontrast{color} local bold = (yesno(args.bold) == false) or ';font-weight:bold' if inline == 'route' then -- Template:RouteBox if link then result = '' .. _line .. ' ' else result = '' .. _line .. ' '				end elseif inline == 'croute' then -- Template:Bahnlinie if link then result = '' .. _line .. ' ' else result = '' .. _line .. ' '				end elseif inline == 'xroute' then -- Template:Bahnlinie if link then result = ' ' .. _line .. ' ' else result = '' .. _line .. ' '				end else -- Template:Legend (fallback; duplication to simplify logic) result = '   ' .. line .. result .. ' '			end end

result = mw.ustring.gsub(result, ':%s*#transparent', ':transparent')

return result end end

p.box = makeInvokeFunction('_box')

function p._line(args, frame) local system = args[1] or args.system local line = args[2] or args.line if not line then return '' end local branch = args[3] or args.branch local data = args.data if system or data then data = data or getData(system, true) if data then line = getLine(data, line) or error(i18n[lang]['error_unknown'](line)) if branch then branch = data['aliases'] and data['aliases'][lower(branch)] or branch branch = line['branches'] and line['branches'][branch] and line['branches'][branch]['branch title'] or branch end line = line['line title'] or error(i18n[lang]['error_missing']('line title')) else line = frame:expandTemplate{ title = system .. ' lines', args = {line, ['branch'] = branch} } if mw.text.trim(line) == '' then return error(i18n[lang]['error_unknown'](_line)) end end

if branch then if line == '' then line = branch else line = line .. ' – ' .. branch end end return line end end

p.line = makeInvokeFunction('_line')

function p._station(args, frame) local system = args[1] or args.system local station = args[2] or args.station if not station then return '' end local _line = args[3] or args.line local _branch = args[4] or args.branch local data = args.data if system or data then data = data or getData(system, true) if data then local _Format = data['station format'][station] or data['station format'][1] if _Format then if data['aliases'] then if _line then _line = data['aliases'][lower(_line)] or _line end if _branch then _branch = data['aliases'][lower(_branch)] or _branch end end station = getStation(station, _Format) else station = station or '' end else station = frame:expandTemplate{ title = system .. ' stations', args = {['station'] = station, ['line'] = _line, ['branch'] = _branch} } end

return station end end

p.station = makeInvokeFunction('_station')

function p._style(args, frame) local style = args[1] or args.style local system = args[2] or args.system local line = args[3] or args.line local station = args[4] or args.station local result = {} local data = args.data local default = 'background-color:#efefef' -- Default background color for. if system or data then data = data or getData(system, true) end if data then local function getValue(var) if type(var) == 'table' then var = var[line] or var[1] if type(var) == 'table' then var = var[station] or var[1] end end if var ~= '' then return var end end

if style == 'header' then local tmp = data['name format'] and getValue(data['name format']) if tmp then table.insert(result, tmp) end elseif style == 'subheader' then local tmp = data['header background color'] and getValue(data['header background color']) if tmp then table.insert(result, 'background-color:#' .. tmp) local color = data['header text color'] and getValue(data['header text color']) if color then table.insert(result, 'color:#' .. color) else local greatercontrast = require('Module:Color contrast')._greatercontrast if greatercontrast{tmp} == '#FFFFFF' then table.insert(result, 'color:#FFFFFF') end end else table.insert(result, default) local color = data['header text color'] and getValue(data['header text color']) if color then table.insert(result, 'color:#' .. color) end end end result = table.concat(result, ';') elseif system then local title = 'Template:' .. system .. ' style' local titleObj = mw.title.new(title) if titleObj and titleObj.exists then local tmp if style == 'header' then tmp = frame:expandTemplate{ title = title, args = {'name_format', line, station} } if tmp ~= '' then table.insert(result, tmp) end elseif style == 'subheader' then tmp = frame:expandTemplate{ title = title, args = {'thbgcolor', line, station} } if tmp ~= '' then table.insert(result, 'background-color:#' .. tmp) local color = frame:expandTemplate{ title = title, args = {'thcolor', line, station} } if color ~= '' then table.insert(result, 'color:#' .. color) else local ratio = require('Module:Color contrast')._ratio if ratio{tmp, '222222'} < 4.5 then table.insert(result, 'color:#FFFFFF') end -- 222222 is the default text color in Vector end else table.insert(result, default) tmp = frame:expandTemplate{ title = title, args = {'thcolor', line, station} } if tmp ~= '' then table.insert(result, 'color:#' .. tmp) end end end result = table.concat(result, ';') else if style == 'subheader' then result = default else result = '' end end else if style == 'subheader' then result = default else result = '' end end

return result end

function p.style(frame) local args = getArgs(frame, {frameOnly = true}) return p._style(args, frame) end

return p