Модуль:Случайная картинка
Материал из Space Station 14 Вики
Для документации этого модуля может быть создана страница Модуль:Случайная картинка/doc
local p = {}
local getArgs = require('Module:Arguments').getArgs
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 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 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
return p