Kirby Wiki
m (Whoops)
m (Trim input parameter)
 
(5 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
local p = {}
 
local p = {}
   
local REGEX = '%s-%[+[fF]ile:([^|%]]*)[^%]]-%]+%s-'
+
local REGEX = '%s-%[+[^:]+:([^|%]]*)[^%]]-%]+%s-'
  +
local URL_REGEX = '^https?://(.*)'
   
 
local function replacer(captures)
 
local function replacer(captures)
Line 39: Line 40:
 
function p.imagenum(frame)
 
function p.imagenum(frame)
 
local file = mw.text.trim(frame.args[1] or '')
 
local file = mw.text.trim(frame.args[1] or '')
p.num = frame.args[2] or 1
+
p.num = tonumber(frame.args[2] or '1')
local newfile, _ = file:gsub(REGEX, replacer2)
+
local newfile, count = file:gsub(REGEX, replacer2)
  +
if p.num > 1 and count == 0 then
  +
return ''
  +
end
 
return newfile
 
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
 
end
   

Latest revision as of 13:46, 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 = mw.text.trim(image):gsub(URL_REGEX, 'https://%1')
        if count == 0 then
            return ''
        else
            return result
        end
    else
        return ''
    end
end

return p