Module:Adjacent stations/sandbox: Difference between revisions

Content deleted Content added
No edit summary
sync with live template
 
(28 intermediate revisions by 7 users not shown)
Line 72:
table.insert(formatKeys, k)
end
if not key then return name .. ' was not specified nor a default was found. Choices were: ' .. table.concat(formatKeys, ', ') end
return name .. ' was "' .. key .. '" but neither an entry for it nor a default was found. Choices were: ' .. table.concat(formatKeys, ', ')
end
Line 132 ⟶ 131:
 
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 = {
Line 145 ⟶ 147:
 
for k, v in pairs(_args) do -- Maps each raw argument to processed arguments by string matching
local a_args[k] = mw.ustring.v:match(k, '^%s*(.*%D-)%d+s*$') or k -- The parameter; address 1 can be omitted
if _args[k] and _args[k] ~= '' then
local b = tonumber(mw.ustring.match(k, '^.*%D(%d+)$')) or 1 -- The address for a given argument; address 1 can be omitted
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 = require('Module:Yesno')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
Line 172 ⟶ 177:
['header midcell'] = 'colspan="3" class="hmA"|',
['body cell'] = 'class="bcA"|',
['body banner'] = 'class="bbA notheme" style="color:inherit;background-color:#',
}
 
Line 232 ⟶ 237:
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)
 
Line 258 ⟶ 263:
color = Type['color']
color_2 = Type['color2'] or color
elseif Type['background color'] then
background_color = Type['background color']
color = line['color'] or default['color'] or ''
color_2 = line['color2'] or color
else
background_color = Type['background color'] or line['background color']
color = line['color'] or default['color'] or ''
color_2 = line['color2'] or color
Line 282 ⟶ 283:
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 trimq = require('Module:Trim quotes')._trim
local _through = args[v]['through-' .. b] or args[v]['through']
local _through_data = getLine(data[v], _through)
Line 390:
return function (frame)
local args = getArgs(frame, {parentOnly = true})
return p[funcName](args, frame)
end
end
 
local function makeTemplateFunction(funcName)
-- makes a function that can be returned from #invoke, using
-- [[Module:Arguments]]
return function (frame)
local args = getArgs(frame, {frameOnly = true})
return p[funcName](args, frame)
end
Line 411 ⟶ 420:
lineN = args[2] or args.line
if not (system or lineN) then return '' end
local line, Type, line_data, out_text
local inline = args[3] or args.inline
typeN = args.type
local station = args.station
local data = args.data
if system or data then
Line 420 ⟶ 428:
local color
if data then
local default = data['lines']['_default'] or {}
line, lineN = getLine(data, lineN)
if typeN then
Line 427 ⟶ 436:
color = getColor(data, nil, lineN, typeN)
if inline ~= 'box' then
line_data = line or error(i18n[lang]['error_unknown'](lineN))
if line then
line = line_data['title'] or default['title'] or error(i18n[lang]['error_missing']('title'))
if station and station ~= true and not mw.ustring.match(inline or '', 'route') then
line = mw.ustring.gsub(line, '%%1', lineN)
local _Format = data['station format'][station] or data['station format'][1]
out_text = _Format and getStation(station, _Format) or station
else
line_data = next(line) and line or error(i18n[lang]['error_unknown'](lineN))
out_text = line_data['title'] or error(i18n[lang]['error_missing']('title'))
end
else
out_text = data['system title']
end
end
else
color = getColor(nil, system, lineN, typeN, frame)
if inline ~= 'box' then
out_textline = frame:expandTemplate{ title = system .. ' lines', args = {lineN, ['branch'] = typeN} }
if mw.text.trim(out_textline) == '' then return error(i18n[lang]['error_unknown'](lineN)) end
end
Type = typeN
Line 450 ⟶ 451:
local result
 
if Type and Type ~= '' and inline ~= 'box' and not station then
if out_textline == '' then
out_textline = Type
else
result = ' – ' .. Type
Line 461 ⟶ 462:
 
