模組:线路时刻表
外观

local p = {}
local getArgs = require('Module:Arguments').getArgs
local function makeInvokeFunction(funcName)
return function(frame)
local args = getArgs(frame, {parentOnly = true})
return p[funcName](args, frame)
end
end
local data
local as_data
-- 数据导入
local function getData(system)
local title = mw.title.new('Module:线路时刻表/' .. system)
if not (title and title.exists) then return nil end
return require('Module:线路时刻表/' .. system)
end
local function getAsData(system)
local title = mw.title.new('Module:Adjacent stations/' .. system)
if not (title and title.exists) then return nil end
return require('Module:Adjacent stations/' .. system)
end
-- Adjacent stations 数据接口
local function getTermini(line, type)
if type and type ~= '' then
return {
as_data.lines[line].types[type]['right terminus'] or as_data.lines[line]['right terminus'],
as_data.lines[line].types[type]['left terminus'] or as_data.lines[line]['left terminus']
}
else
return {
as_data.lines[line]['right terminus'],
as_data.lines[line]['left terminus']
}
end
end
-- 文本处理函数
local function makeTerminus(frame, name, system, line, type)
local circular
if type and type ~= '' then
circular = as_data.lines[line].types[type]['circular'] or as_data.lines[line]['circular']
else
circular = as_data.lines[line]['circular']
end
if circular then
return name
else
return require('Module:Adjacent stations')._station({system, name, line, type}, frame)
end
end
-- 车站时刻
function p._station(args, frame)
local system = args[1]
local station = args[2]
local line = args[3]
local type = args[4]
local fnl = args[fnl]
data = getData(system)
if not data then error('不存在模块:线路时刻表/'..system) end
as_data = getAdData(system)
if not as_data then error('不存在模块:Adjacent stations/'..system) end
if fnl ~= 'no' then
local body = {}
for r, route_data in ipairs(data.first_n_last) do
local _data = route_data[station]
if _data then
local row = {}
local color = require('Module:Adjacent stations')._color({system, line, type}, frame)
local termini = getTermini(route_data.line, route_data.type)
if route_data.subtermini then
if route_data.subtermini.scope == 'last' then
-- 待实现
else
error('不支持的子终点站作用域') -- 按需求扩展
end
else
table.insert(row, '<tr><td class="bar" rowspan=' .. #termini .. ' style="background-color: #' .. color .. '"></td>')
for t, terminus in ipairs(termini) do
if t ~= 1 then table.insert(row, '<tr>') end
table.insert(row, '<th class="terminus" colspan=2>' .. makeTerminus(frame, terminus, system, line, type) .. '</th>')
for v, variant in ipairs(data.variants) do
table.insert(row, '<td>' .. _data[v][1][t] .. '</td><td>' .. _data[v][2][t] .. '</td>')
end
table.insert(row, '</tr>')
end
end
table.insert(body, table.concat(row))
end
end
if body then
if #data.variants > 1 then
local header = { { }, { } }
for v, variant in ipairs(data.variants) do
table.insert(h[1], '<th colspan=2>' .. variant .. '</th>')
table.insert(h[2], '<th>首班</th><th>末班</th>')
end
return '<table><tr><th colspan=3 rowspan=2>' .. data.ref .. '</th>' .. header[1] .. '</tr><tr>' .. header[2] .. '</tr>' .. table.concat(body) .. '</table>'
else
return '<table class="wikitable station-fnl"><tr><th colspan=3>' .. data.ref .. '</th><th>首班</th><th>末班</th></tr>' .. table.concat(body) .. '</table>'
end
else
return nil
end
end
end
p.station = makeInvokeFunction('_station')
return p