跳转到内容

模块:LineStationBuilder:修订间差异

来自TPC
HYH-HS留言 | 贡献
创建页面,内容为“-- 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,而…”
 
HYH-HS留言 | 贡献
无编辑摘要
 
(未显示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]         -- 获取 {{{1}}}
     local lineCode = args[1]
     if not lineCode or lineCode == '' then
     if not lineCode or lineCode == '' then
         return ''
         return ''
     end
     end


     -- 1. 获取该线路的原始站点列表字符串(复用现有的 TextLineList 模板)
     -- 1. 获取原始站点列表字符串(复用现有 TextLineList 模板)
    --    注意:这里只调用一次 TextLineList,而不是 44 次
     local raw = frame:expandTemplate{ title = 'TextLineList', args = { lineCode } }
     local rawStationList = frame:expandTemplate{ title = 'TextLineList', args = { lineCode } }
     if not raw or raw == '' then
     if not rawStationList or rawStationList == '' then
         return ''
         return ''
     end
     end


     -- 2. 将原始字符串解析为 Lua 表(按空格分割,假设站点之间用空格隔开)
     -- 2. 按空白字符拆分,并过滤掉无效的站点名
    --    示例:如果 rawStationList = "站A 站B 站C 站D"
    --    则 stations = {"站A", "站B", "站C", "站D"}
     local stations = {}
     local stations = {}
     for station in string.gmatch(rawStationList, "%S+") do
     for token in string.gmatch(raw, "%S+") do
         table.insert(stations, station)
         -- 去除首尾空白(虽然 %S+ 不会匹配空白,但安全起见)
        local name = token:match("^%s*(.-)%s*$")
        -- 过滤:空字符串、单个横线、其他无意义符号(可根据需要扩展)
        if name ~= "" and name ~= "-" and name ~= "—" then
            table.insert(stations, name)
        end
     end
     end


    -- 3. 根据站点数量动态生成最终字符串
    --    格式要求:前两个站点之间用 • 连接,后续每个站点前也加 •
    --    结果示例:站A • 站B • 站C • 站D
     if #stations == 0 then
     if #stations == 0 then
         return ''
         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
    -- 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