Jump to content

Module:Random slideshow

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Evad37 (talk | contribs) at 09:39, 3 June 2018 (New module to create a randomised slideshow.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- Creates a slideshow gallery where the order is randomised. Intended for use on portal pages.

local randomModule = require('Module:Random')

function cleanupArgs(argsTable)
	local cleanArgs = {}
	for key, val in pairs(argsTable) do
		if type(val) == 'string' then
			val = val:match('^%s*(.-)%s*$')
			if val ~= '' then
				cleanArgs[key] = val
			end
		else
			cleanArgs[key] = val
		end
	end
	return cleanArgs
end

function isAffirmed(val)
	if not(val) then return false end
	local affirmedWords = ' add added affirm affirmed include included on true yes y '
	return string.find(affirmedWords, ' '..string.lower(val)..' ', 1, true ) and true or false
end

function makeOutput(galleryLines)
	local randomiseArgs = {	['t'] = galleryLines }
	local randomisedLines = randomModule.main('array', galleryLines)
	local galleryTagContent = table.concat(randomisedLines, '\n')
	local galleryTag = mw.html.create('gallery')
	galleryTag
		:attr('mode', 'slideshow')
		:wikitext(tagContent)
	return tostring(galleryTag)
end

function makeGalleryLine(file, caption)
	local title = mw.title.new(args[i], "File" )
	return title.prefixedText .. '{{!}}' .. (caption or '')
end

function makeGalleryLinesTable(args)
	local galleryLinesTable = {}
	local i = 1
	while args[i] do
		table.insert( makeGalleryLine(args[i], args[i+1]) )
		i = i + 2
	end
	return galleryLinesTable 
end

p.main = function(frame)
	local parent = frame.getParent(frame)
	local parentArgs = parent.args
	local args = cleanupArgs(parentArgs)
	local lines = makeGalleryLinesTable(args)
	return makeOutput(lines)
end

return p