Module:Defines

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

local p = {};

local getArgs = require('Module:Arguments').getArgs

local NDefines = require("Module:Defines/List")

function p.read_define(frame)
    local args = getArgs(frame)
    return p._read_define(args)
end
function p._read_define(args)
    s = NDefines[args[1]][args[2]]
    if type(s)=="string" then
        s = "\"" .. s .. "\""
    end
    if type(s)=="table" and args[3] then
        s = s[tonumber(args[3])]
        if type(s) == "table" and args[4] then
            s = s[tonumber(args[4])]
        end
    end
    if type(s)=="table" then
        for k, v in pairs(s) do
            if type(v)=="table" then
                s[k] = "{ " .. table.concat(v, ", ") .. " }"
            end
        end
        s = "{ " .. table.concat(s, ", ") .. " }"
    end
    if args.percent == "yes" then
        s = s * 100 .. "%"
    end
    return s
end

function p.create_reference(frame)
    local args = getArgs(frame)
    return p._create_reference(args)
end
function p._create_reference(args)
    s = string.format("<code>NDefines.%s.%s", args[1], args[2])
    if args[3] then
        s = s .. string.format("[%s]", args[3])
    end
    if args[4] then
        s = s .. string.format("[%s]", args[4])
    end
    return s .. string.format(" = %s</code>", p._read_define(args))
end
function p.create_references(frame)
    local args = getArgs(frame)
    return p._create_references(args)
end
function p._create_references(args)
    tableName = args[1]
    tbl = NDefines[tableName]
    base = "<code>NDefines." .. tableName .. "."
    output = {}
    for i, j in pairs(args) do
        if i ~= 1 and type(i) == "number" then
            s = tbl[j]
            if type(s)=="string" then
                s = "\"" .. s .. "\""
            elseif type(s)=="table" then
                for k, v in pairs(s) do
                    if type(v)=="table" then
                        s[k] = "{ " .. table.concat(v, ", ") .. " }"
                    end
                end
                s = "{ " .. table.concat(s, ", ") .. " }"
            elseif type(s)=="boolean" then
                if(s) then
                    s = "true"
                else
                    s = "false"
                end
            end
            s = base .. j .. " = " .. s .. "</code>"
            output[i-1] = s
        end
    end
    if args.sep then
        sep = args.sep:gsub("\\n", "\n"):gsub("_", " ")
        s = table.concat(output, sep)
    elseif #output == 1 then
        s = output[1]
    elseif #output == 2 then
        s = output[1] .. " and " .. output[2]
    else
        s = table.concat(output, ", ", 1, #output-1) .. ", and " .. output[#output]
    end
    return s
end
return p