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

Материал из Space Station 14 Вики
мНет описания правки
мНет описания правки
Строка 2: Строка 2:


local function trim(s)
local function trim(s)
if not s then return s end
    if not s then return s end
return (s:gsub("^%s*(.-)%s*$", "%1"))
    return (s:gsub("^%s*(.-)%s*$", "%1"))
end
end


local function parse_keys_from_template(content, switches)
local function load_module_data(page)
if not content then return {} end
    local baseUser = "IanComradeBot/"
local lower = content:lower()
    local moduleName = "Module:" .. baseUser .. page .. "/data"
local result = {}
    local ok, data = pcall(mw.loadData, moduleName)
for _, sw in ipairs(switches) do
    if not ok then return nil end
result[sw] = {}
    return data
local swLower = sw:lower()
local pos = lower:find("|%s*" .. swLower .. "%s*=")
if not pos then
-- no keys for this switch
else
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
return result
end
end


local function load_module_data(page)
local function load_template_content(path)
local baseUser = "IanComradeBot/"
    local title = mw.title.new("Template:" .. path)
local moduleName = "Module:" .. baseUser .. page .. "/data"
    if not title then return nil end
local ok, data = pcall(mw.loadData, moduleName)
    local ok, content = pcall(function() return title:getContent() end)
if not ok then return nil end
    if not ok then return nil end
return data
    return content
end
end


local function load_template_content(path)
local function parse_keys_from_template(content, switches)
local title = mw.title.new("Template:" .. path)
    if not content then return {} end
if not title then return nil end
    local lower = content:lower()
local ok, content = pcall(function() return title:getContent() end)
    local result = {}
if not ok then return nil end
    for _, sw in ipairs(switches) do
return content
        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
    return result
end
end


function p.get(frame)
local function lcfirst(s)
local args = frame.args or {}
    if not s or s == "" then return s end
local id = args[1] or ""
    return string.lower(s:sub(1, 1)) .. (s:sub(2) or "")
if id == "" then return "" end
end


local componentDefs = load_module_data("component.json")
local function makeTplCall(tplPath, sw, key, id, extra)
local prototypeDefs = load_module_data("prototype.json")
    local tplStr = "{{" .. tplPath .. "|" .. sw .. "|" .. key
if not componentDefs or not prototypeDefs then
    tplStr = tplStr .. "|id=" .. tostring(id)
return ""
    if extra and extra ~= "" then tplStr = tplStr .. "|" .. extra end
end
    tplStr = tplStr .. "}}"
    return tplStr
end


local foundComponents = {}
local function makeSourceLink(s)
local foundPrototypes = {}
    local className = s.name .. (s.kind == "component" and "Component" or "Prototype")
local compList = componentDefs[id]
    local tplLabel = "Template:" .. s.tplPath
if type(compList) == "table" then
    return "[[" .. tplLabel .. "|" .. className .. "]]"
for _, v in ipairs(compList) do
end
if type(v) == "string" then
foundComponents[v] = true
end
end
end


local protoList = prototypeDefs[id]
function p.get(frame)
if type(protoList) == "table" then
    local args = frame.args or {}
for _, v in ipairs(protoList) do
    local id = args[1] or ""
if type(v) == "string" then
    if id == "" then return "" end
foundPrototypes[v] = true
end
end
end


for name in string.gmatch(id, "[^,]+") do
    local componentDefs = load_module_data("component.json")
local n = trim(name)
    local prototypeDefs = load_module_data("prototype.json")
if n ~= "" then
    if not componentDefs or not prototypeDefs then return "" end
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 foundComponents, foundPrototypes = {}, {}
local errors = {}
    local compList = componentDefs[id]
local switches = { "card", "title" }
    if type(compList) == "table" then for _, v in ipairs(compList) do if type(v) == "string" then foundComponents[v] = true end end end
local switchKeyOrder = {}
    local protoList = prototypeDefs[id]
local switchKeyToTemplates = {}
    if type(protoList) == "table" then for _, v in ipairs(protoList) do if type(v) == "string" then foundPrototypes[v] = true end end end
for _, sw in ipairs(switches) do
    for name in string.gmatch(id, "[^,]+") do
switchKeyOrder[sw] = {}
        local n = trim(name)
switchKeyToTemplates[sw] = {}
        if n ~= "" then
end
            if componentDefs[n] ~= nil then foundComponents[n] = true end
