模块:LineStationBuilder:修订间差异
外观
创建页面,内容为“-- 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,而…” |
无编辑摘要 |
||
| 第1行: | 第1行: | ||
-- Module:LineStationBuilder | -- Module:LineStationBuilder | ||
-- | -- 生成带链接的线路站点列表,格式:站A • 站B • 站C (无末尾多余符号) | ||
local p = {} | local p = {} | ||
function p.main(frame) | function p.main(frame) | ||
local args = frame.args | local args = frame.args | ||
local lineCode = args[1] | local lineCode = args[1] | ||
if not lineCode or lineCode == '' then | if not lineCode or lineCode == '' then | ||
return '' | return '' | ||
end | end | ||
-- 1. | -- 1. 获取原始站点列表字符串(如 "站A 站B 站C") | ||
local rawStationList = frame:expandTemplate{ title = 'TextLineList', args = { lineCode } } | local rawStationList = frame:expandTemplate{ title = 'TextLineList', args = { lineCode } } | ||
if not rawStationList or rawStationList == '' then | if not rawStationList or rawStationList == '' then | ||
| 第17行: | 第16行: | ||
end | end | ||
-- 2. | -- 2. 按空格拆分为表 | ||
local stations = {} | local stations = {} | ||
for station in string.gmatch(rawStationList, "%S+") do | for station in string.gmatch(rawStationList, "%S+") do | ||
| 第25行: | 第22行: | ||
end | end | ||
if #stations == 0 then | if #stations == 0 then | ||
return '' | return '' | ||
end | end | ||
-- 3. 将每个站点名转换为链接 [[站点名]] | |||
-- 注意:如果原始站点名已经包含链接(例如 [[站A]]),则不要再重复加。 | |||
-- 假设 TextLineList 返回的是纯站点名,所以这里加 [[ ]] | |||
for i, name in ipairs(stations) do | |||
stations[i] = '[[' .. name .. ']]' | |||
end | |||
-- 4. 用 " • " 拼接,最后一个后面不加分隔符 | |||
local result = stations[1] | |||
for i = 2, #stations do | |||
result = result .. ' • ' .. stations[i] | |||
end | |||
return result | |||
end | end | ||
return p | return p | ||
2026年6月8日 (一) 14:25的版本
此模块的文档可以在模块: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 .. ']]'
end
-- 4. 用 " • " 拼接,最后一个后面不加分隔符
local result = stations[1]
for i = 2, #stations do
result = result .. ' • ' .. stations[i]
end
return result
end
return p