Модуль:Песочница/Pok: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local function trim(s) | |||
if not s then return s end | |||
return (s:gsub("^%s*(.-)%s*$", "%1")) | |||
end | |||
local function split(s, sep) | local function split(s, sep) | ||
| Строка 11: | Строка 16: | ||
end | end | ||
local function | local function parse_keys_from_template(content) | ||
local | if not content then return {} end | ||
if | local keys = {} | ||
return | local titlePos = string.find(content, "|%s*Title%s*=") | ||
if not titlePos then | |||
return keys | |||
end | end | ||
local | local substr = content:sub(titlePos) | ||
if | local endPos = substr:find("}}") | ||
local region = substr | |||
if endPos then | |||
region = substr:sub(1, endPos) | |||
end | end | ||
for key in string.gmatch(region, "|%s*([^=|%}]-)%s*=") do | |||
local k = trim(key) | |||
if k ~= "" then | |||
table.insert(keys, k) | |||
local | |||
if | |||
end | end | ||
end | end | ||
return | return keys | ||
end | end | ||
local function | local function load_module_data(page) | ||
local baseUser = "IanComradeBot/" | |||
local | local moduleName = "Module:" .. baseUser .. page .. "/data" | ||
local ok, data = pcall(mw.loadData, moduleName) | |||
if not ok then return nil end | |||
return data | |||
local | |||
return | |||
end | end | ||
local function | local function load_template_content(path) | ||
local title = mw.title.new("Template:" .. path) | |||
if not title then return nil end | |||
if | local ok, content = pcall(function() return title:getContent() end) | ||
if not ok then return nil end | |||
return content | |||
end | end | ||
| Строка 97: | Строка 57: | ||
local args = frame.args or {} | local args = frame.args or {} | ||
local id = args[1] or "" | local id = args[1] or "" | ||
if id == "" then return "" end | |||
local | local componentDefs = load_module_data("component.json") | ||
local | local prototypeDefs = load_module_data("prototype.json") | ||
local entities = load_module_data("entity_prototypes.json") | |||
if not componentDefs or not prototypeDefs then | |||
return "" | |||
end | |||
if not entities then | |||
return "" | |||
end | |||
local | local entity = entities[id] | ||
if not | if not entity then | ||
return "" | return "" | ||
end | end | ||
local | local foundComponents = {} | ||
if | -- If entity explicitly lists components use that | ||
if type(entity.components) == "table" then | |||
for _, v in ipairs(entity.components) do | |||
if type(v) == "string" then | |||
foundComponents[v] = true | |||
elseif type(v) == "table" and v.name then | |||
foundComponents[v.name] = true | |||
end | |||
end | |||
else | |||
-- fallback: detect component keys present in entity | |||
for compName,_ in pairs(componentDefs) do | |||
if entity[compName] ~= nil then | |||
foundComponents[compName] = true | |||
end | |||
end | |||
end | end | ||
if | |||
local foundPrototypes = {} | |||
if type(entity.prototypes) == "table" then | |||
for _, v in ipairs(entity.prototypes) do | |||
if type(v) == "string" then | |||
foundPrototypes[v] = true | |||
end | |||
end | |||
else | |||
for protoName,_ in pairs(prototypeDefs) do | |||
if entity[protoName] ~= nil then | |||
foundPrototypes[protoName] = true | |||
end | |||
end | |||
end | end | ||
local out = {} | |||
for compName,_ in pairs(foundComponents) do | |||
local tplPath = "component/" .. compName | |||
local content = load_template_content(tplPath) | |||
if not content then | |||
table.insert(out, "<h2>" .. mw.text.encode(compName) .. "</h2>") | |||
table.insert(out, "Template: " .. tplPath) | |||
else | |||
local keys = parse_keys_from_template(content) | |||
for _, key in ipairs(keys) do | |||
table.insert(out, "<h2>" .. key .. "</h2>") | |||
table.insert(out, "{{" .. tplPath .. "|title|" .. key .. "}}") | |||
end | |||
end | |||
end | end | ||
for protoName,_ in pairs(foundPrototypes) do | |||
local tplPath = "prototype/" .. protoName | |||
local content = load_template_content(tplPath) | |||
if not content then | |||
table.insert(out, "<h2>" .. mw.text.encode(protoName) .. "</h2>") | |||
table.insert(out, "Template: " .. tplPath) | |||
else | |||
local keys = parse_keys_from_template(content) | |||
for _, key in ipairs(keys) do | |||
table.insert(out, "<h2>" .. key .. "</h2>") | |||
table.insert(out, "{{" .. tplPath .. "|title|" .. key .. "}}") | |||
end | |||
end | |||
end | end | ||
return table.concat(out, "\n\n") | |||
end | end | ||
return p | return p | ||
Версия от 12:14, 21 января 2026
Для документации этого модуля может быть создана страница Модуль:Песочница/Pok/doc
local p = {}
local function trim(s)
if not s then return s end
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
local function split(s, sep)
if s == nil then return {} end
local parts = {}
sep = sep or "%."
for part in string.gmatch(s, "([^" .. sep .. "]+)") do
table.insert(parts, part)
end
return parts
end
local function parse_keys_from_template(content)
if not content then return {} end
local keys = {}
local titlePos = string.find(content, "|%s*Title%s*=")
if not titlePos then
return keys
end
local substr = content:sub(titlePos)
local endPos = substr:find("}}")
local region = substr
if endPos then
region = substr:sub(1, endPos)
end
for key in string.gmatch(region, "|%s*([^=|%}]-)%s*=") do
local k = trim(key)
if k ~= "" then
table.insert(keys, k)
end
end
return keys
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
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")
local entities = load_module_data("entity_prototypes.json")
if not componentDefs or not prototypeDefs then
return ""
end
if not entities then
return ""
end
local entity = entities[id]
if not entity then
return ""
end
local foundComponents = {}
-- If entity explicitly lists components use that
if type(entity.components) == "table" then
for _, v in ipairs(entity.components) do
if type(v) == "string" then
foundComponents[v] = true
elseif type(v) == "table" and v.name then
foundComponents[v.name] = true
end
end
else
-- fallback: detect component keys present in entity
for compName,_ in pairs(componentDefs) do
if entity[compName] ~= nil then
foundComponents[compName] = true
end
end
end
local foundPrototypes = {}
if type(entity.prototypes) == "table" then
for _, v in ipairs(entity.prototypes) do
if type(v) == "string" then
foundPrototypes[v] = true
end
end
else
for protoName,_ in pairs(prototypeDefs) do
if entity[protoName] ~= nil then
foundPrototypes[protoName] = true
end
end
end
local out = {}
for compName,_ in pairs(foundComponents) do
local tplPath = "component/" .. compName
local content = load_template_content(tplPath)
if not content then
table.insert(out, "<h2>" .. mw.text.encode(compName) .. "</h2>")
table.insert(out, "Template: " .. tplPath)
else
local keys = parse_keys_from_template(content)
for _, key in ipairs(keys) do
table.insert(out, "<h2>" .. key .. "</h2>")
table.insert(out, "{{" .. tplPath .. "|title|" .. key .. "}}")
end
end
end
for protoName,_ in pairs(foundPrototypes) do
local tplPath = "prototype/" .. protoName
local content = load_template_content(tplPath)
if not content then
table.insert(out, "<h2>" .. mw.text.encode(protoName) .. "</h2>")
table.insert(out, "Template: " .. tplPath)
else
local keys = parse_keys_from_template(content)
for _, key in ipairs(keys) do
table.insert(out, "<h2>" .. key .. "</h2>")
table.insert(out, "{{" .. tplPath .. "|title|" .. key .. "}}")
end
end
end
return table.concat(out, "\n\n")
end
return p