Module:State

Katty von Keksburg讨论 | 贡献2024年5月13日 (一) 21:31的版本

可在Module:State/doc创建此模块的帮助文档

local p = {}

-- 导入州名数据模块
local states = require('Module:State/List')

-- 导入参数处理模块
local getArgs = require('Module:Arguments').getArgs

-- 根据ID获取州名(包括name2和name)
function p._state_name(args)
    local id = tonumber(args[1])
    if not states[id] then
        return "<span style=\"color: red;\">[[Module:State/List]] does not have state:</span>"
    else
        local name2 = states[id].name2 or ""
        local name = states[id].name or ""
        -- 返回name2和name
        return name2 .. " (" .. name .. ")"
    end
end

-- 根据多个ID获取州名列表(包括name2和name)
function p._state_names(args)
    local output = {}
    for i, j in pairs(args) do
        if type(i) == "number" then
            if not states[tonumber(j)] then
                return "<span style=\"color: red;\">[[Module:State/List]] does not have state: " .. j .. "</span>"
            end
            -- 返回name2和name
            local name2 = states[tonumber(j)].name2 or ""
            local name = states[tonumber(j)].name or ""
            if not args.nob then
                name = string.format("'''%s'''", name)
            end
            if not args.noid then
                name = name .. string.format(" ''(%s)''", j)
            end
            output[i] = name2 .. " (" .. name .. ")"
        end
    end
    -- 根据参数中的分隔符进行分隔
    if args.sep then
        local sep = args.sep:gsub("\\n", "\n"):gsub("_", " ")
        return table.concat(output, sep)
    elseif #output == 1 then
        return output[1]
    elseif #output == 2 then
        return output[1] .. " and " .. output[2]
    else
        return table.concat(output, ", ", 1, #output-1) .. ", and " .. output[#output]
    end
end

-- 导出模块
return p