Module:参数

来自植物大战僵尸百科
跳转到导航 跳转到搜索
{{🛈}}模組文檔[查看] [編輯] [歷史] [清除缓存]此文档嵌入自Module:参数/文档

本模組是元模組,在其他模組中用於加載參數模組。

模組內容

p

本模組的返回值。

p.getMsg(frame)
從框架對象frame接收參數。frame.args是表,第一項爲參數模組頁面名字串,第二項爲鍵名字串,返回值。
p.loadData(source)
輸入參數模組source字串,必須帶有命名空間,必須使用頁面原名。返回對應資料的參數ts對象,參數模組的返回值在ts._data中。

ts

ts:msg{ key = key, sub = sub, pre = pre, args = args }
ts:msg{ key, sub, ... }
ts:msg(opts, ...)
ts:msg(key, ...)
輸入內容說明:
key
鍵名字串。
sub
子鍵名字串,預設爲數字1
pre
二元,是否預處理返回的訊息,預設爲假false
args
參數表。
...
參數表中各項的值。
opts
設定表,包括keysub(或subkey)、preargsargs...不應同時輸入。
找到的訊息爲ts._data[key],如果其是表,則改爲ts._data[key][sub]。將其中的$dd爲從1開始的整數)替換爲args{ ... }中對應的值。之後根據pre決定是否預處理並返回結果。
ts:parameter(key, args)
key爲鍵名字串,args爲參數表。若p等於key,且之後會遍歷ts._data[key]中提供的參數名,則返回第一個找到的args[p]
ts:numberedParameter(key, num, args)
ts:parameter相似,但要輸入數字numkey中用%d標出了數字num在參數名稱中的位置。
ts:compare(key, compare)
key爲鍵名字串,compare爲要對比的字串。若p等於key,且之後會遍歷ts._data[key]中提供的參數名,找到第一個匹配的args[p]時返回真true,否則返回假false
----修改自[[Dev:I18n]],功能減少了很多。
local p = {}

local ts = {}
ts.__index = ts

-- 尋找訊息
function ts:msg(opts, ...)
    if not self or not opts then
        error('ts:msg缺少參數')
    end

    -- 初始化
    local key, subkey, args, preprocess
    if type(opts) == 'table' then
        key = opts.key or table.remove(opts, 1) or error('ts:msg缺少參數')
        subkey = opts.sub or opts.subkey or table.remove(opts, 1) or 1
        preprocess = opts.pre or opts.preprocess
        args = opts.args or opts or { ... }
    else
        key = opts
        subkey = 1
        args = { ... }
    end

    -- 尋找對應訊息
    local msg = self._data[key]
    if type(msg) == 'table' then
        msg = msg[subkey]
    end
    if msg then
        for i, a in ipairs(args) do
            msg = (string.gsub(msg, '%$' .. tostring(i), tostring(a)))
        end
        return preprocess and mw.getCurrentFrame():preprocess(msg) or msg
    end
end

-- 在所有別名中尋找參數
function ts:parameter(key, args)
    if not self or not key or not args then
        error('ts:parameter缺少參數')
    end

    local arg = args[key]
    if arg then
        return arg
    end
    local aliases = self._data[key]
    arg = aliases and args[aliases]
    if arg then -- 找到訊息
        return arg
    end
    if type(aliases) == 'table' then -- 訊息在表中
        for _, alias in ipairs(aliases) do
            arg = alias and args[alias]
            if arg then
                return arg
            end
        end
    end
    return nil
end

-- 在所有別名中尋找參數,key參數中需要表示數字位置
function ts:numberedParameter(key, num, args)
    if not self or not key or not args then
        error('ts:numberedParameter缺少參數')
    end

    local function connectNum(alias)
        if type(alias) ~= 'string' then
            return alias
        end
        return string.format(alias, num)
    end

    num = num or 1

    local arg = args[connectNum(key)]
    if arg then
        return arg
    end
    local aliases = self._data[key]
    arg = aliases and args[connectNum(aliases)]
    if arg then
        return arg
    end
    if type(aliases) == 'table' then
        for _, alias in ipairs(aliases) do
            arg = alias and args[connectNum(alias)]
            if arg then
                return arg
            end
        end
    end
    return nil
end

-- 是否匹配目標內容的一項
function ts:compare(key, compare)
    if not self or not key or not compare then
        error('ts:compare缺少參數')
    end

    if key == compare then
        return true
    end
    local aliases = self._data[key]
    if aliases == compare then
        return true
    end
    if type(aliases) == 'table' then
        for _, alias in ipairs(aliases) do
            if alias == compare then
                return true
            end
        end
    end
    return false
end

-- #invoke 直接尋找訊息
function p.getMsg(frame)
    if type(frame) ~= 'table' or type(frame.args) ~= 'table' or not frame.args[1] or not frame.args[2] then
        error('p.getMsg缺少參數')
    end
    local source = table.remove(frame.args, 1)
    local key = table.remove(frame.args, 2)
    local ptsd = p.loadData(source)
    return ptsd:msg { key = key, args = frame.args } or key
end

-- 讀入資料
function p.loadData(source)
    if type(source) ~= 'string' then
        error('p.loadData的參數不是字符串')
    end
    local ptsd = setmetatable({}, ts)
    if type(source) == 'string' then
        ptsd._data = mw.loadData(source)
    end
    return ptsd
end

return p