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

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


local function trim(s)
local function trim(s)
     if not s then return s end
     if not s then return "" end
     return (s:gsub("^%s*(.-)%s*$", "%1"))
     return mw.text.trim(tostring(s))
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
     if not ok then return nil end
        return ""
    return data
    end
end
 
     local params = ""
     local hasExtraArgs = false


local function load_template_content(path)
    for k, v in pairs(args) do
    local title = mw.title.new("Template:" .. path)
        if k ~= 1 and k ~= "cardTag" and k ~= "tag" and v ~= nil and v ~= "" then
    if not title then return nil end
            params = params .. "|" .. k .. "=" .. v
    local ok, content = pcall(function() return title:getContent() end)
    if not ok then return nil end
    return content
end


local function parse_keys_from_template(content, switches)
             if k ~= "blacklist" then
    if not content then return {} end
                 hasExtraArgs = true
    local lower = content:lower()
    local result = {}
    for _, sw in ipairs(switches) do
        result[sw] = {}
        local swLower = sw:lower()
        local pos = lower:find("|%s*" .. swLower .. "%s*=")
        if pos then
            local startBrace = content:find("{{", pos)
            local region = nil
             if startBrace then
                local len = #content
                local i = startBrace
                local depth = 0
                while i <= len - 1 do
                    local two = content: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
                            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
         end
     end
     end
    return result
end


local function lcfirst(s)
    local ajax = "{{ajax|<nowiki>{{#invoke:Сущность/data|get|" .. id .. params .. "}}</nowiki>|auto}}"
    if not s or s == "" then return s end
    local text = ajax
    return string.lower(s:sub(1, 1)) .. (s:sub(2) or "")
end


local function makeTplCall(tplPath, sw, key, id, extra)
    if not hasExtraArgs then
    local tplStr = "{{" .. tplPath .. "|" .. sw .. "|" .. key
        local cardTagText = entityData.collectCardTagText(frame, args, id)
    tplStr = tplStr .. "|id=" .. tostring(id)
        if cardTagText ~= "" then
    if extra and extra ~= "" then tplStr = tplStr .. "|" .. extra end
            params = params .. "|cardTag=" .. cardTagText
    tplStr = tplStr .. "}}"
        end
     return tplStr
        text =
end
            "{{#if:{{NAMESPACE}}|{{переключатель_проекта|[[Категория:{{NAMESPACE}}:Сущности]]{{NAMESPACE}}}}|[[Категория:Сущности]]{{переключатель_проекта}}}}" ..
            '<span style="display:none;">{{#invoke:CategoryFormatter|main|' .. cardTagText .. '}}</span>' .. ajax
     end


local function makeSourceLink(s)
     return frame:preprocess(text)
     local className = s.name .. (s.kind == "component" and "Component" or "Prototype")
    local tplLabel = "Template:" .. s.tplPath
    return "[[" .. tplLabel .. "|" .. className .. "]]"
