FANDOM


-- <nowiki>
-- Lua module that changes file links in infoboxes
-- to proper infobox gallery items
local p = {}
 
local REGEX = '%s-%[+[^:]+:([^|%]]*)[^%]]-%]+%s-'
local URL_REGEX = '^https?://(.*)'
 
local function replacer(captures)
    local s = ''
    if p.count > 0 then
        s = ' (' .. p.count .. ')\n'
    end
    s = s .. captures .. '|' .. p.caption
    p.count = p.count + 1
    return s
end
 
local function replacer2(capture)
    p.num = p.num - 1
    if p.num ~= 0 then
        return ''
    else
        return capture
    end
end
 
function p.strip(frame)
    local file = mw.text.trim(frame.args[1] or '')
    p.caption = frame.args[2] or ''
    p.count = 0
    local newfile = file:gsub(REGEX, replacer)
    if p.count > 1 then
        -- add last number
        newfile = newfile .. ' (' .. p.count .. ')'
    end
    return newfile
end
 
function p.imagenum(frame)
    local file = mw.text.trim(frame.args[1] or '')
    p.num = tonumber(frame.args[2] or '1')
    local newfile, count = file:gsub(REGEX, replacer2)
    if p.num > 1 and count == 0 then
        return ''
    end
    return newfile
end
 
function p.urlimage(frame)
    local image = frame.args[1]
    if image then
        -- Make image HTTPS
        local result, count = mw.text.trim(image):gsub(URL_REGEX, 'https://%1')
        if count == 0 then
            return ''
        else
            return result
        end
    else
        return ''
    end
end
 
return p
Community content is available under CC-BY-SA unless otherwise noted.