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

мНет описания правки
Метка: отменено
Нет описания правки
(не показаны 22 промежуточные версии этого же участника)
Строка 1: Строка 1:
-- Загрузка данных
local p = {}
local accentData = mw.loadData("Модуль:IanComradeBot/prototypes/accent.json/data")
 
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 lower = content:lower()
local titlePos = lower:find("|%s*title%s*=")
if not titlePos then
return keys
end
local startBrace = content:find("{{", titlePos)
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(titlePos)
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(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 p = {}
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")
if not componentDefs or not prototypeDefs then
return ""
end


function p.table(frame)
local foundComponents = {}
    local targetId = frame.args[1] or ""
local foundPrototypes = {}
    if targetId == "" then
local compList = componentDefs[id]
        return "Ошибка: не указан параметр id."
if type(compList) == "table" then
    end
for _, v in ipairs(compList) do
if type(v) == "string" then
foundComponents[v] = true
end
end
end


    local foundData = nil
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 _, item in ipairs(accentData) do
for name in string.gmatch(id, "[^,]+") do
        if item.id == targetId then
local n = trim(name)
            foundData = item
if n ~= "" then
            break
if componentDefs[n] ~= nil then
        end
foundComponents[n] = true
    end
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


    if not foundData then
local out = {}
        return "Ошибка: данные по id '" .. targetId .. "' не найдены."
local errors = {}
    end
local keyOrder = {}
local keyToTemplates = {}


    local output = {}
local function lcfirst(s)
table.insert(output, '{| class="sortable-grid sortable-grid--small-column sortable"')
if not s or s == "" then return s end
table.insert(output, '! Сортировка (по алфавиту)')
return string.lower(s:sub(1,1)) .. (s:sub(2) or "")
table.insert(output, '|-')
end


for key, value in pairs(foundData.wordReplacements) do
for compName,_ in pairs(foundComponents) do
local keyData = "{{#invoke:Ftl|main|translation|".. key .."}}"
local compPathName = lcfirst(compName)
local valueData = "{{#invoke:Ftl|main|translation|".. value .."}}"
local tplPath = "component/" .. compPathName
    if keyData ~= "Ошибка: Ключ не найден." and valueData ~= "Ошибка: Ключ не найден." then
local content = load_template_content(tplPath)
        table.insert(output, "|")
if not content then
        table.insert(
table.insert(errors, "Ошибка: не найден шаблон component/" .. compPathName)
            output,
else
            frame:preprocess(
local keys = parse_keys_from_template(content)
                string.format(
for _, key in ipairs(keys) do
                    "{{Cut-Layout|arrow=1|background-color=var(--bg-color-message-subtle)|%s|%s}}",
if not keyToTemplates[key] then
                    keyData,
keyToTemplates[key] = {}
                    valueData
table.insert(keyOrder, key)
                )
end
            )
table.insert(keyToTemplates[key], "{{" .. tplPath .. "|title|" .. key .. "}}")
        )
end
        table.insert(output, "|-")
end
    end
end
end


table.insert(output, '|}')
for protoName,_ in pairs(foundPrototypes) do
local protoPathName = lcfirst(protoName)
local tplPath = "prototype/" .. protoPathName
local content = load_template_content(tplPath)
if not content then
table.insert(errors, "Ошибка: не найден шаблон prototype/" .. protoPathName)
else
local keys = parse_keys_from_template(content)
for _, key in ipairs(keys) do
if not keyToTemplates[key] then
keyToTemplates[key] = {}
table.insert(keyOrder, key)
end
table.insert(keyToTemplates[key], "{{" .. tplPath .. "|title|" .. key .. "}}")
end
end
end
 
for _, e in ipairs(errors) do
table.insert(out, "<div class=\"error\">" .. mw.text.encode(e) .. "</div>")
end
 
for _, key in ipairs(keyOrder) do
table.insert(out, "<h2>" .. mw.text.encode(key) .. "</h2>")
for _, tplCall in ipairs(keyToTemplates[key]) do
table.insert(out, tplCall)
end
end


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


return p
return p