local switchConfigs = {
            if prototypeDefs[n] ~= nil then foundPrototypes[n] = true end
card = {
            if componentDefs[n] == nil and prototypeDefs[n] == nil then foundComponents[n] = true end
wrapper = function(key, tplCalls)
        end
if not tplCalls or #tplCalls == 0 then return "" end
    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 switches = { "card", "title" }
if not s or s == "" then return s end
    local switchKeyOrder, switchKeyToTemplates, switchKeySources = {}, {}, {}
return string.lower(s:sub(1,1)) .. (s:sub(2) or "")
    for _, sw in ipairs(switches) do
end
        switchKeyOrder[sw] = {}; switchKeyToTemplates[sw] = {}; switchKeySources[sw] = {}
    end


local items = {}
    local switchConfigs = {
for compName,_ in pairs(foundComponents) do
        card = {
table.insert(items, { kind = "component", name = compName })
            wrapper = function(key, tplCalls, sources)
end
                if not tplCalls or #tplCalls == 0 then return "" end
for protoName,_ in pairs(foundPrototypes) do
                local calls = table.concat(tplCalls, " ")
table.insert(items, { kind = "prototype", name = protoName })
                local srcStr = ""
end
                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
        }
    }


for _, item in ipairs(items) do
    local errors = {}
local kind = item.kind
    local function processItem(kind, name)
local name = item.name
        local pathName = lcfirst(name)
local pathName = lcfirst(name)
        local tplPath = kind .. "/" .. pathName
local tplPath = kind .. "/" .. pathName
        local content = load_template_content(tplPath)
local content = load_template_content(tplPath)
        if not content then
if not content then
            local className = name .. (kind == "component" and "Component" or "Prototype")
table.insert(errors, "Ошибка: не найден шаблон " .. kind .. "/" .. pathName)
            local tplLabel = "Template:" .. tplPath
else
            if kind == "component" then
local parsed = parse_keys_from_template(content, switches)
                table.insert(errors,
for _, sw in ipairs(switches) do
                    "Ошибка: сущность имеется не описаный компонент [[" .. tplLabel .. "|" .. className .. "]]")
local keys = parsed[sw] or {}
            else
for _, key in ipairs(keys) do
                table.insert(errors,
if not switchKeyToTemplates[sw][key] then
                    "Ошибка: сущность содержится в не описанном прототипе [[" .. tplLabel .. "|" .. className .. "]]")
switchKeyToTemplates[sw][key] = {}
            end
table.insert(switchKeyOrder[sw], key)
            return
end
        end
local param = sw
        local parsed = parse_keys_from_template(content, switches)
local extra = ""
        local ok, dp = pcall(require, "Module:DataParser")
local ok, dp = pcall(require, "Module:GetField")
        for _, sw in ipairs(switches) do
if ok and dp and dp.flattenField then
            local keys = parsed[sw] or {}
local dataPage = kind .. "/" .. pathName .. ".json"
            for _, key in ipairs(keys) do
extra = dp.flattenField({ args = { id, dataPage } })
                if not switchKeyToTemplates[sw][key] then
end
                    switchKeyToTemplates[sw][key] = {}
local tplStr = "{{" .. tplPath .. "|" .. param .. "|" .. key
                    switchKeySources[sw][key] = {}
tplStr = tplStr .. "|id=" .. tostring(id)
                    table.insert(switchKeyOrder[sw], key)
if extra and extra ~= "" then
                end
tplStr = tplStr .. "|" .. extra
                local extra = ""
end
                if ok and dp and dp.flattenField then
tplStr = tplStr .. "}}"
                    local dataPage = tplPath .. ".json"
table.insert(switchKeyToTemplates[sw][key], tplStr)
                    extra = dp.flattenField({ args = { id, dataPage } })
end
                end
end
                local tplStr = makeTplCall(tplPath, sw, key, id, extra)
end
                table.insert(switchKeyToTemplates[sw][key], tplStr)
end
                table.insert(switchKeySources[sw][key],
                    { kind = kind, name = name, pathName = pathName, tplPath = tplPath })
            end
        end
    end


for _, e in ipairs(errors) do
    local items = {}
table.insert(out, "<div class=\"error\">" .. mw.text.encode(e) .. "</div>")
    for compName, _ in pairs(foundComponents) do table.insert(items, { kind = "component", name = compName }) end
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


for _, sw in ipairs(switches) do
    local out = {}
local cfg = switchConfigs[sw] or {}
    for _, e in ipairs(errors) do table.insert(out, "<div class=\"error\">" .. e .. "</div>") end
for _, key in ipairs(switchKeyOrder[sw]) do
    for _, sw in ipairs(switches) do
local tplCalls = switchKeyToTemplates[sw][key] or {}
        local cfg = switchConfigs[sw] or {}
if cfg.wrapper then
        for _, key in ipairs(switchKeyOrder[sw]) do
local outStr = cfg.wrapper(key, tplCalls)
            local tplCalls = switchKeyToTemplates[sw][key] or {}
if outStr and outStr ~= "" then
            local sources = switchKeySources[sw][key] or {}
table.insert(out, outStr)
            if cfg.wrapper then
end
                local outStr = cfg.wrapper(key, tplCalls, sources)
end
                if outStr and outStr ~= "" then table.insert(out, outStr) end
end
            end
end
        end
    end


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


return p
return p

Версия от 09:09, 23 января 2026

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

local p = {}

local function trim(s)
    if not s then return s end
    return (s:gsub("^%s*(.-)%s*$", "%1"))
end

local function load_module_data(page)
    local baseUser = "IanComradeBot/"
    local moduleName = "Module:" .. baseUser .. page .. "/data"
    local ok, data = pcall(mw.loadData, moduleName)
    if not ok then return nil end
    return data
end

local function load_template_content(path)
    local title = mw.title.new("Template:" .. path)
    if not title then return nil end
    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 not content then return {} end
    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
    return result
end

local function lcfirst(s)
    if not s or s == "" then return s end
    return string.lower(s:sub(1, 1)) .. (s:sub(2) or "")
end

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

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

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

    local componentDefs = load_module_data("component.json")
    local prototypeDefs = load_module_data("prototype.json")
    if not componentDefs or not prototypeDefs then return "" end

    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

    local switches = { "card", "title" }
    local switchKeyOrder, switchKeyToTemplates, switchKeySources = {}, {}, {}
    for _, sw in ipairs(switches) do
        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

    local items = {}
    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"))
end

return p