FANDOM


--| Module:GalleryPreview (v1.1.2)
--- Creates a preview of images from a gallery page.
--by author: "The JoTS"
 
-- update 12/7/16 - New param for portal image to gallery page.
--      This counts towards the number_of_images limit.
 
-- <nowiki>
 
--% Creates a metatable wrapper for checking arguments.
--@ frArgs (table) The arguments table from a frame object. (i.e. frame.args)
--: (table) "Wrapped" table. Arguments my be indexed with membership operator.
local function argChecker(frArgs)
    return setmetatable({}, {
        __index = function(t, arg)
            if arg:match('^_') then
                return frArgs[arg:gsub('^_','')]
            else
                return assert(frArgs[arg],
                    'Argument "' .. arg .. '" was not provided')
            end
        end
    })
end
 
-- Gallery object template
local gallery_src = mw.html.create("gallery")
    :attr({navigation = 'true', hideaddbutton = 'true', position = 'center'})
 
-- Module source
return {
    main = function(frame)
        local args = argChecker(frame.args)
 
        local gallerypg  = args.gallery_page  -- Link to gallery
        local settings   = args._settings     -- Settings to apply to <gallery>
        local defaultTxt = args._default      -- Text if link is a redlink
        local portalImg  = args._portal_image -- Portal image that links to page
        local numOfImgs  = assert(tonumber(args.number_of_images),
            'Argument "number_of_images" is not an integer')
            - (portalImg and 1 or 0)
 
        gallerypg = mw.title.new(gallerypg)
 
        if gallerypg.exists then
            local contents = gallerypg:getContent()
            local galleries = contents:gmatch(
                "<%s*[Gg]allery.->(.-)</%s*[Gg]allery%s*>")
 
            local images = {} -- will contain all images from all galleries
            local selected = mw.clone(gallery_src)
 
            -- Import all images
            for gallery in galleries do
                for img in mw.text.gsplit(gallery, '\n') do
                    if img:gsub('%s','') ~= '' then
                        table.insert(images, img)
                    end
                end
            end
 
            -- Set randomseed
            math.randomseed(os.time())
 
            -- Select images randomly
            for n = 1, math.min(#images, numOfImgs) do
                local i = math.random(#images)
                local img = mw.text.split(images[i], '|')[1] -- omit caption
 
                selected:wikitext(img .. '\n')
 
                table.remove(images, i)
            end
 
            -- Include portal image
            if portalImg then
                selected:wikitext(portalImg .. "|link=" .. gallerypg.text ..'\n')
            end
 
            -- Apply custom settings to gallery
            if settings then
                local settPair =
                    settings:gmatch([=[(%w+):%s*["'](.-)["'][,;]?]=])
 
                for opt,val in settPair do
                    selected:attr(opt, val)
                end
            end
 
            return frame:preprocess(tostring(selected))
        else
            return frame:preprocess(defaultTxt or
                ("No gallery for {{PAGENAME}} exists yet! You can create it ["
                .. gallerypg:fullUrl("action=edit") .. " here]."))
        end
    end
}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.