Module:Portal image banner

From blackwiki
Revision as of 09:20, 2 August 2018 by test>FR30799386 (test)
Jump to navigation Jump to search

Template:Uses Lua This module implements {{Portal image banner}}; see documentation there for usage instructions. This module will not work when used directly (as {{#invoke:Portal image banner|main}}). Rather it is advisable that the main template Portal image banner is used.

See also



local p = {}
local randomModule = require('Module:Random')

p.main = function(frame)
	local parent = frame.getParent(frame)
	local parentArgs = parent.args
	local args = cleanupArgs(parentArgs)
	local output = p._main(args)
	return frame:preprocess(output)
end

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

p._main = function(args)
	if not args[1] then
		return error(linked and 'No page specified' or 'No page specified', 0)
	end
	local lines=makeGalleryLinesTable(args)
	return makeOutput(lines, args.overflow, args.height)
end

function makeGalleryLine(file, caption, link)
	local title = mw.title.new(file, "File" )
	local linktext = ( link and '{{!}}link=' .. link  or '' )
	return '[[' .. title.prefixedText ..(caption and'{{!}}'..caption or '').. linktext ..']]' .. (caption and '<div style="text-align:center;">' .. caption ..'</div>' or '') 
end

function makeGalleryLinesTable(args)
	local galleryLinesTable = {}
	local i = 1
	while args[i] do
		table.insert(galleryLinesTable, makeGalleryLine(args[i], args[i+1],args.link))
		i = i + 2
	end
	return galleryLinesTable 
end
function makeOutput(imageLines, maxHeight, overflow)
	local randomiseArgs = {	['t'] = imageLines }
	local randomisedLines = randomModule.main('array', randomiseArgs )
	local galleryContent = table.concat(randomisedLines, '\n',1,1)
	local output = '<div class="portal-banner-image" style="max-height:' .. (maxHeight or 'intial') .. '; overflow:'..(overflow or 'auto')..';">'..galleryContent..'</div>'
	return output
end

return p