Changes
Module:Adjacent stations (view source)
Revision as of 20:35, 29 January 2023
, 20:35, 29 January 2023Per edit request
require('Module:No globals')
require('strict')
local p = {}
local p = {}
if lineN then _Format = mw.ustring.gsub(_Format, '%%2', lineN) 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({'[[', mw.ustring.gsub(_Format, '%%1', station), '|', station, ']]'})
return (mw.ustring.match(_Format, '%[%[.+%]%]')) and (mw.ustring.gsub(_Format, '%%1', station)) or table.concat({'[[', mw.ustring.gsub(_Format, '%%1', station), '|', 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
end
local yesno = require('Module:Yesno')
local yesno = require('Module:Yesno')
local trimq = require('Module:Trim quotes')._trim
local trimq = require('Module:Trim quotes')._trim
local boolean = {
local boolean = {
['oneway-left'] = true,
['oneway-left'] = true,
['body banner'] = 'class="bbA" style="background-color:#',
['body banner'] = 'class="bbA" style="background-color:#',
}
}
local function rgb(var)
local function rgb(var)
local data = {} -- A table of data modules for each address
local data = {} -- A table of data modules for each address
local wikitable = {'{| class="wikitable adjacent-stations"'}
local noclearclass = (((_args.noclear or '') ~= '') and ' adjacent-stations-noclear' or '')
local wikitable = {'{| class="wikitable adjacent-stations' .. noclearclass .. '"'}
for i, v in ipairs(index) do
for i, v in ipairs(index) do
-- If an address has no system, the row uses data from the previous address
-- If an address has no system, the row uses data from the previous address
or data[index[i - 1]]
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']))
or error(i18n[lang]['error_unknown'](args[v]['system']))
local lang = data[v]['lang'] or lang
local lang = data[v]['lang'] or lang
if args[v]['system'] then -- Header row
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']
local stop_noun = data[v]['header stop noun'] or i18n[lang]['stop_noun']
table.insert(wikitable, table.concat({'\n|-',
table.insert(wikitable, table.concat({'\n|-',
'\n!', style['header cell'], i18n[lang]['preceding'](stop_noun),
'\n! scope="col" ', 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! scope="col" ', 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)
'\n! scope="col" ', style['header cell'], i18n[lang]['following'](stop_noun)
}))
}))
table.insert(wikitable, '')
table.insert(wikitable, '')
table.insert(wikitable, '')
table.insert(wikitable, '')
else
else
Format = data[v]['station format'] or i18n[lang]['error_format']
local Format = data[v]['station format'] or i18n[lang]['error_format']
local color, background_color, circular
local color, background_color, circular
end
end
local mainText = args[v]['note-' .. b] and station(args[v][b]) .. small(args[v]['note-' .. b]) or station(args[v][b])
local mainText = args[v]['note-' .. b] and getTerminusText(args[v][b], Format) .. small(args[v]['note-' .. b]) or getTerminusText(args[v][b], Format)
local subText = (args[v]['oneway-' .. b] or line['oneway-' .. b]) and i18n[lang]['oneway']
local subText = (args[v]['oneway-' .. b] or line['oneway-' .. b]) and i18n[lang]['oneway']
or args[v][b] == terminusT and i18n[lang]['terminus']
or args[v][b] == terminusT and i18n[lang]['terminus']
or circular and terminusT
or circular and terminusT
or i18n[lang]['towards'](station(terminusT))
or i18n[lang]['towards'](getTerminusText(terminusT, Format))
subText = small(subText, true)
subText = small(subText, true)
-- Transfer; uses system's station link table
-- Transfer; uses system's station link table
(args[v]['transfer'] and small('transfer at ' .. station(args[v]['transfer']), true) or ''),
(args[v]['transfer'] and small('transfer at ' .. getTerminusText(args[v]['transfer'], Format), true) or ''),
'\n|', style['body banner'], color, '"|'}))
'\n|', style['body banner'], color, '"|'}))
if args[v]['note-row'] then -- Note
if args[v]['note-row'] then -- Note
table.insert(wikitable, '\n|-\n|colspan="5" ' .. style['body cell'] .. args[v]['note-row'])
if args[v]['note-row']:match('^%s*<tr') or args[v]['note-row']:match('^%s*%|%-') then
table.insert(wikitable, '\n' .. args[v]['note-row'])
else
table.insert(wikitable, '\n|-\n|colspan="5" ' .. style['body cell'] .. args[v]['note-row'])
end
table.insert(wikitable, '')
table.insert(wikitable, '')
table.insert(wikitable, '')
table.insert(wikitable, '')
if not inline then -- [[Template:Legend]]
if not inline then -- [[Template:Legend]]
result = '<div class="legend" style="-webkit-column-break-inside:avoid;page-break-inside:avoid;break-inside:avoid-column"><span class="legend-color" style="display:inline-block;width:1.5em;height:1.5em;margin:1px 0;border:1px solid black;background-color:#' .. color .. '"> </span> ' .. line .. result .. '</div>'
result = '<div class="legend" style="page-break-inside:avoid;break-inside:avoid-column"><span class="legend-color" style="display:inline-block;min-width:1.25em;height:1.25em;line-height:1.25;margin:1px 0;border:1px solid black;background-color:#' .. color .. '"> </span> ' .. line .. result .. '</div>'
elseif inline == 'yes' then
elseif inline == 'yes' then
result = '<span style="background-color:#' .. color .. ';border:1px solid #000"> </span> ' .. line .. result
result = '<span style="background-color:#' .. color .. ';border:1px solid #000"> </span> ' .. line .. result
elseif inline == 'box' then
elseif inline == 'box' then
result = '<span style="background-color:#' .. color .. ';border:1px solid #000"> </span>' .. result
result = '<span style="background-color:#' .. color .. ';border:1px solid #000"> </span>' .. result
end
end
elseif inline == 'square' then
elseif inline == 'square' then
result = '<span style="color:#' .. color .. ';line-height:initial">■</span> ' .. line .. result
result = '<span style="color:#' .. color .. ';line-height:initial">■</span> ' .. line .. result
elseif inline == 'lsquare' then
elseif inline == 'lsquare' then
local link = args.link or mw.ustring.match(line, '%[%[([^%[:|%]]+)[|%]]')
local link = args.link or mw.ustring.match(line, '%[%[([^%[:|%]]+)[|%]]')
local link = args.link or mw.ustring.match(line, '%[%[([^%[:|%]]+)[|%]]')
local link = args.link or mw.ustring.match(line, '%[%[([^%[:|%]]+)[|%]]')
local border_color, text_color
local border_color, text_color
local color_box = data['color box format'] or data['rail box format'] or {}
if line_data then
if line_data then
if line_data['types'] and line_data['types'][typeN] then
if line_data['types'] and line_data['types'][typeN] then
border_color = Type_data['border color'] or line_data['border color'] or color
border_color = Type_data['border color'] or line_data['border color'] or color
text_color = Type_data['text color'] or line_data['text color']
text_color = Type_data['text color'] or line_data['text color']
lineN = Type_data['short name'] or line_data['short name'] or lineN
if color_box == 'title' and not args[4] then
lineN = Type_data['short name'] or line_data['short name'] or require('Module:Delink')._delink{line}
else
lineN = Type_data['short name'] or line_data['short name'] or lineN
end
else
else
border_color = line_data['border color'] or color
border_color = line_data['border color'] or color
text_color = line_data['text color']
text_color = line_data['text color']
lineN = line_data['short name'] or lineN
if color_box == 'title' and not args[4] then
lineN = line_data['short name'] or require('Module:Delink')._delink{line}
else
lineN = line_data['short name'] or lineN
end
end
end
else
else
border_color = color
border_color = color
end
end
text_color = text_color and '#' .. text_color or require('Module:Color contrast')._greatercontrast{color}
local bold = ';font-weight:bold'
local bold = (yesno(args.bold) == false) or ';font-weight:bold'
if (yesno(args.bold) == false) then bold = '' end
if inline == 'route' then -- [[Template:RouteBox]]
if inline == 'route' then -- [[Template:RouteBox]]
if link then
if link then
else
else
result = '<span style="border:.075em solid #' .. border_color .. ';border-radius:.5em;padding:0 .3em;color:#' .. color .. bold .. ';font-size:inherit;white-space:nowrap">' .. lineN .. '</span>'
result = '<span style="border:.075em solid #' .. border_color .. ';border-radius:.5em;padding:0 .3em;color:#' .. color .. bold .. ';font-size:inherit;white-space:nowrap">' .. lineN .. '</span>'
end
elseif inline == 'broute' then
if link then
result = '<span style="background-color:#' .. color .. ';border:.075em solid #000;padding:0 .3em">[[' .. link .. '|<span style="color:' .. text_color .. bold .. ';font-size:inherit;white-space:nowrap">' .. lineN .. '</span>]]</span>'
else
result = '<span style="background-color:#' .. color .. ';border:.075em solid #000;padding:0 .3em;color:' .. text_color .. bold .. ';font-size:inherit;white-space:nowrap">' .. lineN .. '</span>'
end
end
else -- [[Template:Legend]] (fallback; duplication to simplify logic)
else -- [[Template:Legend]] (fallback; duplication to simplify logic)
result = '<div class="legend" style="-webkit-column-break-inside:avoid;page-break-inside:avoid;break-inside:avoid-column"><span class="legend-color" style="display:inline-block;width:1.5em;height:1.5em;margin:1px 0;border:1px solid black;background-color:#' .. color .. '"> </span> ' .. line .. result .. '</div>'
result = '<div class="legend" style="page-break-inside:avoid;break-inside:avoid-column"><span class="legend-color" style="display:inline-block;min-width:1.25em;height:1.25em;line-height:1.25;margin:1px 0;border:1px solid black;background-color:#' .. color .. '"> </span> ' .. line .. result .. '</div>'
end
end
end
end
function p._icon(args, frame)
function p._icon(args, frame)
local system = args[1] or args.system
local system = args[1] or args.system
local data = args.data
local data = args.data
local icon, Format
if not system and not data then
return
end
data = data or getData(system)
local line, line_name = getLine(data, args[2] or args.line)
local icon
local icon_format
line = (getLine(data, line))
if line then
local line_type = args[3] or args.type
if line_type then
line_type = data.aliases and data.aliases[mw.ustring.lower(line_type)] or line_type
line_type = line.types and line.types[line_type] -- If there's no type table or entry for this type, then it can't have its own icon
icon_format = line_type['icon format'] or data['type icon format']
if line_type.icon then
if Type then
icon = line_type.icon
icon = line['icon']
end
end
end
end
if not (Format or icon) then
if not icon then
icon = line.icon
end
-- Only if there is no icon use the icon_format.
if not icon and not icon_format then
icon_format = line['icon format'] or data['line icon format']
end
end
if Format then
local default = data.lines._default or {}
if icon and string.find(icon, "%%1") and default and default.icon then
icon = mw.ustring.gsub(default.icon, '%%1', line_name)
end
end
end
if not icon then
icon = data['system icon']
end
if not icon_format then
icon_format = data['system icon format']
end
if icon_format then
if icon_format ~= 'image' then
icon = p._box({data = data, [2] = (args[2] or args.line), [3] = icon_format, type = (args[3] or args.type), bold = args.bold, link = args.link}, frame)
if args.name then
if line and line.title then
return icon .. " " .. line.title
end
icon = mw.ustring.gsub(icon, '(%[%[[^%]|]+)([%]|])', '%1' .. size .. '%2')
return icon .. " " .. data["system title"]
end
end
end
end
end
local size = args.size
if size then
if mw.ustring.match(size, '%d$') then
size = '|' .. size .. 'px'
else
size = '|' .. size
end
-- Upright values are to be disabled until there is use of upright scaling in subpages; doesn't seem to work anyway as of 2018-08-10
local regex = {
'|%s*%d*x?%d+px%s*([%]|])', -- '|%s*upright=%d+%.?%d*%s*([%]|])', '|%s*upright%s*([%]|])'
}
if mw.ustring.match(icon, regex[1]) then
icon = mw.ustring.gsub(icon, regex[1], size .. '%1')
-- elseif mw.ustring.match(icon, regex[2]) then
-- icon = gsub(icon, regex[2], size .. '%1')
-- elseif mw.ustring.match(icon, regex[3]) then
-- icon = gsub(icon, regex[3], size .. '%1')
else
icon = mw.ustring.gsub(icon, '(%[%[[^%]|]+)([%]|])', '%1' .. size .. '%2')
end
end
local link = args.link
if link then
if mw.ustring.match(icon, '|%s*link=[^%]|]*[%]|]') then
icon = mw.ustring.gsub(icon, '|%s*link=[^%]|]*([%]|])', '|link=' .. link .. '%1')
else
icon = mw.ustring.gsub(icon, '(%[%[[^%]|]+)([%]|])', '%1|link=' .. link .. '%2')
end
end
end
local alt = args.alt or link
if alt then
if mw.ustring.match(icon, '|%s*alt=[^%]|]*[%]|]') then
icon = mw.ustring.gsub(icon, '|%s*alt=[^%]|]*([%]|])', '|alt=' .. alt .. '%1')
else
icon = mw.ustring.gsub(icon, '(%[%[[^%]|]+)([%]|])', '%1|alt=' .. alt .. '%2')
end
end
end
return icon
if args.name then
if line and line.title then
return icon .. " " .. line.title
end
return icon .. " " .. data["system title"]
end
end
return icon
end
end
end
end
if Type then
if Type and Type ~= '' then
if line == '' then
if line == '' then
line = Type
line = Type
p.station = makeInvokeFunction('_station')
p.station = makeInvokeFunction('_station')
function p._terminusTable(args, frame)
local system = args[1] or args.system
lineN = args[2] or args.line
local side = mw.ustring.sub(mw.ustring.lower(args[3] or args.side or ''), 1, 1)
typeN = args.type
local prefix = (side == 'r') and 'right' or 'left'
local data = args.data
if system or data then
data = data or getData(system, true)
end
if data then
local line = getLine(data, lineN) or error(i18n[lang]['error_unknown'](lineN))
if typeN and data and data['aliases'] then typeN = data['aliases'][mw.ustring.lower(typeN)] or typeN end
local Type = line['types'] and line['types'][typeN]
local circular
if Type then
if Type['circular'] then
-- Type may override the circular status of the line
circular = Type['circular']
end
else
circular = line['circular']
end
return Type and Type[prefix .. ' terminus'] or line[prefix .. ' terminus'], data['station format'] or i18n[lang]['error_format'], circular
else
local terminus = frame:expandTemplate{ title = 'S-line/' .. system .. ' ' .. prefix .. '/' .. lineN }
return mw.ustring.gsub(terminus, '{{{type}}}', typeN)
end
end
function p._terminus(args, frame)
local var, Format, circular = p._terminusTable(args, frame)
return circular and var or getTerminusText(var, Format)
end
p.terminus = makeInvokeFunction('_terminus')
function p._style(args, frame)
function p._style(args, frame)
local code = mw.text.split(mw.ustring.gsub(args[1], '^%s*{{(.*)}}%s*$', '%1'), '%s*}}%s*{{%s*')
local code = mw.text.split(mw.ustring.gsub(args[1], '^%s*{{(.*)}}%s*$', '%1'), '%s*}}%s*{{%s*')
local system
local system
local group = 0
local group = tonumber(args.offset or 0) or 0
local firstgroup = group + 1
local delete = {
local delete = {
['s-rail'] = true,
['s-rail'] = true,
local remove_rows = {}
local remove_rows = {}
local data = {}
local data = {}
local noclear = false
for i, v in ipairs(code) do
for i, v in ipairs(code) do
code[i] = mw.ustring.gsub(code[i], '\n', ' ')
code[i] = mw.ustring.gsub(code[i], '\n', ' ')
local template = mw.ustring.lower(mw.text.trim(mw.ustring.match(code[i], '^[^|]+')))
local template = mw.ustring.lower(mw.text.trim(mw.ustring.match(code[i], '^[^|]+')))
code[i] = mw.ustring.match(code[i], '(|.+)$')
code[i] = mw.ustring.match(code[i], '(|.+)$')
if (mw.ustring.match(code[i] or '', 'noclear%s*=%s*[a-z]')) then
noclear = true
end
if template == 's-line' then
if template == 's-line' then
data[i] = {}
data[i] = {}
end
end
code = table.concat(code, '\n')
code = table.concat(code, '\n')
local t = {'{{Adjacent stations', '\n}}'}
local t = {'{{Adjacent stations' .. (noclear and '|noclear=y\n' or ''), '\n}}'}
system = mw.ustring.match(code, '|system(%d*)=')
system = mw.ustring.match(code, '|system(%d*)=')
code = mw.ustring.gsub(code, '\n\n+', '\n')
code = mw.ustring.gsub(code, '\n\n+', '\n')
if tonumber(system) > 1 then
if tonumber(system) > firstgroup then
-- If s-line isn't the first template then the system will have to be moved to the top
-- If s-line isn't the first template then the system will have to be moved to the top
system = mw.ustring.match(code, '|system%d*=([^|]*[^|\n])')
system = mw.ustring.match(code, '|system%d*=([^|]*[^|\n])')
code = mw.ustring.gsub(code, '|system%d*=[^|]*', '')
code = mw.ustring.gsub(code, '|system%d*=[^|]*', '')
code = '\n|system1='..system..code
code = '\n|system'..firstgroup..'='..system..code
elseif not mw.ustring.match(code, '^[^{%[]*|[^=|]+2=') then
elseif not mw.ustring.match(code, '^[^{%[]*|[^=|]+2=') then
-- If there's only one parameter group then there's no need to have line breaks
-- If there's only one parameter group then there's no need to have line breaks