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

мНет описания правки
Нет описания правки
 
(не показано 68 промежуточных версий этого же участника)
Строка 1: Строка 1:
-- Загрузка данных
local accentData = mw.loadData("Модуль:IanComradeBot/prototypes/accent.json/data")
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs


function p.table(frame)
function p.main(frame)
     local targetId = frame.args[1] or ""
     local args = getArgs(frame, { removeBlanks = false })
     if targetId == "" then
    local name = args[1] or ""
         return "Ошибка: не указан параметр id."
    local attributes = args[2] or ""
     if name == "" then
         return "<span class=\"error\">Ошибка: не указано имя файла.</span>"
     end
     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 = {}


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


     for _, item in ipairs(accentData) do
     for i = 1, max do
         if item.id == targetId then
        local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
             foundData = item
         if t and t.exists then
            break
             table.insert(found, "-" .. i)
         end
         end
     end
     end


     if not foundData then
     if #found == 0 then
         return "Ошибка: данные по id '" .. targetId .. "' не найдены."
         return ""
     end
     end


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


     for key, value in pairs(foundData.wordReplacements) do
     local parts = {}
        table.insert(output, string.format("| {{Cut-Layout|background-color=var(--bg-color-message-subtle)|%s|%s}}", key, value))
    table.insert(parts, "<choose before=\"" .. before .. "\" after=\"" .. after .. "\">")
         table.insert(output, "|-")
    for _, suf in ipairs(found) do
         table.insert(parts, "<option>" .. suf .. "</option>")
     end
     end
    table.insert(parts, "</choose>")


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


return p
return p