模块:LineStationBuilder
外观
此模块的文档可以在模块:LineStationBuilder/doc创建
-- Module:LineStationBuilder
-- 动态生成带链接的线路站点列表,自动过滤无效条目(如 "-")
local p = {}
function p.main(frame)
local args = frame.args
local lineCode = args[1]
if not lineCode or lineCode == '' then
return ''
end
-- 1. 获取原始站点列表字符串(复用现有 TextLineList 模板)
local raw = frame:expandTemplate{ title = 'TextLineList', args = { lineCode } }
if not raw or raw == '' then
return ''
end
-- 2. 按空白字符拆分,并过滤掉无效的站点名
local stations = {}
for token in string.gmatch(raw, "%S+") do
-- 去除首尾空白(虽然 %S+ 不会匹配空白,但安全起见)
local name = token:match("^%s*(.-)%s*$")
-- 过滤:空字符串、单个横线、其他无意义符号(可根据需要扩展)
if name ~= "" and name ~= "-" and name ~= "—" then
table.insert(stations, name)
end
end
if #stations == 0 then
return ''
end
-- 3. 将每个站点名转换为链接 [[站点名]]
-- 如果 TextLineList 已经返回 [[站A]] 形式,请去掉下面这一行的括号包装
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