Module:SimpleGallery

From Fishing Planet Wiki
Jump to navigation Jump to search

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

local p = {}
function p._trim(val)
    return val:match("^%s*(.-)%s*$")
end

function p.gallery(f)
    local args = f:getParent().args
    local width, height = args[1], args[2]

    local pages = {}
    for page in string.gmatch(args[3], '([^,]+)') do
        table.insert(pages, p._trim(page))
    end

    local rows = {}
    for _, page in ipairs(pages) do
        table.insert(rows, 'File:' .. page .. '.png|' .. page .. '|alt=' .. page .. '|link=' .. page);
    end

    local html = '<gallery widths="' .. width .. '" heights="' .. height .. '" mode="packed">\n' .. table.concat(rows, '\n') .. '\n</gallery>'
    return f:preprocess(html)
end

function p.fishList(f)
    local args = f.args
    local width, height = args['width'], args['height']

    local rows = {}
    for key, value in spairs(f:getParent().args) do
        local types = {}
        for type in string.gmatch(value, '([^,]+)') do
            table.insert(types, p._prepareType(p._trim(type), args));
        end

        local name = p._trim(key)
        table.insert(rows, 'Image:' .. name .. '.png|{{Lt|' .. name .. '}}<br/>' .. table.concat(types, ' ') .. '|alt=' .. name .. '|link={{TNTN|' .. name .. '}}');
    end

    local html = '<gallery class="fish-list" widths="' .. width .. '" heights="' .. height .. '" mode="packed">\n' .. table.concat(rows, '\n') .. '\n</gallery>'
    return f:preprocess(html)
end

function spairs(t)
    -- collect the keys
    local keys = {}
    for k in pairs(t) do
        if k ~= 'noshift' and k ~= 'template' and k ~= 'uselang' then
            keys[#keys + 1] = k
        end
    end

    -- sort by name
    table.sort(keys) -- function(a, b) return a:upper() < b:upper() end

    -- return the iterator function
    local i = 0
    return function()
        i = i + 1
        if keys[i] then
            return keys[i], t[keys[i]]
        end
    end
end

function p._prepareType(type, tArgs)
    if type == 'young' then return tArgs['young']
    elseif type == 'trophy' then return tArgs['trophy']
    elseif type == 'unique' then return tArgs['unique']
    else return tArgs['common'] end
end

return p