require('strict')  
local p = {}  
local lang = 'ta' -- 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 = require("Module:Adjacent stations/i18n")
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 function getLine(data, lineN)
if lineN then
if data['aliases'] then
lineN = data['aliases'][mw.ustring.lower(lineN)] or lineN
end
local default = data['lines']['_default'] or {}
local line = data['lines'][lineN] or {}
for k, v in pairs(default) do
if v then line[k] = line[k] or v end
end
line['title'] = line['title'] and mw.ustring.gsub(line['title'], '%%1', lineN)
return line, lineN
end
end  
local function getColor(data, system, line, Type, )
if system then
if line then return :expandTemplate{ title = system .. ' color', args = {line, ['branch'] = Type} } end
return :expandTemplate{ title = system .. ' color' }
else
line = (getLine(data, line))
local default = data['lines']['_default']
if line or default then
default = default or {}
if not line then line = mw.clone(default) end
local color = line['color'] or line['background color'] or default['color'] or default['background color'] or data['system color']
local Type_value = Type and line['types'] and (line['types'][Type] and line['types'][Type]['color'])
if Type_value then color = Type_value end
return color
end
return (default and (default['color'] or default['background color']) or data['system color'] or '')
end
end  
local lineN, typeN  
local function somethingMissing(name, key, formats)
local formatKeys = {}
for k in pairs(formats) do
table.insert(formatKeys, k)
end
return name .. ' was "' .. key .. '" but neither an entry for it nor a default was found. Choices were: ' .. table.concat(formatKeys, ', ')
end  
local function getStation(station, _Format)
if type(_Format) == 'table' then
local lineNformats = _Format
_Format = lineNformats[lineN] or lineNformats[1]
if not _Format then
error(somethingMissing('lineN', lineN, lineNformats))
elseif type(_Format) == 'table' then
local typeNformats = _Format
_Format = typeNformats[typeN] or typeNformats[1]
if not _Format then
error(somethingMissing('typeN', typeN, typeNformats))
end
end
end
if typeN then _Format = mw.ustring.gsub(_Format, '%%3', typeN) end
if lineN then _Format = mw.ustring.gsub(_Format, '%%2', lineN) end
return (mw.ustring.match(_Format, '%[%[.+%]%]')) and (mw.ustring.gsub(_Format, '%%1', station)) or table.concat({*, station, *})
end  
local function getTerminusText(var, 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  
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 table.concat(t)
else
return ''
end
else
return var or ''
end
end  
function p._main(_args) -- Arguments are processed here instead of the main function  
local yesno = require('Module:Yesno')
local trimq = require('Module:Trim quotes')._trim  
local boolean = {
['oneway-'] = true,
['oneway-'] = true,
['reverse'] = true,
['reverse-'] = true,
['reverse-'] = 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 = mw.ustring.match(k, '^(.*%D)%d+$') or k -- The parameter; address 1 can be omitted
local b = tonumber(mw.ustring.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}
table.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 '<div>' .. s .. '</div>'
or '<div>' .. s .. '</div>'
end  
local style = { -- Style for each cell type
['header cell'] = '|',
['header midcell'] = '|',
['body cell'] = '|',
['body banner'] = 'wikitable adjacent-stations' .. noclearclass .. '"'}  
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]]
or (args[v]['header'] and getData(args[index[i+1]]['system']))
or error(i18n[lang]['error_unknown'](args[v]['system']))  
local lang = data[v]['lang'] or lang  
if args[v]['system'] and not args[v]['hide-system'] then -- Header row
local stop_noun = data[v]['header stop noun'] or i18n[lang]['stop_noun']
table.insert(wikitable, table.concat({'\n|-',
'\n! scope="col" ', style['header cell'], i18n[lang]['preceding'](stop_noun),
'\n! scope="col" ', style['header midcell'], (data[v]['system icon'] and data[v]['system icon'] .. ' ' or ''), (data[v]['system title'] or ('[['.. args[v]['system'] ..']]')),
'\n! scope="col" ', style['header cell'], i18n[lang]['following'](stop_noun)
}))
table.insert(wikitable, '')
table.insert(wikitable, '')
table.insert(wikitable, '')
end  
if args[v]['header'] then -- Subheader
table.insert(wikitable, '\n|-\n!|'.. args[v]['header'])
table.insert(wikitable, '')
table.insert(wikitable, '')
table.insert(wikitable, '')
end  
if args[v]['line'] or args[v][*] or args[v][*] or args[v]['nonstop'] then
if not args[v]['line'] and i > 1 and not args[v]['system'] then
args[v]['line'] = args[index[i - 1]]['line']
end  
lineN = args[v]['line'] or '_default'
typeN = args[v]['type']
if data[v]['aliases'] then
lineN = data[v]['aliases'][mw.ustring.lower(lineN)] or lineN
if typeN then typeN = data[v]['aliases'][mw.ustring.lower(typeN)] or typeN end
end  
-- get the line table
local line = data[v]['lines'] and (mw.clone(data[v]['lines'][lineN]) or error(i18n[lang]['error_unknown'](args[v]['line']))) or error(i18n[lang]['error_line'])
local default = data[v]['lines']['_default'] or {}
line['title'] = line['title'] or default['title'] or ''
line['title'] = mw.ustring.gsub(line['title'], '%%1', lineN)  
-- cell across row for non-stop service
if args[v]['nonstop'] then
table.insert(wikitable,
table.concat({'\n|-\n| ',
style['body cell'],
((args[v]['nonstop'] == 'former') and i18n[lang]['nonstop_past'] or i18n[lang]['nonstop_present'])(p._box({data = data[v], line = lineN, Type = typeN, inline = 'yes'}))
})