Модуль:Песочница/Pok: различия между версиями

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


local function trim(s)
function p.main(frame)
if not s then return s end
    local args = getArgs(frame, { removeBlanks = false })
return (s:gsub("^%s*(.-)%s*$", "%1"))
    local name = args[1] or ""
end
    local attributes = args[2] or ""
    if name == "" then
        return "<span class=\"error\">Ошибка: не указано имя файла.</span>"
    end
    local ext = (args["ext"] or "png"):gsub("^%.", "")  
    local namespace = args["namespace"] or "Файл"
    local max = tonumber(args["max"]) or 50
    local include_base = (args["base"] ~= "no")


local function split(s, sep)
    local found = {}
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 include_base then
if not content then return {} end
        local t = mw.title.new("Файл:" .. name .. "." .. ext)
local keys = {}
        if t and t.exists then
local titlePos = string.find(content, "|%s*Title%s*=")
            table.insert(found, "")
if not titlePos then
        end
return keys
    end
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)
    for i = 1, max do
local baseUser = "IanComradeBot/"
        local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
local moduleName = "Module:" .. baseUser .. page .. "/data"
        if t and t.exists then
local ok, data = pcall(mw.loadData, moduleName)
            table.insert(found, "-" .. i)
if not ok then return nil end
        end
return data
    end
end


local function load_template_content(path)
    if #found == 0 then
local title = mw.title.new("Template:" .. path)
        return ""
if not title then return nil end
    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 before = "[[" .. namespace .. ":" .. name
local args = frame.args or {}
    local after = "." .. ext .. "|" .. attributes .. "]]"
local id = args[1] or ""
if id == "" then return "" end


local componentDefs = load_module_data("component.json")
    local parts = {}
local prototypeDefs = load_module_data("prototype.json")
    table.insert(parts, "<choose before=\"" .. before .. "\" after=\"" .. after .. "\">")
local entities = load_module_data("entity_prototypes.json")
    for _, suf in ipairs(found) do
if not componentDefs or not prototypeDefs then
        table.insert(parts, "<option>" .. suf .. "</option>")
return ""
    end
end
    table.insert(parts, "</choose>")
if not entities then
return ""
end


local entity = entities[id]
    return frame:preprocess(table.concat(parts, "\n"))
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
end


return p
return p

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

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

local p = {}
local getArgs = require('Module:Arguments').getArgs

function p.main(frame)
    local args = getArgs(frame, { removeBlanks = false })
    local name = args[1] or ""
    local attributes = args[2] or ""
    if name == "" then
        return "<span class=\"error\">Ошибка: не указано имя файла.</span>"
    end
    local ext = (args["ext"] or "png"):gsub("^%.", "") 
    local namespace = args["namespace"] or "Файл"
    local max = tonumber(args["max"]) or 50
    local include_base = (args["base"] ~= "no")

    local found = {}

    if include_base then
        local t = mw.title.new("Файл:" .. name .. "." .. ext)
        if t and t.exists then
            table.insert(found, "")
        end
    end

    for i = 1, max do
        local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
        if t and t.exists then
            table.insert(found, "-" .. i)
        end
    end

    if #found == 0 then
        return ""
    end

    local before = "[[" .. namespace .. ":" .. name
    local after = "." .. ext .. "|" .. attributes .. "]]"

    local parts = {}
    table.insert(parts, "<choose before=\"" .. before .. "\" after=\"" .. after .. "\">")
    for _, suf in ipairs(found) do
        table.insert(parts, "<option>" .. suf .. "</option>")
    end
    table.insert(parts, "</choose>")

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

return p