Модуль:Сущность: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 135: | Строка 135: | ||
if not switchKeyToTemplates[sw][key] then | if not switchKeyToTemplates[sw][key] then | ||
switchKeyToTemplates[sw][key] = {} | switchKeyToTemplates[sw][key] = {} | ||
table.insert(switchKeyOrder[sw], key) | table.insert(switchKeyOrder[sw], key) | ||
end | end | ||
| Строка 144: | Строка 143: | ||
end | end | ||
local tplStr = makeTplCall(tplPath, sw, key, id, extra) | local tplStr = makeTplCall(tplPath, sw, key, id, extra) | ||
local priority = 1 | |||
if parsed and parsed.priority ~= nil then | |||
{ kind = kind, name = name, pathName = pathName, tplPath = tplPath }) | 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 | ||
end | end | ||
| Строка 160: | Строка 172: | ||
local cfg = switchConfigs[sw] or {} | local cfg = switchConfigs[sw] or {} | ||
for _, key in ipairs(switchKeyOrder[sw]) do | for _, key in ipairs(switchKeyOrder[sw]) do | ||
local | local entries = switchKeyToTemplates[sw][key] or {} | ||
local sources = | 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 cfg.wrapper then | if cfg.wrapper then | ||
local outStr = cfg.wrapper(key, tplCalls, sources) | local outStr = cfg.wrapper(key, tplCalls, sources) | ||
Версия от 20:58, 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 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 "{{карточка/Сущность|" .. 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 function getTemplateMeta(frame, tplPath)
local expanded = frame:expandTemplate {
title = tplPath,
args = { "json" }
}
local ok, data = pcall(mw.text.jsonDecode, expanded)
if ok and type(data) == "table" then
return data
end
return ""
end
local errors = {}
local function processEntity(kind, name)
local pathName = lcfirst(name)
local tplPath = kind .. "/" .. pathName
local content = load_template_content(tplPath)
if not content then
local classType = (kind == "component" and "Component" or "Prototype")
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
if not switchKeyToTemplates[sw][key] then
switchKeyToTemplates[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)
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
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 processEntity(it.kind, it.name) end
local out = {}
for _, sw in ipairs(switches) do
local cfg = switchConfigs[sw] or {}
for _, key in ipairs(switchKeyOrder[sw]) do
local entries = switchKeyToTemplates[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 cfg.wrapper then
local outStr = cfg.wrapper(key, tplCalls, sources)
if outStr and outStr ~= "" then table.insert(out, outStr) end
end
end
end
for _, e in ipairs(errors) do table.insert(out, e) end
return frame:preprocess(table.concat(out, "\n\n"))
end
return p