Module:Adjacent stations: Difference between revisions

Content deleted Content added
m error message is too cryptic if it always says "_default"
No edit summary
Line 5:
local lang = 'en-GB' -- local default language
 
-- Below these comments: InternationalizationInternationalisation table
-- How to translate this module (for languages without variants):
-- • Characters inside single and double quotation marks are called strings.
Line 67:
end,
['error_unknown'] = function(var)
return 'Unknown linesystem nameor line "' .. (var or '') .. '"; change the input or add the system or line to the data page'
end
},
Line 116:
end,
['error_unknown'] = function(var)
return 'Unknown linesystem nameor line "' .. (var or '') .. '"; change the input or add the system or line to the data page'
end
}
Line 122:
 
local require = require
local lower = mw.ustring.lower
local gsub = mw.ustring.gsub
local Type = type
local type = nil
 
local function getData(system, verify)
Line 132 ⟶ 136:
)
end
 
local lower = mw.ustring.lower
local gsub = mw.ustring.gsub
 
local function getLine(data, _line)
Line 150 ⟶ 151:
end
 
local function getColor(data, system, line, Typetype, frame)
if system then
if line then return frame:expandTemplate{ title = system .. ' color', args = {line, ['branch'] = Typetype} } end
return frame:expandTemplate{ title = system .. ' color' }
else
Line 158 ⟶ 159:
if line then
local color = line['color'] or line['background color'] or data['color']
local Type_valuetype_value = Typetype and line['types'] and (line['types'][Typetype] and line['types'][Typetype]['color'])
if Type_valuetype_value then color = Type_valuetype_value end
return color
end
Line 168 ⟶ 169:
local match = mw.ustring.match
local concat = table.concat
local _line, _Type_type
 
local function getStation(station, _Format)
if typeType(_Format) == 'table' then
_Format = _Format[_line] or _Format[1]
if typeType(_Format) == 'table' then
_Format = _Format[_Type_type] or _Format[1]
end
end
if _Type_type then _Format = gsub(_Format, '%%3', _Type_type) end
if _line then _Format = gsub(_Format, '%%2', _line) end
return (match(_Format, '%[%[.+%]%]')) and (gsub(_Format, '%%1', station)) or concat({'[[', gsub(_Format, '%%1', station), '|', station, ']]'})
Line 235 ⟶ 236:
local function subst(var1, var2)
-- var1 is the terminus or table of termini; var2 is the key for the table of termini
return typeType(var1) == 'string' and getStation(var1, (Format[var1] or Format[1]))
or typeType(var1) == 'table' and #var1 > 0 and getStation(var1[var2], (Format[var1[var2]] or Format[1]))
or ''
end
Line 242 ⟶ 243:
local function station(var)
if Format then
if typeType(var) == 'string' then
return subst(var)
elseif typeType(var) == 'table' and #var > 0 then
local t = {subst(var, 1)}
 
Line 286 ⟶ 287:
-- If an address has no system, the row uses data from the previous address
or data[index[i - 1]]
or error(i18n[lang]['error_unknown'](args[v]['system']))
 
data[v]['aliases'] = data[v]['line/type aliases'] or data[v]['aliases']
data[v]['color'] = data[v]['line color'] or data[v]['color']
data[v]['icon'] = data[v]['system icon'] or data[v]['icon']
data[v]['icon format'] = data[v]['system icon format'] or data[v]['icon format']
local lang = data[v]['lang'] or lang
 
Line 293 ⟶ 300:
insert(wikitable, concat({'\n|-',
'\n!', style['header cell'], i18n[lang]['preceding'](stop_noun),
'\n!', style['header midcell'], (data[v]['system icon'] and data[v]['system icon'] .. ' ' or ''), (data[v]['system title'] or ('[['.. args[v]['system'] ..']]')),
'\n!', style['header cell'], i18n[lang]['following'](stop_noun)
}))
Line 314 ⟶ 321:
 
_line = args[v]['line'] or '_default'
_Type_type = args[v]['type']
if data[v]['aliases'] then
_line = data[v]['aliases'][lower(_line)] or _line
if _Type_type then _Type_type = data[v]['aliases'][lower(_Type_type)] or _Type_type end
end
 
Line 331 ⟶ 338:
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, Typetype = _Type_type, inline = 'yes'}))
})
)
Line 341 ⟶ 348:
 
local color, background_color
local Typetype = line['types'] and line['types'][_Type_type] -- get the line type table
 
if Typetype then
if Typetype['color'] then
-- line color is used as background if there is no background color in the line type table
background_color = Typetype['background color'] or line['color']
color = Typetype['color']
elseif Typetype['background color'] then
background_color = Typetype['background color']
color = line['color'] or default['color'] or data[v]['color'] or ''
else
Line 355 ⟶ 362:
color = line['color'] or default['color'] or data[v]['color'] or ''
end
type['type title'] = type['title'] or type['type title']
else
background_color = line['background color']
color = line['color'] or default['color'] or data[v]['color'] or ''
end
line['line title'] = line['title'] or line['line title']
 
-- Alternate termini can be specified based on type
Line 376 ⟶ 386:
 
