Модуль:Случайная картинка: различия между версиями

Материал из Space Station 14 Вики
Нет описания правки
Нет описания правки
 
Строка 4: Строка 4:
function p.main(frame)
function p.main(frame)
     local args = getArgs(frame, { removeBlanks = false })
     local args = getArgs(frame, { removeBlanks = false })
     local name = args[1] or ""
     local raw = args[1] or ""
     local attributes = args[2] or ""
     local attributes = args[2] or ""
     if name == "" then
 
     if raw == "" then
         return "<span class=\"error\">Ошибка: не указано имя файла.</span>"
         return "<span class=\"error\">Ошибка: не указано имя файла.</span>"
     end
     end
    local name, ext = raw:match("^(.-)%.([^%.%s]+)%s*$")
    if not name then
        name = raw
        ext = "png"
    end
    ext = tostring(ext):gsub("^%.", ""):lower()
     local max = tonumber(args["max"]) or 50
     local max = tonumber(args["max"]) or 50
     local include_base = (args["base"] ~= "no")
     local include_base = (args["base"] ~= "no")
Строка 15: Строка 24:


     if include_base then
     if include_base then
         local t = mw.title.new("Файл:" .. name)
         local t = mw.title.new("Файл:" .. name .. "." .. ext)
         if t and t.exists then
         if t and t.exists then
             table.insert(found, "")
             table.insert(found, "")
Строка 22: Строка 31:


     for i = 1, max do
     for i = 1, max do
         local t = mw.title.new("Файл:" .. name .. "-" .. i)
         local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
         if t and t.exists then
         if t and t.exists then
             table.insert(found, "-" .. i)
             table.insert(found, "-" .. i)
Строка 33: Строка 42:


     local before = "[[Файл:" .. name
     local before = "[[Файл:" .. name
     local after = "|" .. attributes .. "]]"
     local attr_part = (attributes ~= "" and ("|" .. attributes)) or ""
    local after = "." .. ext .. attr_part .. "]]"


     local parts = {}
     local parts = {}

Текущая версия от 05:03, 17 марта 2026

Для документации этого модуля может быть создана страница Модуль:Случайная картинка/doc

local p = {}
local getArgs = require('Module:Arguments').getArgs

function p.main(frame)
    local args = getArgs(frame, { removeBlanks = false })
    local raw = args[1] or ""
    local attributes = args[2] or ""

    if raw == "" then
        return "<span class=\"error\">Ошибка: не указано имя файла.</span>"
    end

    local name, ext = raw:match("^(.-)%.([^%.%s]+)%s*$")
    if not name then
        name = raw
        ext = "png"
    end
    ext = tostring(ext):gsub("^%.", ""):lower()

    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
        local t = mw.title.new("Файл:" .. name .. "-" .. i .. "." .. ext)
        if t and t.exists then
            table.insert(found, "-" .. i)
        end
    end

    if #found == 0 then
        return ""
    end

    local before = "[[Файл:" .. name
    local attr_part = (attributes ~= "" and ("|" .. attributes)) or ""
    local after = "." .. ext .. attr_part .. "]]"

    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

return p