跳转到内容

模块: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留言 | 贡献
无编辑摘要
第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]         -- 获取 {{{1}}}
     local lineCode = args[1]
     if not lineCode or lineCode == '' then
     if not lineCode or lineCode == '' then
         return ''
         return ''
     end
     end


     -- 1. 获取该线路的原始站点列表字符串(复用现有的 TextLineList 模板)
     -- 1. 获取原始站点列表字符串(如 "站A 站B 站C")
    --    注意:这里只调用一次 TextLineList,而不是 44 次
     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. 将原始字符串解析为 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 station in string.gmatch(rawStationList, "%S+") do
第25行: 第22行:
     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. 将每个站点名转换为链接 [[站点名]]
    --    注意:如果原始站点名已经包含链接(例如 [[站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