Модуль:Сущность: различия между версиями

Материал из Space Station 14 Вики
Нет описания правки
Нет описания правки
 
(не показано 98 промежуточных версий этого же участника)
Строка 2: Строка 2:
local getArgs = require('Module:Arguments').getArgs
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 load_module_data(page)
local function apply_pattern(s, pattern, repl)
     local baseUser = "IanComradeBot/"
     if not pattern or pattern == "" or not s then
    local moduleName = "Module:" .. baseUser .. page .. "/data"
        return s
    local ok, data = pcall(mw.loadData, moduleName)
     end
    if not ok then return nil end
     return data
end


local function load_template_content(path)
    local text = tostring(s)
     local title = mw.title.new("Template:" .. path)
     local replacement
    if not title then return nil end
    if repl and repl ~= "" then
    local ok, content = pcall(function() return title:getContent() end)
        replacement = tostring(repl)
     if not ok then return nil end
        replacement = replacement:gsub("\\(%d)", "%%%1")
     return content
     else
end
        replacement = "%1"
     end


local function lcfirst(s)
    local patt = pattern
     if not s or s == "" then return s end
     if not patt:find("%^") and not patt:find("%$") then
    return string.lower(s:sub(1, 1)) .. (s:sub(2) or "")
        patt = "^" .. patt .. "$"
end
    end


local function makeTplCall(tplPath, sw, key, id, extra)
    return (text:gsub(patt, replacement))
    local tplStr = "{{" .. tplPath .. "|" .. sw .. "|" .. key
    tplStr = tplStr .. "|id=" .. tostring(id)
    if extra and extra ~= "" then tplStr = tplStr .. "|" .. extra end
    tplStr = tplStr .. "}}"
    return tplStr
end
end


local function makeSourceLink(s)
function p.get(frame)
     local className = s.name .. (s.kind and (s.kind:sub(1, 1):upper() .. s.kind:sub(2)) or "")
     local args = getArgs(frame, { removeBlanks = false })
     local tplLabel = "Template:" .. s.tplPath
     local id = args[1] or ""
     return "[[" .. tplLabel .. "|" .. className .. "]]"
     if id == "" then return "" end
end


local function renderTitleBlock(key, tplCalls, sources, includeHeader)
     local params = ""
     local parts = {}
     for k, v in pairs(args) do
    if includeHeader then table.insert(parts, "<h2>" .. mw.text.encode(key) .. "</h2>") end
        if k ~= 1 then
     if tplCalls and #tplCalls > 0 then
             params = params .. "|" .. k .. "=" .. v
        for i, tpl in ipairs(tplCalls) do
            local line = tpl
             local src = sources and sources[i]
            line = '<span>' .. line .. '</span><span class="ts-Сущность-field">' .. makeSourceLink(src) .. '</span>'
            table.insert(parts, '<p class="ts-Сущность">' .. line .. '</p>')
         end
         end
     end
     end
     return table.concat(parts, "\n")
 
     local text = "{{ajax|<nowiki>{{#invoke:Сущность/data|get|" .. id .. params .. "}}</nowiki>|auto}}[[Категория:Сущности]]"
 
    return frame:preprocess(text)  
end
end


local switches = { "card", "title" }
function p.preview(frame)
local switchConfigs = {
    local args = getArgs(frame, { removeBlanks = false })
     card = {
    local id = args[1] or ""
        wrapper = function(key, tplCalls, sources)
     if id == "" then return "" end
            if not tplCalls or #tplCalls == 0 then return "" end
 
            local calls = table.concat(tplCalls, " ")
    local params = ""
            local srcStr = ""
    for k, v in pairs(args) do
            if sources and #sources > 0 then
        if k ~= 1 then
                local srcParts = {}
            params = params .. "|" .. k .. "=" .. v
                for _, s in ipairs(sources) do table.insert(srcParts, makeSourceLink(s)) end
                srcStr = " " .. table.concat(srcParts, " ")
            end
            return "{{карточка/Сущность|" .. mw.text.encode(key) .. "|" .. calls .. srcStr .. "}}"
         end
         end
     },
     end
     title = {
 
        wrapper = function(key, tplCalls, sources)
     local text = "{{#invoke:Сущность/data|preview|" .. id .. params .. "}}"
            return renderTitleBlock(key, tplCalls, sources, true)
 
        end
    return frame:preprocess(text)
    }
end
}


