切換選單
切換偏好設定選單
切換個人選單
尚未登入
若您做出任何編輯,會公開您的 IP 位址。

模組:LinkJoin

出自卡特霍普

可在模組:LinkJoin/doc建立此模組的說明文件

local p = {}

function p.main(frame)
    local args = frame:getParent().args or {}
    return p._joinLinks(args)
end

function p._joinLinks(args)
    local links = {}
    local labels = {}
    local maxIndex = 0
    
    for key, value in pairs(args) do
        if type(key) == 'number' or tonumber(key) then
            local index = tonumber(key)
            if index > maxIndex then maxIndex = index end
            if value and value ~= '' then
                links[index] = value
            end
        elseif type(key) == 'string' then
            local labelIndex = key:match('^label(%d+)$')
            if labelIndex then
                labelIndex = tonumber(labelIndex)
                if labelIndex and value and value ~= '' then
                    labels[labelIndex] = value
                end
            end
        end
    end
    
    local linkList = {}
    for i = 1, maxIndex do
        if links[i] then
            if labels[i] then
                table.insert(linkList, '[[' .. links[i] .. '|' .. labels[i] .. ']]')
            else
                table.insert(linkList, '[[' .. links[i] .. ']]')
            end
        end
    end
    
    return p._joinList(linkList)
end

function p._joinList(list)
    local count = #list
    
    if count == 0 then
        return ""
    elseif count == 1 then
        return list[1]
    elseif count == 2 then
        return list[1] .. "和" .. list[2]
    else
        local firstPart = table.concat(list, "、", 1, count - 1)
        return firstPart .. "和" .. list[count]
    end
end

return p