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

Нет описания правки
Нет описания правки
 
Строка 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 = {}