if not inline then -- [[Template:Legend]]
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;color:inherit;background-color:#' .. color .. '"> </span> ' .. out_textline .. result .. '</div>'
elseif inline == 'yes' then
result = '<span style="color:inherit;background-color:#' .. color .. ';border:1px solid #000">    </span>&nbsp;' .. out_textline .. result
elseif inline == 'box' then
result = '<span style="color:inherit;background-color:#' .. color .. ';border:1px solid #000">    </span>' .. result
elseif inline == 'link' then
local link = args.link or mw.ustring.match(out_textline, '%[%[([^%[:|%]]+)[|%]]')
if link then
result = '[[' .. link .. '|<span style="color:inherit;background-color:#' .. color .. ';border:1px solid #000">    </span>]]' .. result
else
result = '<span style="color:inherit;background-color:#' .. color .. ';border:1px solid #000">    </span>' .. result
end
elseif inline == 'square' then
result = '<span style="color:#' .. color .. ';line-height:initial">■</span>&nbsp;' .. out_textline .. result
elseif inline == 'lsquare' then
local link = args.link or mw.ustring.match(out_textline, '%[%[([^%[:|%]]+)[|%]]')
if link then
result = '[[' .. link .. '|<span style="color:#' .. color .. ';line-height:initial">■</span>]]'
Line 483 ⟶ 484:
end
elseif inline == 'dot' then
result = '<span style="color:#' .. color .. ';line-height:initial">●</span> ' .. out_textline .. result
elseif inline == 'ldot' then
local link = args.link or mw.ustring.match(out_textline, '%[%[([^%[:|%]]+)[|%]]')
if link then
result = '[[' .. link .. '|<span style="color:#' .. color .. ';line-height:initial">●</span>]]'
Line 492 ⟶ 493:
end
elseif inline == 'small' then
result = '<span style="color:inherit;background-color:#' .. color .. '"> </span>' .. ' ' .. out_textline .. result
else
local yesno = require("Module:Yesno")
local link = args.link or mw.ustring.match(out_textline, '%[%[([^%[:|%]]+)[|%]]')
local border_color, text_color
local color_box = data['color box format'] or data['rail box format'] or {}
if line_data then
local color_box = data['color box format'] or data['rail box format'] or {}
if line_data['types'] and line_data['types'][typeN] then
local Type_data = line_data['types'][typeN]
Line 504 ⟶ 505:
text_color = Type_data['text color'] or line_data['text color']
if color_box == 'title' and not args[4] then
lineN = Type_data['short name'] or line_data['short name'] or require('Module:Delink')._delink{out_textline}
else
lineN = Type_data['short name'] or line_data['short name'] or lineN
Line 512 ⟶ 513:
text_color = line_data['text color']
if color_box == 'title' and not args[4] then
lineN = line_data['short name'] or require('Module:Delink')._delink{out_textline}
else
lineN = line_data['short name'] or lineN
Line 518 ⟶ 519:
end
else
border_color = color
local default = data['lines']['_default'] or {}
border_color = default['border color'] or color
text_color = default['text color']
lineN = require('Module:Delink')._delink{out_text}
end
text_color = text_color and '#' .. text_color or require('Module:Color contrast')._greatercontrast{color}
local bold = (yesno(args.bold) == false) and '' or ';font-weight:bold'
if (yesno(args.bold) == false) then bold = '' end
 
