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

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


local p = {}
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 = {}


function p.getSize(frame)
     if include_base then
     -- Получаем первый параметр, который должен быть id
        local t = mw.title.new("Файл:" .. name .. "." .. ext)
    local id = frame.args[1] or frame.args.id
        if t and t.exists then
    if not id then
            table.insert(found, "")
        return "Ошибка: не передан id"
        end
     end
     end


    -- Проходим по всем записям в sizeData
     for i = 1, max do
     for _, item in ipairs(sizeData) do
        local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
         if item.id == id then
         if t and t.exists then
             return item.size
             table.insert(found, "-" .. i)
         end
         end
     end
     end


     -- Если не нашли совпадение, возвращаем стандартный текст
     if #found == 0 then
     return "Не имеет размера"
        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
end


return p
return p