Модуль:Песочница/Pok: различия между версиями

Материал из Space Station 14 Вики
мНет описания правки
Нет описания правки
(не показано 46 промежуточных версий этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs


local function split(s, sep)
local function is_array(tbl)
if s == nil then return {} end
    local max = 0
local parts = {}
    local count = 0
sep = sep or "%."
    for k in pairs(tbl) do
for part in string.gmatch(s, "([^" .. sep .. "]+)") do
        if type(k) ~= "number" then
table.insert(parts, part)
            return false
end
        end
return parts
        if k > max then max = k end
        count = count + 1
    end
    return count > 0 and max == count
end
end


local function parse_indexed_part(part)
local function apply_pattern(s, pattern, repl)
-- supports "key", "key[1]" and numeric "1" forms
    if not pattern or pattern == "" or not s then
local key, idx = string.match(part, "^(.-)%[(%d+)%]$")
        return s
if key then
    end
return key, tonumber(idx)
 
end
    local text = tostring(s)
-- numeric-only part (array index)
    local replacement
local num = tonumber(part)
    if repl and repl ~= "" then
if num then
        replacement = tostring(repl)
return nil, num
        replacement = replacement:gsub("\\(%d)", "%%%1")
end
    else
return part, nil
        replacement = "%1"
    end
 
    local patt = pattern
    if not patt:find("%^") and not patt:find("%$") then
        patt = "^" .. patt .. "$"
    end
 
    return (text:gsub(patt, replacement))
end
end


local function get_by_path(tbl, path)
function p.get(frame)
if not tbl then return nil end
    local args = getArgs(frame, { removeBlanks = false })
local parts = split(path, "%.")
    local id = args[1] or ""
local cur = tbl
    if id == "" then return "" end
for _, part in ipairs(parts) do
 
local key, idx = parse_indexed_part(part)
    local params = ""
if key and key ~= "" then
    for k, v in pairs(args) do
cur = cur[key]
        if k ~= 1 then
end
            params = params .. "|" .. k .. "=" .. v
if idx then
        end
-- Lua tables from mw.loadData are 1-based for arrays, so we use the index directly.
    end
if type(cur) ~= "table" then
 
return nil
    local text = "{{#invoke:Сущность/data|get|" .. id .. params .. "}}[[Категория:Сущности]]" -- TODO: {{ajax|<nowiki></nowiki>|auto}}
end
 
cur = cur[idx]
    return frame:preprocess(text)
end
if cur == nil then
return nil
end
end
return cur
end
end


local function is_array(t)
function p.preview(frame)
if type(t) ~= "table" then return false end
    local args = getArgs(frame, { removeBlanks = false })
local i = 0
    local id = args[1] or ""
for _ in pairs(t) do
    if id == "" then return "" end
i = i + 1
 
-- if any key is not a number then not a pure array
    local params = ""
end
    for k, v in pairs(args) do
-- We'll treat table as array if all keys are numeric 1..n
        if k ~= 1 then
local n = 0
            params = params .. "|" .. k .. "=" .. v
for k in pairs(t) do
        end
if type(k) ~= "number" then
    end
return false
 
end
    local text = "{{#invoke:Сущность/data|preview|" .. id .. params .. "}}"
if k > n then n = k end
 
end
    return frame:preprocess(text)
return n > 0
end
end


local function format_value(v)
function p.json(frame)
if v == nil then return "" end
    local args = getArgs(frame, { removeBlanks = false })
local t = type(v)
    local jsonStr = mw.text.unstripNoWiki(args[1] or args.json or "")
if t == "string" or t == "number" or t == "boolean" then
    local tplPath = mw.text.unstripNoWiki(args[2] or args.template or "")
return tostring(v)
    if jsonStr == "" or tplPath == "" then return "" end
elseif t == "table" then
 
-- array-like: join with commas
    local ok, data = pcall(mw.text.jsonDecode, jsonStr)
if is_array(v) then
    if not ok or type(data) ~= "table" then
local out = {}
        return ""
local max = 0
    end
for k in pairs(v) do if type(k) == "number" and k > max then max = k end end
 
for i = 1, max do
    local okDp, dp = pcall(require, "Module:GetField")
table.insert(out, format_value(v[i]))
 
end
    local calls = {}
return table.concat(out, ", ")
 
else
    local function makeCall(id, obj)
-- associative table: "k: v" pairs joined by ", "
        if type(id) ~= "string" then return end
local out = {}
        local parts = { "{{" .. tplPath, "id=" .. id }
for k, val in pairs(v) do
        if type(obj) == "table" then
table.insert(out, tostring(k) .. ": " .. format_value(val))
            if okDp and dp and type(dp.flattenParams) == "function" then
end
                local extra = dp.flattenParams(obj)
return table.concat(out, ", ")
                for i = 1, #extra do
end
                    parts[#parts + 1] = extra[i]
else
                end
return tostring(v)
            else
end
                for k, v in pairs(obj) do
                    if v ~= nil then
                        parts[#parts + 1] = tostring(k) .. "=" .. tostring(v)
                    end
                end
            end
        end
        parts[#parts + 1] = "}}"
        calls[#calls + 1] = table.concat(parts, "|")
    end
 
    if is_array(data) then
        for _, item in ipairs(data) do
            if type(item) == "table" then
                for k, v in pairs(item) do
                    makeCall(k, v)
                end
            end
        end
    else
        for k, v in pairs(data) do
            makeCall(k, v)
        end
    end
 
    if #calls == 0 then
        return ""
    end
 
    local rendered = table.concat(calls, " ")
    return frame:preprocess(rendered)
end
end


-- Public entry point for invocation
function p.jsonList(frame)
function p.get(frame)
    local args = getArgs(frame, { removeBlanks = false })
local args = frame.args or {}
    local jsonStr = mw.text.unstripNoWiki(args[1] or args.json or "")
local id = args[1] or ""
    if jsonStr == "" then return "" end
local pagePath = args[2] or ""
 
local keyPath = args[3] or ""
    local ok, data = pcall(mw.text.jsonDecode, jsonStr)
    if not ok or type(data) ~= "table" then
        return ""
    end
 
    local outputType = (args.type or "list"):lower()
 
    local bullet = mw.text.unstripNoWiki(args.prefix or "* ")
    local sep = mw.text.unstripNoWiki(args.sep or ": ")
    if outputType == "none" then
        bullet = ""
        sep = ""
    end
    local keyPattern = mw.text.unstripNoWiki(args.key_pattern or "(.*)")
    local keyReplace = mw.text.unstripNoWiki(args.key_replace or "\\1")
    local valuePattern = mw.text.unstripNoWiki(args.value_pattern or "(.*)")
    local valueReplace = mw.text.unstripNoWiki(args.value_replace or "\\1")
 
    local pairPattern = mw.text.unstripNoWiki(args.pattern or "(.*)")
    local pairReplace = mw.text.unstripNoWiki(args.replace or "\\1")
 
    local out = {}
 
    if is_array(data) then
        for _, v in ipairs(data) do
            local text = ""
 
            if type(v) == "table" then
                if is_array(v) then
                    text = table.concat(v, ", ")
                else
                    local okJson, jsonVal = pcall(mw.text.jsonEncode, v)
                    if okJson and jsonVal then
                        text = jsonVal
                    end
                end
            else
                text = tostring(v)
            end
 
            if text ~= "" then
                local patt = valuePattern ~= "" and valuePattern or keyPattern
                local repl = valueReplace ~= "" and valueReplace or keyReplace
                text = apply_pattern(text, patt, repl)
 
                local line
                if outputType == "enum" then
                    line = text
                else
                    line = bullet .. text
                end
 
                if pairPattern ~= "" then
                    line = apply_pattern(line, pairPattern, pairReplace)
                end
 
                table.insert(out, line)
            end
        end
    else
        local keys = {}
        for k in pairs(data) do
            keys[#keys + 1] = k
        end
        table.sort(keys, function(a, b) return tostring(a) < tostring(b) end)
 
        for _, k in ipairs(keys) do
            local v = data[k]
            local vStr
 
            if type(v) == "table" then
                local okJson, jsonVal = pcall(mw.text.jsonEncode, v)
                if okJson and jsonVal then
                    vStr = jsonVal
                else
                    vStr = ""
                end
            else
                vStr = tostring(v)
            end
 
            local baseKey = apply_pattern(tostring(k), keyPattern, "\\1")
 
            local MARK_KEY = "\31KEY\31"
            local vRepl = (valueReplace or "\\1"):gsub("\\2", MARK_KEY)
            local vStr0 = apply_pattern(vStr, valuePattern, vRepl)
            vStr0 = tostring(vStr0):gsub(MARK_KEY, baseKey)
 
            local MARK_VAL = "\31VAL\31"
            local kRepl = (keyReplace or "\\1"):gsub("\\2", MARK_VAL)
            local keyStr0 = apply_pattern(tostring(k), keyPattern, kRepl)
            local keyStr = tostring(keyStr0):gsub(MARK_VAL, vStr0)


-- Build module load path. The user requested appending to IanComradeBot/
            vStr = vStr0
local baseUser = "IanComradeBot/"
local moduleName = "Module:" .. baseUser .. pagePath .. "/data"


-- Try load
            if vStr ~= "" then
local ok, data = pcall(mw.loadData, moduleName)
                local line
if not ok or not data then
                if outputType == "enum" then
-- Fallback: try using pagePath directly as a Module name (in case the caller passed full path)
                    line = vStr .. " " .. keyStr
local altModuleName = "Module:" .. pagePath .. "/data"
                else
ok, data = pcall(mw.loadData, altModuleName)
                    line = bullet .. keyStr .. sep .. vStr
if not ok or not data then
                end
return ""
end
end


-- Find by id, fallback to default
                if pairPattern ~= "" then
local entry = nil
                    line = apply_pattern(line, pairPattern, pairReplace)
if id ~= "" then
                end
entry = data[id]
end
if entry == nil then
entry = data["default"]
end
if entry == nil then
return ""
end


if keyPath == "" then
                table.insert(out, line)
-- If no keyPath provided, return formatted entire entry
            end
return format_value(entry)
        end
end
    end


local value = get_by_path(entry, keyPath)
    if outputType == "enum" then
return format_value(value)
        return frame:preprocess(table.concat(out, ", "))
    else
        return frame:preprocess(table.concat(out, "\n"))
    end
end
end


return p
return p

Версия от 00:17, 17 марта 2026

Для документации этого модуля может быть создана страница Модуль:Песочница/Pok/doc

local p = {}
local getArgs = require('Module:Arguments').getArgs

local function is_array(tbl)
    local max = 0
    local count = 0
    for k in pairs(tbl) do
        if type(k) ~= "number" then
            return false
        end
        if k > max then max = k end
        count = count + 1
    end
    return count > 0 and max == count
end

local function apply_pattern(s, pattern, repl)
    if not pattern or pattern == "" or not s then
        return s
    end

    local text = tostring(s)
    local replacement
    if repl and repl ~= "" then
        replacement = tostring(repl)
        replacement = replacement:gsub("\\(%d)", "%%%1")
    else
        replacement = "%1"
    end

    local patt = pattern
    if not patt:find("%^") and not patt:find("%$") then
        patt = "^" .. patt .. "$"
    end

    return (text:gsub(patt, replacement))
end

function p.get(frame)
    local args = getArgs(frame, { removeBlanks = false })
    local id = args[1] or ""
    if id == "" then return "" end

    local params = ""
    for k, v in pairs(args) do
        if k ~= 1 then
            params = params .. "|" .. k .. "=" .. v
        end
    end

    local text = "{{#invoke:Сущность/data|get|" .. id .. params .. "}}[[Категория:Сущности]]" -- TODO: {{ajax|<nowiki></nowiki>|auto}}

    return frame:preprocess(text) 
end

function p.preview(frame)
    local args = getArgs(frame, { removeBlanks = false })
    local id = args[1] or ""
    if id == "" then return "" end

    local params = ""
    for k, v in pairs(args) do
        if k ~= 1 then
            params = params .. "|" .. k .. "=" .. v
        end
    end

    local text = "{{#invoke:Сущность/data|preview|" .. id .. params .. "}}"

    return frame:preprocess(text)
end

function p.json(frame)
    local args = getArgs(frame, { removeBlanks = false })
    local jsonStr = mw.text.unstripNoWiki(args[1] or args.json or "")
    local tplPath = mw.text.unstripNoWiki(args[2] or args.template or "")
    if jsonStr == "" or tplPath == "" then return "" end

    local ok, data = pcall(mw.text.jsonDecode, jsonStr)
    if not ok or type(data) ~= "table" then
        return ""
    end

    local okDp, dp = pcall(require, "Module:GetField")

    local calls = {}

    local function makeCall(id, obj)
        if type(id) ~= "string" then return end
        local parts = { "{{" .. tplPath, "id=" .. id }
        if type(obj) == "table" then
            if okDp and dp and type(dp.flattenParams) == "function" then
                local extra = dp.flattenParams(obj)
                for i = 1, #extra do
                    parts[#parts + 1] = extra[i]
                end
            else
                for k, v in pairs(obj) do
                    if v ~= nil then
                        parts[#parts + 1] = tostring(k) .. "=" .. tostring(v)
                    end
                end
            end
        end
        parts[#parts + 1] = "}}"
        calls[#calls + 1] = table.concat(parts, "|")
    end

    if is_array(data) then
        for _, item in ipairs(data) do
            if type(item) == "table" then
                for k, v in pairs(item) do
                    makeCall(k, v)
                end
            end
        end
    else
        for k, v in pairs(data) do
            makeCall(k, v)
        end
    end

    if #calls == 0 then
        return ""
    end

    local rendered = table.concat(calls, " ")
    return frame:preprocess(rendered)
end

function p.jsonList(frame)
    local args = getArgs(frame, { removeBlanks = false })
    local jsonStr = mw.text.unstripNoWiki(args[1] or args.json or "")
    if jsonStr == "" then return "" end

    local ok, data = pcall(mw.text.jsonDecode, jsonStr)
    if not ok or type(data) ~= "table" then
        return ""
    end

    local outputType = (args.type or "list"):lower()

    local bullet = mw.text.unstripNoWiki(args.prefix or "* ")
    local sep = mw.text.unstripNoWiki(args.sep or ": ")
    if outputType == "none" then
        bullet = ""
        sep = ""
    end
    local keyPattern = mw.text.unstripNoWiki(args.key_pattern or "(.*)")
    local keyReplace = mw.text.unstripNoWiki(args.key_replace or "\\1")
    local valuePattern = mw.text.unstripNoWiki(args.value_pattern or "(.*)")
    local valueReplace = mw.text.unstripNoWiki(args.value_replace or "\\1")

    local pairPattern = mw.text.unstripNoWiki(args.pattern or "(.*)")
    local pairReplace = mw.text.unstripNoWiki(args.replace or "\\1")

    local out = {}

    if is_array(data) then
        for _, v in ipairs(data) do
            local text = ""

            if type(v) == "table" then
                if is_array(v) then
                    text = table.concat(v, ", ")
                else
                    local okJson, jsonVal = pcall(mw.text.jsonEncode, v)
                    if okJson and jsonVal then
                        text = jsonVal
                    end
                end
            else
                text = tostring(v)
            end

            if text ~= "" then
                local patt = valuePattern ~= "" and valuePattern or keyPattern
                local repl = valueReplace ~= "" and valueReplace or keyReplace
                text = apply_pattern(text, patt, repl)

                local line
                if outputType == "enum" then
                    line = text
                else
                    line = bullet .. text
                end

                if pairPattern ~= "" then
                    line = apply_pattern(line, pairPattern, pairReplace)
                end

                table.insert(out, line)
            end
        end
    else
        local keys = {}
        for k in pairs(data) do
            keys[#keys + 1] = k
        end
        table.sort(keys, function(a, b) return tostring(a) < tostring(b) end)

        for _, k in ipairs(keys) do
            local v = data[k]
            local vStr

            if type(v) == "table" then
                local okJson, jsonVal = pcall(mw.text.jsonEncode, v)
                if okJson and jsonVal then
                    vStr = jsonVal
                else
                    vStr = ""
                end
            else
                vStr = tostring(v)
            end

            local baseKey = apply_pattern(tostring(k), keyPattern, "\\1")

            local MARK_KEY = "\31KEY\31"
            local vRepl = (valueReplace or "\\1"):gsub("\\2", MARK_KEY)
            local vStr0 = apply_pattern(vStr, valuePattern, vRepl)
            vStr0 = tostring(vStr0):gsub(MARK_KEY, baseKey)

            local MARK_VAL = "\31VAL\31"
            local kRepl = (keyReplace or "\\1"):gsub("\\2", MARK_VAL)
            local keyStr0 = apply_pattern(tostring(k), keyPattern, kRepl)
            local keyStr = tostring(keyStr0):gsub(MARK_VAL, vStr0)

            vStr = vStr0

            if vStr ~= "" then
                local line
                if outputType == "enum" then
                    line = vStr .. " " .. keyStr
                else
                    line = bullet .. keyStr .. sep .. vStr
                end

                if pairPattern ~= "" then
                    line = apply_pattern(line, pairPattern, pairReplace)
                end

                table.insert(out, line)
            end
        end
    end

    if outputType == "enum" then
        return frame:preprocess(table.concat(out, ", "))
    else
        return frame:preprocess(table.concat(out, "\n"))
    end
end

return p