if inline == 'route' then -- [[Template:RouteBox]]
if link then
result = '<span style="color:inherit;background-color:#' .. color .. ';border:.075em solid #' .. border_color .. ';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 #' .. border_color .. ';padding:0 .3em;color:' .. text_color .. bold .. ';font-size:inherit;white-space:nowrap">' .. lineN .. '</span>'
Line 534 ⟶ 532:
elseif inline == 'croute' then -- [[Template:Bahnlinie]]
if link then
result = '<span style="color:inherit;background-color:#' .. color .. ';border:.075em solid #' .. border_color .. ';border-radius:.5em;padding:0 .3em">[[' .. link .. '|<span style="color:' .. text_color .. bold .. ';font-size:inherit;white-space:nowrap">' .. lineN .. '</span>]]</span>'
else
result = '<span style="color:inherit;background-color:#' .. color .. ';border:.075em solid #' .. border_color .. ';border-radius:.5em;padding:0 .3em;color:' .. text_color .. bold .. ';font-size:inherit;white-space:nowrap">' .. lineN .. '</span>'
end
elseif inline == 'xroute' then -- [[Template:Bahnlinie]]
Line 546 ⟶ 544:
elseif inline == 'broute' then
if link then
result = '<span style="color:inherit;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
else -- [[Template:Legend]] (fallback; duplication to simplify logic)
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;color:inherit;background-color:#' .. color .. '"> </span> ' .. out_textline .. result .. '</div>'
end
end
Line 566 ⟶ 564:
local system = args[1] or args.system
local data = args.data
 
if not system and not data then return end
if not system and not data then
return
end
 
data = data or getData(system)
lineN = args[2] or args.line
typeN = args[3] or args.type
 
local iconline, icon_formatline_name = getLine(data, args[2] or args.line)
 
line, lineN = getLine(data, lineN)
local icon
local icon_format
 
if line then
local line_type = args[3] or args.type
if typeN then
if line_type then
typeN = data['aliases'] and data['aliases'][mw.ustring.lower(typeN)] or typeN
line_type = data.aliases and data.aliases[mw.ustring.lower(line_type)] or line_type
local Type = line['types'] and line['types'][typeN] -- If there's no type table or entry for this type, then it can't have its own icon
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 = Type['icon format'] or data['type icon format']
iconicon_format = Typeline_type['icon format'] or data['type icon format']
 
if line_type.icon then
icon = line_type.icon
end
end
 
if not icon then
icon = line.icon
end
 
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
 
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
 
if icon and string.find(icon, "%%1") then icon = mw.ustring.gsub(icon, '%%1', lineN) end
end
 
if not icon then
if not icon_format then icon_format = data['system icon format'] end
icon = data['system icon']
end
 
if not icon_format then
local name = require('Module:Yesno')(args.name)
icon_format = data['system icon format']
local link = args.link
end
 
if icon_format then
if icon_format ~= 'image' then
icon = p._box({data = data, [2] = lineN(args[2] or args.line), [3] = icon_format, type = typeN(args[3] or args.type), bold = args.bold, link = args.link, station = name}, frame)
 
if name then
if args.name then
local filter = {yes = true, small = true, dot = true, square = true}
localif stationline =and argsline.stationtitle then
return icon .. " " .. line.title
if station then
local _Format = data['station format'][station] or data['station format'][1]
station = _Format and getStation(station, _Format) or station
return filter[icon_format] and (icon ' – ' .. station) or (icon .. " " .. station)
end
return filter[icon_format] and icon or icon .. '" '" .. (line and line['title'] or data['"system title'"] or '')
end
return icon
end
end
 
if not icon then icon = (data['system icon'] ~= '') and data['system icon'] or error(i18n[lang]['error_missing']('icon')) end
 
local size = args.size
Line 635 ⟶ 647:
end
 
local link = args.link
if link then
if mw.ustring.match(icon, '|%s*link=[^%]|]*[%]|]') then
Line 652 ⟶ 665:
end
 
if args.name then
if line and line.title then
local station = args.station
return icon .. " " .. line.title
if station then
local _Format = data['station format'][station] or data['station format'][1]
return icon .. " " .. (_Format and getStation(station, _Format) or station)
end
return icon .. " " .. (line and line['title'] or data["system title"] or '')
end
return icon
Line 664 ⟶ 675:
 