end
end


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


     local componentDefs = load_module_data("component.json")
     local params = ""
    local prototypeDefs = load_module_data("prototype.json")
     for k, v in pairs(args) do
    if not componentDefs or not prototypeDefs then return "" end
        if k ~= 1 and k ~= "cardTag" and k ~= "tag" then
 
            params = params .. "|" .. k .. "=" .. v
    local foundComponents, foundPrototypes = {}, {}
    local compList = componentDefs[id]
     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(protoList) == "table" then for _, v in ipairs(protoList) do if type(v) == "string" then foundPrototypes[v] = true 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 prototypeDefs[n] ~= nil then foundPrototypes[n] = true end
            if componentDefs[n] == nil and prototypeDefs[n] == nil then foundComponents[n] = true end
         end
         end
     end
     end


     local switches = { "card", "title" }
     local cardTagText = entityData.mergeCardTagText(args.cardTag or "", args.tag or "")
    local switchKeyOrder, switchKeyToTemplates, switchKeySources = {}, {}, {}
    if cardTagText ~= "" then
    for _, sw in ipairs(switches) do
         params = params .. "|cardTag=" .. trim(cardTagText)
        switchKeyOrder[sw] = {}; switchKeyToTemplates[sw] = {}; switchKeySources[sw] = {}
    end
 
    local switchConfigs = {
        card = {
            wrapper = function(key, tplCalls, sources)
                if not tplCalls or #tplCalls == 0 then return "" end
                local calls = table.concat(tplCalls, " ")
                local srcStr = ""
                if sources and #sources > 0 then
                    local srcParts = {}
                    for _, s in ipairs(sources) do table.insert(srcParts, makeSourceLink(s)) end
                    srcStr = " " .. table.concat(srcParts, " ")
                end
                return "{{card|" .. mw.text.encode(key) .. "|" .. calls .. srcStr .. "}}"
            end
        },
        title = {
            wrapper = function(key, tplCalls, sources)
                local parts = {}
                table.insert(parts, "<h2>" .. mw.text.encode(key) .. "</h2>")
                if tplCalls and #tplCalls > 0 then
                    for i, tpl in ipairs(tplCalls) do
                        local line = tpl
                        local src = sources and sources[i]
                        if src then line = line .. " " .. makeSourceLink(src) end
                        table.insert(parts, "<p>" .. line .. "</p>")
                    end
                end
                return table.concat(parts, "\n")
            end
        }
    }
 
    local errors = {}
    local function processItem(kind, name)
        local pathName = lcfirst(name)
        local tplPath = kind .. "/" .. pathName
        local content = load_template_content(tplPath)
         if not content then
            local className = name .. (kind == "component" and "Component" or "Prototype")
            local tplLabel = "Template:" .. tplPath
            if kind == "component" then
                table.insert(errors,
                    "Ошибка: сущность имеется не описаный компонент [[" .. tplLabel .. "|" .. className .. "]]")
            else
                table.insert(errors,
                    "Ошибка: сущность содержится в не описанном прототипе [[" .. tplLabel .. "|" .. className .. "]]")
            end
            return
        end
        local parsed = parse_keys_from_template(content, switches)
        local ok, dp = pcall(require, "Module:DataParser")
        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] = {}
                    switchKeySources[sw][key] = {}
                    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)
                table.insert(switchKeyToTemplates[sw][key], tplStr)
                table.insert(switchKeySources[sw][key],
                    { kind = kind, name = name, pathName = pathName, tplPath = tplPath })
            end
        end
     end
     end


     local items = {}
     local text = "{{#invoke:Сущность/data|preview|" .. id .. params .. "}}"
    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 _, it in ipairs(items) do processItem(it.kind, it.name) end
 
    local out = {}
    for _, e in ipairs(errors) do table.insert(out, "<div class=\"error\">" .. e .. "</div>") end
    for _, sw in ipairs(switches) do
        local cfg = switchConfigs[sw] or {}
        for _, key in ipairs(switchKeyOrder[sw]) do
            local tplCalls = switchKeyToTemplates[sw][key] or {}
            local sources = switchKeySources[sw][key] or {}
            if cfg.wrapper then
                local outStr = cfg.wrapper(key, tplCalls, sources)
                if outStr and outStr ~= "" then table.insert(out, outStr) end
            end
        end
    end


     return frame:preprocess(table.concat(out, "\n\n"))
     return frame:preprocess(text)
end
end


return p
return p

Текущая версия от 18:08, 6 апреля 2026

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

local p = {}
local getArgs = require('Module:Arguments').getArgs
local entityData = require('Module:Сущность/data')

local function trim(s)
    if not s then return "" end
    return mw.text.trim(tostring(s))
end

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

    local params = ""
    local hasExtraArgs = false

    for k, v in pairs(args) do
        if k ~= 1 and k ~= "cardTag" and k ~= "tag" and v ~= nil and v ~= "" then
            params = params .. "|" .. k .. "=" .. v

            if k ~= "blacklist" then
                hasExtraArgs = true
            end
        end
    end

    local ajax = "{{ajax|<nowiki>{{#invoke:Сущность/data|get|" .. id .. params .. "}}</nowiki>|auto}}"
    local text = ajax

    if not hasExtraArgs then
        local cardTagText = entityData.collectCardTagText(frame, args, id)
        if cardTagText ~= "" then
            params = params .. "|cardTag=" .. cardTagText
        end
        text =
            "{{#if:{{NAMESPACE}}|{{переключатель_проекта|[[Категория:{{NAMESPACE}}:Сущности]]{{NAMESPACE}}}}|[[Категория:Сущности]]{{переключатель_проекта}}}}" ..
            '<span style="display:none;">{{#invoke:CategoryFormatter|main|' .. cardTagText .. '}}</span>' .. ajax
    end

    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 and k ~= "cardTag" and k ~= "tag" then
            params = params .. "|" .. k .. "=" .. v
        end
    end

    local cardTagText = entityData.mergeCardTagText(args.cardTag or "", args.tag or "")
    if cardTagText ~= "" then
        params = params .. "|cardTag=" .. trim(cardTagText)
    end

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

    return frame:preprocess(text)
end

return p