模块:LineStationBuilder
外观
此模块的文档可以在模块:LineStationBuilder/doc创建
-- Module:LineStationBuilder
-- 生成带链接的线路站点列表,格式:站A • 站B • 站C (无末尾多余符号)
local p = {}
function p.main(frame)
local args = frame.args
local lineCode = args[1]
if not lineCode or lineCode == '' then
return ''
end
-- 1. 获取原始站点列表字符串(如 "站A 站B 站C")
local rawStationList = frame:expandTemplate{ title = 'TextLineList', args = { lineCode } }
if not rawStationList or rawStationList == '' then
return ''
end
-- 2. 按空格拆分为表
local stations = {}
for station in string.gmatch(rawStationList, "%S+") do
table.insert(stations, station)
end
if #stations == 0 then
return ''
end
-- 3. 将每个站点名转换为链接 [[站点名]]
-- 注意:如果原始站点名已经包含链接(例如 [[站A]]),则不要再重复加。
-- 假设 TextLineList 返回的是纯站点名,所以这里加 [[ ]]
for i, name in ipairs(stations) do
stations[i] = '[['..name..'||轨道交通/车站/' .. name .. ']]'
end
-- 4. 用 " • " 拼接,最后一个后面不加分隔符
local result = stations[1]
for i = 2, #stations do
result = result .. ' • ' .. stations[i]
end
return result
end
return p