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

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


function p.main(frame)
function p.main(frame)
     local input = frame.args[1] or ""
     local args = getArgs(frame, { removeBlanks = false })
     local results = {}
    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
     for part in mw.text.gsplit(input, "/", true) do
         local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
         local name = mw.text.trim(part)
        if t and t.exists then
        if name ~= "" then
             table.insert(found, "-" .. i)
            local title = mw.title.new(name)
            local displayText = name
            -- Если страница с таким названием существует, выводит её в виде ссылки
            if title and title.exists then
                displayText = "[[" .. name .. "]]"
            end
            -- Формирования строки: выводит обработанный текст и добавляет категорию с таким именем
             table.insert(results, displayText .. "\n[[Category:" .. name .. "]]")
         end
         end
     end
     end


     return table.concat(results, "\n")
     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
end


return p
return p