模块:LineStationBuilder
外观
此模块的文档可以在模块:LineStationBuilder/doc创建
-- Module:LineStationBuilder
-- 作用:根据线路代码,动态生成完整的站点列表(带分隔符 •)
local p = {}
function p.main(frame)
local args = frame.args
local lineCode = args[1] -- 获取 {{{1}}}
if not lineCode or lineCode == '' then
return ''
end
-- 1. 获取该线路的原始站点列表字符串(复用现有的 TextLineList 模板)
-- 注意:这里只调用一次 TextLineList,而不是 44 次
local rawStationList = frame:expandTemplate{ title = 'TextLineList', args = { lineCode } }
if not rawStationList or rawStationList == '' then
return ''
end
-- 2. 将原始字符串解析为 Lua 表(按空格分割,假设站点之间用空格隔开)
-- 示例:如果 rawStationList = "站A 站B 站C 站D"
-- 则 stations = {"站A", "站B", "站C", "站D"}
local stations = {}
for station in string.gmatch(rawStationList, "%S+") do
table.insert(stations, station)
end
-- 3. 根据站点数量动态生成最终字符串
-- 格式要求:前两个站点之间用 • 连接,后续每个站点前也加 •
-- 结果示例:站A • 站B • 站C • 站D
if #stations == 0 then
return ''
elseif #stations == 1 then
return stations[1]
else
-- 先处理第一个站点(不加前导 •)
local result = stations[1]
-- 从第二个站点开始,每个前面加 " • "
for i = 2, #stations do
result = result .. ' • ' .. stations[i]
end
return result
end
end
return p