Module:GalleryPreview

--| Module:GalleryPreview (v1.0.0) --- Creates a preview of images from a gallery page. --by author: "The JoTS"

--

--% 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         local defaultTxt = args._default     -- Text if link is a redlink local numOfImgs = assert(tonumber(args.number_of_images),            'Argument "number_of_images" is not an integer') gallerypg = mw.title.new(gallerypg) if gallerypg.exists then local contents = gallerypg:getContent local galleries = contents:gmatch(               "<%s*[Gg]allery.->(.-)") 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 -- 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  exists yet! You can create it [" .. gallerypg:fullUrl("action=edit") .. " here].")) end end }