模块: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,而…” |
无编辑摘要 |
||
| (未显示2个用户的7个中间版本) | |||
| 第1行: | 第1行: | ||
-- This module is written by HYH_HS with Deepseek | |||
-- Module:LineStationBuilder | -- Module:LineStationBuilder | ||
-- | -- 动态生成带链接的线路站点列表,自动过滤无效条目(如 "-") | ||
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. 获取原始站点列表字符串(复用现有 TextLineList 模板) | ||
local raw = frame:expandTemplate{ title = 'TextLineList', args = { lineCode } } | |||
local | if not raw or raw == '' then | ||
if not | |||
return '' | return '' | ||
end | end | ||
-- 2. | -- 2. 按空白字符拆分,并过滤掉无效的站点名 | ||
local stations = {} | local stations = {} | ||
for | for token in string.gmatch(raw, "%S+") do | ||
table.insert(stations, | -- 去除首尾空白(虽然 %S+ 不会匹配空白,但安全起见) | ||
local name = token:match("^%s*(.-)%s*$") | |||
-- 过滤:空字符串、单个横线、其他无意义符号(可根据需要扩展) | |||
if name ~= "" and name ~= "-" and name ~= "—" then | |||
table.insert(stations, name) | |||
end | |||
end | end | ||
if #stations == 0 then | if #stations == 0 then | ||
return '' | return '' | ||
end | 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 | end | ||
return p | return p | ||
2026年6月13日 (六) 11:47的最新版本
此模块的文档可以在模块:LineStationBuilder/doc创建
-- This module is written by HYH_HS with Deepseek
-- 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