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

мНет описания правки
Нет описания правки
 
(не показаны 82 промежуточные версии этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs


function p.main(frame)
function p.main(frame)
     local args = frame.args
     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 = {}


     local id = args[1]
     if include_base then
    if not id then
        local t = mw.title.new("Файл:" .. name .. "." .. ext)
        return "Ошибка: не указан обязательный параметр id."
        if t and t.exists then
            table.insert(found, "")
        end
     end
     end
   
    local itemName = frame:preprocess("{{#invoke:Entity Lookup|getname|" .. id .. "}}")


     local prefix = args[2] or ""
     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


     local label = args["label"] or itemName
     if #found == 0 then
        return ""
    end


     local image
     local before = "[[" .. namespace .. ":" .. name
    if args["image"] then
    local after = "." .. ext .. "|" .. attributes .. "]]"
        local size = args["size"] or "32px"
        image = string.format("[[Файл:%s|%s]]", args["image"], size)
    else
        image = string.format("[[Файл:%s.png|%s]]", id, size)
    end


     local labelOutput = label
     local parts = {}
     if args["link"] then
     table.insert(parts, "<choose before=\"" .. before .. "\" after=\"" .. after .. "\">")
        local linkTarget = args["link"]
    for _, suf in ipairs(found) do
        if linkTarget == "" then
         table.insert(parts, "<option>" .. suf .. "</option>")
            linkTarget = itemName
         end
        labelOutput = string.format("[[%s|%s]]", linkTarget, label)
     end
     end
    table.insert(parts, "</choose>")


    local result = image .. " " .. prefix .. " " .. labelOutput
     return frame:preprocess(table.concat(parts, "\n"))
     return frame:preprocess(result)
end
end


return p
return p