-- 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 typeType(_terminus) == 'string' or (typeType(_terminus) == 'table' and (_terminus[2] or _terminus['via'])) then
if args[v]['to-' .. b] then
terminus = args[v]['to-' .. b]
Line 395 ⟶ 405:
terminus = _terminus
end
elseif typeType(_terminus) == 'table' then
terminus = _terminus[args[v]['to-' .. b]] or _terminus[args[v]['to']] or _terminus[1]
end
Line 416 ⟶ 426:
'\n|', (background_color and 'class="bcA" style="background-color:rgba(' .. concat(rgb(background_color), ',') .. ',.2)"|' or style['body cell']), line['line title'],
 
-- Typetype; table key 'types' in subpages (datatype table, with strings as keys). If table does not exist then the input is displayed as the text
(_Type_type and '<div>' .. (Typetype and Typetype['type title'] or _Type_type) .. '</div>' or ''),
 
-- Note-mid; table key 'note-mid' in subpages. Overridden by user input
((args[v]['note-mid'] and small(args[v]['note-mid'])) or (Typetype and Typetype['note-mid'] and small(Typetype['note-mid'])) or (line['note-mid'] and small(line['note-mid'])) or ''),
 
-- Transfer; uses system's station link table
Line 488 ⟶ 498:
local _line = args[2] or args.line
if not (system or _line) then return '' end
local line, Typetype, line_data
local inline = args[3] or args.inline
local _Type_type = args.type
local data = args.data
if system or data then
Line 497 ⟶ 507:
if data then
line, _line = getLine(data, _line)
if _Type_type then
_Type_type = data['aliases'] and data['aliases'][lower(_Type_type)] or _Type_type
Typetype = line['types'] and line['types'][_Type_type] and line['types'][_Type_type]['type title'] or _Type_type
end
color = getColor(data, nil, _line, _Type_type)
if inline ~= 'box' then
line_data = line or error(i18n[lang]['error_unknown'](_line))
Line 507 ⟶ 517:
end
else
color = getColor(nil, system, _line, _Type_type, frame)
if inline ~= 'box' then
line = frame:expandTemplate{ title = system .. ' lines', args = {_line, ['branch'] = _Type_type} }
if mw.text.trim(line) == '' then return error(i18n[lang]['error_unknown'](_line)) end
end
Typetype = _Type_type
end
 
local result
 
if Typetype and Typetype ~= '' and inline ~= 'box' then
if line == '' then
line = Typetype
else
result = ' – ' .. Typetype
end
end
Line 565 ⟶ 575:
local border_color, text_color
if line_data then
if line_data['types'] and line_data['types'][_Type_type] then
local Type_datatype_data = line_data['types'][_Type_type]
border_color = Type_datatype_data['border color'] or line_data['border color'] or color
text_color = Type_datatype_data['text color'] or line_data['text color']
_line = Type_datatype_data['short name'] or line_data['short name'] or _line
else
border_color = line_data['border color'] or color
Line 615 ⟶ 625:
local system = args[1] or args.system
local line = args[2] or args.line
local Typetype = args[3] or args.type
local data = args.data
if system or data then
Line 625 ⟶ 635:
 
if line then
if Typetype then
Typetype = data['aliases'] and data['aliases'][lower(Typetype)] or Typetype
Typetype = line['types'] and line['types'][Typetype] -- If there's no type table or entry for this type, then it can't have its own icon
Format = Typetype['icon format'] or data['type icon format']
icon = Typetype['icon']
end
if not (Format or icon) then
Line 642 ⟶ 652:
 
if Format then
if Format ~= 'image' then return p._box({data = data, [2] = (args[2] or args.line), [3] = Format, typeType = (args[3] or args.type), bold = args.bold, link = args.link}, frame) end
end
 
Line 695 ⟶ 705:
local line = args[2] or args.line
if not line then return '' end
local Typetype = args[3] or args.type
local data = args.data
if system or data then
Line 701 ⟶ 711:
if data then
line = (getLine(data, line)) or error(i18n[lang]['error_unknown'](line))
if Typetype then
Typetype = data['aliases'] and data['aliases'][lower(Typetype)] or Typetype
Typetype = line['types'] and line['types'][Typetype] and line['types'][Typetype]['type title'] or Typetype
end
line = line['line title'] or error(i18n[lang]['error_missing']('line title'))
else
line = frame:expandTemplate{ title = system .. ' lines', args = {line, ['branch'] = Typetype} }
if mw.text.trim(line) == '' then return error(i18n[lang]['error_unknown'](_line)) end
end
 
if Typetype then
if line == '' then
line = Typetype
else
line = line .. ' – ' .. Typetype
end
end
Line 729 ⟶ 739:
if not station then return '' end
local _line = args[3] or args.line
local _Type_type = args[4] or args.type
local data = args.data
if system or data then
Line 740 ⟶ 750:
_line = data['aliases'][lower(_line)] or _line
end
if _Type_type then
_Type_type = data['aliases'][lower(_Type_type)] or _Type_type
end
end
Line 749 ⟶ 759:
end
else
station = frame:expandTemplate{ title = system .. ' stations', args = {['station'] = station, ['line'] = _line, ['branch'] = _Type_type} }
end
 
Line 771 ⟶ 781:
if data then
local function getValue(var)
if typeType(var) == 'table' then
var = var[line] or var[1]
if typeType(var) == 'table' then
var = var[station] or var[1]
end