else
result = '<span>' .. lineN .. '</span>'
end
else -- Template:Legend (fallback; duplication to simplify logic)
result = '<div><span></span>' .. line .. result .. '</div>'
end
end  
result = mw.ustring.gsub(result, ':%s*#transparent', ':transparent')  
return result
end
end  
p.box = makeInvokeFunction('_box')  
function p._icon(args, )
local system = args[1] or args.system
local data = args.data  
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  
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
icon = line_type.icon
end
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  
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}, )  
if args.name then
if line and line.title then
return icon .. " " .. line.title
end
return icon .. " " .. data["system title"]
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  
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  
if args.name then
if line and line.title then
return icon .. " " .. line.title
end
return icon .. " " .. data["system title"]
end
return icon
end  
p.icon = makeInvokeFunction('_icon')
p['rail icon'] = makeTemplateFunction('_icon')  
function p._line(args, )
local system = args[1] or args.system
local line = args[2] or args.line
if not line then return '' end
local Type = args[3] or args.type
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 Type then
Type = data['aliases'] and data['aliases'][mw.ustring.lower(Type)] or Type
Type = line['types'] and line['types'][Type] and line['types'][Type]['title'] or Type
end
line = line['title'] or error(i18n[lang]['error_missing']('title'))
else
line = :expandTemplate{ title = system .. ' lines', args = {line, ['branch'] = Type} }
if mw.text.trim(line) == '' then return error(i18n[lang]['error_unknown'](lineN)) end
end  
if Type and Type ~= '' then
if line == '' then
line = Type
else
line = line .. '– ' .. Type
end
end
return line
end
end  
p.line = makeInvokeFunction('_line')  
function p._shortline(args, )
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 = :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 = * .. 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, )
local system = args[1] or args.system
local station = args[2] or args.station
if not station then return '' end
lineN = args[3] or args.line
typeN = args[4] or args.type
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 lineN then
lineN = data['aliases'][mw.ustring.lower(lineN)] or lineN
end
if typeN then
typeN = data['aliases'][mw.ustring.lower(typeN)] or typeN
end
end
station = getStation(station, _Format)
else
station = station or ''
end
else
station = :expandTemplate{ title = system .. ' stations', args = {['station'] = station, ['line'] = lineN, ['branch'] = typeN} }
end  
return station
end
end  
p.station = makeInvokeFunction('_station')
p['station link'] = makeTemplateFunction('_station')  
function p._terminusTable(args, )
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 * or *
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 = :expandTemplate{ title = 'S-line/' .. system .. ' ' .. prefix .. '/' .. lineN }
return mw.ustring.gsub(terminus, '}', typeN)
end
end  
function p._terminus(args, )
local var, Format, circular = p._terminusTable(args, )