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

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


local function trim(s)
local function is_array(tbl)
if not s then return s end
    local max = 0
return (s:gsub("^%s*(.-)%s*$", "%1"))
    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
end


local function parse_keys_from_template(content, switches)
local function apply_pattern(s, pattern, repl)
if not content then return {} end
    if not pattern or pattern == "" or not s then
local lower = content:lower()
        return s
local result = {}
    end
for _, sw in ipairs(switches) do
 
result[sw] = {}
    local text = tostring(s)
local swLower = sw:lower()
    local replacement
local pos = lower:find("|%s*" .. swLower .. "%s*=")
    if repl and repl ~= "" then
if not pos then
        replacement = tostring(repl)
-- no keys for this switch
        replacement = replacement:gsub("\\(%d)", "%%%1")
else
    else
local startBrace = content:find("{{", pos)
        replacement = "%1"
local region = nil
    end
if startBrace then
 
local len = #content
    local patt = pattern
local i = startBrace
    if not patt:find("%^") and not patt:find("%$") then
local depth = 0
        patt = "^" .. patt .. "$"
while i <= len - 1 do
    end
local two = content:sub(i, i+1)
 
if two == "{{" then
    return (text:gsub(patt, replacement))
depth = depth + 1
i = i + 2
elseif two == "}}" then
depth = depth - 1
i = i + 2
if depth == 0 then
region = content:sub(startBrace, i-1)
break
end
else
i = i + 1
end
end
end
if not region then
local substr = content:sub(pos)
local endPos = substr:find("}}")
if endPos then
region = substr:sub(1, endPos)
else
region = substr
end
end
for key in string.gmatch(region, "|%s*([^=|%}]-)%s*=") do
local k = trim(key)
if k ~= "" then
table.insert(result[sw], k)
end
end
end
end
return result
end
end


local function load_module_data(page)
function p.get(frame)
local baseUser = "IanComradeBot/"
    local args = getArgs(frame, { removeBlanks = false })
local moduleName = "Module:" .. baseUser .. page .. "/data"
    local id = args[1] or ""
local ok, data = pcall(mw.loadData, moduleName)
    if id == "" then return "" end
if not ok then return nil end
 
return data
    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
end


local function load_template_content(path)
function p.preview(frame)
local title = mw.title.new("Template:" .. path)
    local args = getArgs(frame, { removeBlanks = false })
if not title then return nil end
    local id = args[1] or ""
local ok, content = pcall(function() return title:getContent() end)
    if id == "" then return "" end
if not ok then return nil end
 
return content
    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
end


function p.get(frame)
function p.jsonList(frame)
local args = frame.args or {}
    local args = getArgs(frame, { removeBlanks = false })
local id = args[1] or ""
    local jsonStr = mw.text.unstripNoWiki(args[1] or args.json or "")
if id == "" then return "" end
    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


local componentDefs = load_module_data("component.json")
                table.insert(out, line)
local prototypeDefs = load_module_data("prototype.json")
            end
if not componentDefs or not prototypeDefs then
        end
return ""
    else
end
        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)


local foundComponents = {}
        for _, k in ipairs(keys) do
local foundPrototypes = {}
            local v = data[k]
local compList = componentDefs[id]
            local vStr
if type(compList) == "table" then
for _, v in ipairs(compList) do
if type(v) == "string" then
foundComponents[v] = true
end
end
end


local protoList = prototypeDefs[id]
            if type(v) == "table" then
if type(protoList) == "table" then
                local okJson, jsonVal = pcall(mw.text.jsonEncode, v)
for _, v in ipairs(protoList) do
                if okJson and jsonVal then
if type(v) == "string" then
                    vStr = jsonVal
foundPrototypes[v] = true
                else
end
                    vStr = ""
end
                end
end
            else
                vStr = tostring(v)
            end


for name in string.gmatch(id, "[^,]+") do
            local baseKey = apply_pattern(tostring(k), keyPattern, "\\1")
local n = trim(name)
if n ~= "" then
if componentDefs[n] ~= nil then
foundComponents[n] = true
end
if prototypeDefs[n] ~= nil then
foundPrototypes[n] = true
end
if componentDefs[n] == nil and prototypeDefs[n] == nil then
foundComponents[n] = true
end
end
end


local out = {}
            local MARK_KEY = "\31KEY\31"
local errors = {}
            local vRepl = (valueReplace or "\\1"):gsub("\\2", MARK_KEY)
local switches = { "card", "title" }
            local vStr0 = apply_pattern(vStr, valuePattern, vRepl)
local switchKeyOrder = {}
            vStr0 = tostring(vStr0):gsub(MARK_KEY, baseKey)