p.icon = makeInvokeFunction('_icon')
p['rail icon'] = makeTemplateFunction('_icon')
 
function p._line(args, frame)
Line 674 ⟶ 686:
data = data or getData(system, true)
if data then
line = (getLine(data, line)) or error(i18n[lang]['error_unknown'](line))
if not next(line) then error(i18n[lang]['error_unknown'](args[2] or args.line)) end
if Type then
Type = data['aliases'] and data['aliases'][mw.ustring.lower(Type)] or Type
Line 698 ⟶ 709:
 
p.line = makeInvokeFunction('_line')
 
function p._shortline(args, frame)
local system = args[1] or args.system
lineN = args[2] or args.line
if not (system or lineN) then return '' end
local line, Type, line_data
typeN = args.type
local data = args.data
if system or data then
data = data or getData(system, true)
if data then
local default = data['lines']['_default'] or {}
line, lineN = getLine(data, lineN)
if typeN then
typeN = data['aliases'] and data['aliases'][mw.ustring.lower(typeN)] or typeN
Type = line['types'] and line['types'][typeN] and line['types'][typeN]['title'] or typeN
end
line_data = line or error(i18n[lang]['error_unknown'](lineN))
line = line_data['title'] or default['title'] or error(i18n[lang]['error_missing']('title'))
line = mw.ustring.gsub(line, '%%1', lineN)
else
line = frame:expandTemplate{ title = system .. ' lines', args = {lineN, ['branch'] = typeN} }
if mw.text.trim(line) == '' then return error(i18n[lang]['error_unknown'](lineN)) end
Type = typeN
end
 
local result
 
if Type and Type ~= '' then
if line == '' then
line = Type
else
result = ' – ' .. Type
end
end
if args.note then result = (result or '') .. ' ' .. args.note end
result = result or ''
 
local link = args.link or mw.ustring.match(line, '%[%[([^%[:|%]]+)[|%]]')
if line_data then
if line_data['types'] and line_data['types'][typeN] then
local Type_data = line_data['types'][typeN]
lineN = Type_data['short name'] or line_data['short name'] or lineN
else
lineN = line_data['short name'] or lineN
end
end
if link then
result = '[[' .. link .. '|' .. lineN .. ']]'
else
result = lineN
end
result = mw.ustring.gsub(result, ':%s*#transparent', ':transparent')
 
return result
end
end
 
p.shortline = makeInvokeFunction('_shortline')
 
function p._station(args, frame)
Line 732 ⟶ 804:
 
p.station = makeInvokeFunction('_station')
p['station link'] = makeTemplateFunction('_station')
 
function p._terminus_terminusTable(args, frame)
local system = args[1] or args.system
lineN = args[2] or args.line
if not lineN then return '' end
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
local terminus
 
if system or data then
Line 747 ⟶ 818:
end
if data then
local line = getLine(data, lineN) or error(i18n[lang]['error_unknown'](lineN))
local line, Type
if typeN and data and data['aliases'] then typeN = data['aliases'][mw.ustring.lower(typeN)] or typeN end
line, lineN = getLine(data, lineN)
local Type = line['types'] and line['types'][typeN]
if not next(line) then error(i18n[lang]['error_unknown'](lineN)) end
if typeN then
typeN = data['aliases'] and data['aliases'][mw.ustring.lower(typeN)] or typeN
Type = line['types'] and line['types'][typeN]
end
 
local circular
if Type then
if Type['circular'] then
terminus = Type[prefix .. ' terminus'] or line[prefix .. ' terminus']
-- Type may override the circular status of the line
if circular = Type['circular'] then return terminus end
end
else
terminuscircular = line[prefix .. ' terminuscircular']
if line['circular'] then return terminus end
end
 
return getTerminusText(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, args = {['type'] = typeN} }
return frame:expandTemplate{ title = system mw.ustring. ' stations', args = {['station'] = gsub(terminus, ['line{{{type}}}'] = lineN, ['branch'] = 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