local function getTemplateMeta(frame, tplPath)
function p.json(frame)
     local expanded = frame:expandTemplate {
     local args = getArgs(frame, { removeBlanks = false })
        title = tplPath,
    local jsonStr = mw.text.unstripNoWiki(args[1] or args.json or "")
        args = { "json" }
    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, expanded)
     local ok, data = pcall(mw.text.jsonDecode, jsonStr)
     if ok and type(data) == "table" then
     if not ok or type(data) ~= "table" then
         return data
         return ""
     end
     end


     return ""
     local okDp, dp = pcall(require, "Module:GetField")
end
 
    local calls = {}


local function parseListArg(str)
    local function makeCall(id, obj)
    local res = {}
        if type(id) ~= "string" then return end
    if not str or str == "" then return res end
        local parts = { "{{" .. tplPath, "id=" .. id }
    for item in string.gmatch(str, "[^,]+") do
         if type(obj) == "table" then
         local s = trim(item)
            if okDp and dp and type(dp.flattenParams) == "function" then
        if s ~= "" then
                local extra = dp.flattenParams(obj)
            local a, b = s:match("^([^_]+)_(.+)$")
                for i = 1, #extra do
            if a and b then
                    parts[#parts + 1] = extra[i]
                res[a] = res[a] or {}
                end
                 res[a][b] = true
            else
                for k, v in pairs(obj) do
                    if v ~= nil then
                        parts[#parts + 1] = tostring(k) .. "=" .. tostring(v)
                    end
                 end
             end
             end
         end
         end
        parts[#parts + 1] = "}}"
        calls[#calls + 1] = table.concat(parts, "|")
     end
     end
    return res
end


local function renderBlocks(frame, switchesTbl, configs, keyOrder, keyToTemplates, keySources, noHeaders)
     if is_array(data) then
     local outLocal = {}
         for _, item in ipairs(data) do
    for _, sw in ipairs(switchesTbl) do
             if type(item) == "table" then
        local cfg = configs[sw] or {}
                 for k, v in pairs(item) do
         for _, key in ipairs(keyOrder[sw] or {}) do
                     makeCall(k, v)
            local entries = keyToTemplates[sw][key] or {}
            local tplCalls = {}
            local sources = {}
             if #entries > 0 then
                table.sort(entries, function(a, b)
                    if a.priority == b.priority then return a.idx < b.idx end
                    return a.priority > b.priority
                end)
                 for _, e in ipairs(entries) do
                     table.insert(tplCalls, e.tpl)
                    table.insert(sources, e.source)
                end
            end
            if noHeaders and sw == "title" then
                local outStr = renderTitleBlock(key, tplCalls, sources, false)
                if outStr and outStr ~= "" then table.insert(outLocal, outStr) end
            else
                if cfg.wrapper then
                    local outStr = cfg.wrapper(key, tplCalls, sources)
                    if outStr and outStr ~= "" then table.insert(outLocal, outStr) end
                 end
                 end
             end
             end
        end
    else
        for k, v in pairs(data) do
            makeCall(k, v)
         end
         end
     end
     end
     return outLocal
 
     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 = getArgs(frame, { removeBlanks = false })
     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 blacklist = parseListArg(args.blacklist or "")
     local ok, data = pcall(mw.text.jsonDecode, jsonStr)
    local whitelist = parseListArg(args.whitelist or "")
    if not ok or type(data) ~= "table" then
     local hasWhitelist = next(whitelist) ~= nil
        return ""
     end


     local componentDefs = load_module_data("component.json")
     local outputType = (args.type or "list"):lower()
    local prototypeDefs = load_module_data("prototype.json")
    if not componentDefs or not prototypeDefs then return "" end


     local foundComponents, foundPrototypes = {}, {}
     local bullet = mw.text.unstripNoWiki(args.prefix or "* ")
     local compList = componentDefs[id]
     local sep = mw.text.unstripNoWiki(args.sep or ": ")
     if type(compList) == "table" then
     if outputType == "none" then
         for _, v in ipairs(compList) do
         bullet = ""
            if type(v) == "string" then
        sep = ""
                foundComponents[v] = true
            end
        end
     end
     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 = ""


    local protoEntry = prototypeDefs[id]
            if type(v) == "table" then
    if type(protoEntry) == "table" then
                if is_array(v) then
        for protoName, _ in pairs(protoEntry) do
                    text = table.concat(v, ", ")
            if type(protoName) == "string" then
                else
                 foundPrototypes[protoName] = true
                    local okJson, jsonVal = pcall(mw.text.jsonEncode, v)
                    if okJson and jsonVal then
                        text = jsonVal
                    end
                end
            else
                 text = tostring(v)
             end
             end
        end
    end
    for name in string.gmatch(id, "[^,]+") do
        local n = trim(name)
        if n ~= "" then
            if componentDefs[n] ~= nil then foundComponents[n] = true end
            if componentDefs[n] == nil and prototypeDefs[n] == nil then foundComponents[n] = true end
        end
    end


    local switchKeyOrder, switchKeyToTemplates, switchKeySources = {}, {}, {}
            if text ~= "" then
    for _, sw in ipairs(switches) do
                local patt = valuePattern ~= "" and valuePattern or keyPattern
        switchKeyOrder[sw] = {}; switchKeyToTemplates[sw] = {}; switchKeySources[sw] = {}
                local repl = valueReplace ~= "" and valueReplace or keyReplace
    end
                text = apply_pattern(text, patt, repl)


    local errors = {}
                 local line
    local function processEntity(kind, name)
                 if outputType == "enum" then
        local pathName = lcfirst(name)
                     line = text
        local tplPath = kind .. "/" .. pathName
        local content = load_template_content(tplPath)
        if not content then
            if hasWhitelist then
                 return
            end
            local classType = (kind and (kind:sub(1, 1):upper() .. kind:sub(2)) or "")
            local className = name .. classType
            local tplLabel = "Template:" .. tplPath
            table.insert(errors,
                 "{{сущность/infobox|тип=" .. classType .. "|название=" .. className .. "|ссылка=" .. tplLabel .. "}}")
            return
        end
        local parsed = getTemplateMeta(frame, tplPath)
        local ok, dp = pcall(require, "Module:GetField")
        for _, sw in ipairs(switches) do
            local keys = parsed[sw] or {}
            for _, key in ipairs(keys) do
                local skip = false
                if next(whitelist) ~= nil then
                     if not (whitelist[sw] and whitelist[sw][key]) then skip = true end
                 else
                 else
                     if blacklist[sw] and blacklist[sw][key] then skip = true end
                     line = bullet .. text
                 end
                 end
                 if not skip then
 
                    if not switchKeyToTemplates[sw][key] then
                 if pairPattern ~= "" then
                        switchKeyToTemplates[sw][key] = {}
                     line = apply_pattern(line, pairPattern, pairReplace)
                        table.insert(switchKeyOrder[sw], key)
                    end
                    local extra = ""
                    if ok and dp and dp.flattenField then
                        local dataPage = tplPath .. ".json"
                        extra = dp.flattenField({ args = { id, dataPage } })
                     end
                    local tplStr = makeTplCall(tplPath, sw, key, id, extra)
                    local priority = 1
                    if parsed and parsed.priority ~= nil then
                        if type(parsed.priority) == "number" then
                            priority = parsed.priority
                        else
                            local pnum = tonumber(parsed.priority)
                            if pnum then priority = pnum end
                        end
                    end
                    local entry = {
                        tpl = tplStr,
                        source = { kind = kind, name = name, pathName = pathName, tplPath = tplPath },
                        priority = priority,
                        idx = #switchKeyToTemplates[sw][key] + 1
                    }
                    table.insert(switchKeyToTemplates[sw][key], entry)
                 end
                 end
                table.insert(out, line)
             end
             end
         end
         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)


    local items = {}
        for _, k in ipairs(keys) do
    for compName, _ in pairs(foundComponents) do table.insert(items, { kind = "component", name = compName }) end
            local v = data[k]
    for protoName, _ in pairs(foundPrototypes) do table.insert(items, { kind = "prototype", name = protoName }) end
            local vStr
    for _, it in ipairs(items) do processEntity(it.kind, it.name) end


    local out = {}
            if type(v) == "table" then
    local blocks = renderBlocks(frame, switches, switchConfigs, switchKeyOrder, switchKeyToTemplates, switchKeySources,
                local okJson, jsonVal = pcall(mw.text.jsonEncode, v)
        hasWhitelist)
                if okJson and jsonVal then
    for _, e in ipairs(errors) do table.insert(out, e) end
                    vStr = jsonVal
    for _, b in ipairs(blocks) do table.insert(out, b) end
                else
                    vStr = ""
                end
            else
                vStr = tostring(v)
            end
 
            local baseKey = apply_pattern(tostring(k), keyPattern, "\\1")


    return frame:preprocess(table.concat(out, "\n\n"))
            local MARK_KEY = "\31KEY\31"
end
            local vRepl = (valueReplace or "\\1"):gsub("\\2", MARK_KEY)
            local vStr0 = apply_pattern(vStr, valuePattern, vRepl)
            vStr0 = tostring(vStr0):gsub(MARK_KEY, baseKey)


function p.preview(frame)
            local MARK_VAL = "\31VAL\31"
    local args = getArgs(frame, { removeBlanks = false })
            local kRepl = (keyReplace or "\\1"):gsub("\\2", MARK_VAL)
    local tplPath = args[1] or ""
            local keyStr0 = apply_pattern(tostring(k), keyPattern, kRepl)
    if tplPath == "" then return "" end
            local keyStr = tostring(keyStr0):gsub(MARK_VAL, vStr0)


    local content = load_template_content(tplPath)
            vStr = vStr0
    if not content then
        return ""
    end


    local parsed = getTemplateMeta(frame, tplPath) or {}
            if vStr ~= "" then
                local line
                if outputType == "enum" then
                    line = vStr .. " " .. keyStr
                else
                    line = bullet .. keyStr .. sep .. vStr
                end


    local switchKeyOrder, switchKeyToTemplates, switchKeySources = {}, {}, {}
                if pairPattern ~= "" then
    for _, sw in ipairs(switches) do
                    line = apply_pattern(line, pairPattern, pairReplace)
        switchKeyOrder[sw] = {}; switchKeyToTemplates[sw] = {}; switchKeySources[sw] = {}
                end
    end


    for _, sw in ipairs(switches) do
                 table.insert(out, line)
        local keys = parsed[sw] or {}
        for idx, key in ipairs(keys) do
            if not switchKeyToTemplates[sw][key] then
                switchKeyToTemplates[sw][key] = {}
                 table.insert(switchKeyOrder[sw], key)
             end
             end
            local tplStr = makeTplCall(tplPath, sw, key, "")
            local entry = {
                tpl = tplStr,
                source = { kind = "", name = tplPath, pathName = tplPath, tplPath = tplPath },
                priority = 1,
                idx = #switchKeyToTemplates[sw][key] + 1
            }
            table.insert(switchKeyToTemplates[sw][key], entry)
         end
         end
     end
     end


     local whitelist = parseListArg(args.whitelist or "")
     if outputType == "enum" then
    local hasWhitelist = next(whitelist) ~= nil
        return frame:preprocess(table.concat(out, ", "))
    local out = {}
    else
    local blocks = renderBlocks(frame, switches, switchConfigs, switchKeyOrder, switchKeyToTemplates, switchKeySources,
        return frame:preprocess(table.concat(out, "\n"))
        hasWhitelist)
    end
    for _, b in ipairs(blocks) do table.insert(out, b) end
 
    return frame:preprocess(table.concat(out, "\n\n"))
end
end


return p
return p

Текущая версия от 17:12, 18 марта 2026

Для документации этого модуля может быть создана страница Модуль:Сущность/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 = "{{ajax|<nowiki>{{#invoke:Сущность/data|get|" .. id .. params .. "}}</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