local switchKeyToTemplates = {}
for _, sw in ipairs(switches) do
switchKeyOrder[sw] = {}
switchKeyToTemplates[sw] = {}
end
local switchConfigs = {
card = {
wrapper = function(key, tplCalls)
if not tplCalls or #tplCalls == 0 then return "" end
return "{{card|" .. mw.text.encode(key) .. "|" .. table.concat(tplCalls, " ") .. "}}"
end
},
title = {
wrapper = function(key, tplCalls)
local parts = {}
table.insert(parts, "<h2>" .. mw.text.encode(key) .. "</h2>")
if tplCalls and #tplCalls > 0 then
for _, tpl in ipairs(tplCalls) do
table.insert(parts, "<p>" .. tpl .. "</p>")
end
end
return table.concat(parts, "\n")
end
}
}


local function lcfirst(s)
            local MARK_VAL = "\31VAL\31"
if not s or s == "" then return s end
            local kRepl = (keyReplace or "\\1"):gsub("\\2", MARK_VAL)
return string.lower(s:sub(1,1)) .. (s:sub(2) or "")
            local keyStr0 = apply_pattern(tostring(k), keyPattern, kRepl)
end
            local keyStr = tostring(keyStr0):gsub(MARK_VAL, vStr0)


local items = {}
            vStr = vStr0
for compName,_ in pairs(foundComponents) do
table.insert(items, { kind = "component", name = compName })
end
for protoName,_ in pairs(foundPrototypes) do
table.insert(items, { kind = "prototype", name = protoName })
end


for _, item in ipairs(items) do
            if vStr ~= "" then
local kind = item.kind
                local line
local name = item.name
                if outputType == "enum" then
local pathName = lcfirst(name)
                    line = vStr .. " " .. keyStr
local tplPath = kind .. "/" .. pathName
                else
local content = load_template_content(tplPath)
                    line = bullet .. keyStr .. sep .. vStr
if not content then
                end
table.insert(errors, "Ошибка: не найден шаблон " .. kind .. "/" .. pathName)
else
local parsed = parse_keys_from_template(content, switches)
for _, sw in ipairs(switches) do
local keys = parsed[sw] or {}
for _, key in ipairs(keys) do
if not switchKeyToTemplates[sw][key] then
switchKeyToTemplates[sw][key] = {}
table.insert(switchKeyOrder[sw], key)
end
local param = sw
local extra = ""
local ok, dp = pcall(require, "Module:Песочница/Pok/2")
if ok and dp and dp.flattenComponent then
extra = dp.flattenComponent({ args = { id, tplPath } })
end
-- build data map from extra (format: k=val|k2=val2)
local dataMap = {}
if extra and extra ~= "" then
for pair in string.gmatch(extra, "([^|]+)") do
local k, v = pair:match("^([^=]+)=(.*)$")
if k and v then
dataMap[k] = mw.text.decode(v)
end
end
end
-- try to extract RHS for this key from the template content and substitute placeholders
local function extract_rhs(templateContent, swName, matchKey)
local lower = templateContent:lower()
local pos = lower:find("|%s*" .. swName:lower() .. "%s*=")
if not pos then return nil end
local innerStart = templateContent:find("{{", pos)
if not innerStart then return nil end
local len = #templateContent
local i = innerStart
local depth = 0
local regionEnd = nil
while i <= len - 1 do
local two = templateContent:sub(i, i+1)
if two == "{{" then depth = depth + 1; i = i + 2
elseif two == "}}" then depth = depth - 1; i = i + 2
if depth == 0 then regionEnd = i - 1; break end
else i = i + 1 end
end
local region = templateContent:sub(innerStart, (regionEnd or #templateContent))
local esc = matchKey:gsub("([^%w])", "%%%1")
local pattern = "|%s*" .. esc .. "%s*=%s*([^|%}]+)"
local rhs = region:match(pattern)
if rhs then return rhs end
return nil
end
local rhs = extract_rhs(content, sw, key)
if rhs then
local rendered = rhs
for k, v in pairs(dataMap) do
rendered = rendered:gsub("{{{" .. k .. "}}}", v)
rendered = rendered:gsub("{{{" .. k .. "|[^}]-}}}", v)
end
table.insert(switchKeyToTemplates[sw][key], trim(rendered))
else
local tplStr = "{{" .. tplPath .. "|" .. param .. "|" .. key
if extra and extra ~= "" then
tplStr = tplStr .. "|" .. extra
end
tplStr = tplStr .. "}}"
table.insert(switchKeyToTemplates[sw][key], tplStr)
end
end
end
end
end


for _, e in ipairs(errors) do
                if pairPattern ~= "" then
table.insert(out, "<div class=\"error\">" .. mw.text.encode(e) .. "</div>")
                    line = apply_pattern(line, pairPattern, pairReplace)
end
                end


for _, sw in ipairs(switches) do
                table.insert(out, line)
local cfg = switchConfigs[sw] or {}
            end
for _, key in ipairs(switchKeyOrder[sw]) do
        end
local tplCalls = switchKeyToTemplates[sw][key] or {}
    end
if cfg.wrapper then
local outStr = cfg.wrapper(key, tplCalls)
if outStr and outStr ~= "" then
table.insert(out, outStr)
end
end
end
end


return frame:preprocess(table.concat(out, "\n\n"))
    if outputType == "enum" then
        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