Kirby Wiki
m (Test)
m (Return nothing when no replacements are found)
Line 5: Line 5:
   
 
local REGEX = '%s-%[+[^:]+:([^|%]]*)[^%]]-%]+%s-'
 
local REGEX = '%s-%[+[^:]+:([^|%]]*)[^%]]-%]+%s-'
local URL_REGEX = 'https?://(.*)'
+
local URL_REGEX = '^https?://(.*)'
   
 
local function replacer(captures)
 
local function replacer(captures)
Line 54: Line 54:
 
local result, count = image:gsub(URL_REGEX, 'https://%1')
 
local result, count = image:gsub(URL_REGEX, 'https://%1')
 
if count == 0 then
 
if count == 0 then
return image
+
return ''
 
else
 
else
 
return result
 
return result

Revision as of 13:44, 16 June 2018

Documentation for this module may be created at Module:Filestrip/doc

-- <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 = image:gsub(URL_REGEX, 'https://%1')
        if count == 0 then
            return ''
        else
            return result
        end
    else
        return ''
    end
end

return p