ମଡ୍ୟୁଲ:Random portal component

This module implements {{random portal component}}. Please see the template page for documentation.


-- This module implements [[Template:Random portal component]]

local p = {}

local randomTools = require('Module:Random')

function p.main(frame)
	-- If called via #invoke, use the args passed into the invoking
	-- template, or the args passed to #invoke if any exist. Otherwise
	-- assume args are being passed directly in from the debug console
	-- or from another Lua module.
	local args
	if frame == mw.getCurrentFrame() then
		args = frame:getParent().args
		for k, v in pairs(frame.args) do
			args = frame.args
			break
		end
	else
		args = frame
		frame = mw.getCurrentFrame()
	end

	-- Gather together all the text snippets used in the template.
	local currentTitle = mw.title.getCurrentTitle()
	local rootpage = args.rootpage or currentTitle.prefixedText
	local boxHeader = rootpage .. '/box-header'
	local header = args.header or 'subpage'
	local rand = randomTools.number{args.max or 1} -- gets a random integer between 1 and args.max; args.max defaults to 1
	local subpageArg = args.subpage or '{{{subpage}}}'
	local subpage = rootpage .. '/' .. subpageArg
	local componentSubpage = subpage .. '/' .. tostring(rand)
	local footerClosingDiv = '<div style="clear:both;"></div></div>'
	local footerArg = args.footer or '{{{footer}}}'
	local boxFooterArg = '[[' .. subpage .. '|' .. footerArg .. ']]'

	-- Assemble the text snippets together.
	local headerPreprocessed = frame:preprocess(mw.ustring.format('{{%s | %s | %s}}', boxHeader, header, componentSubpage))
	local componentPreprocessed = frame:preprocess('{{' .. componentSubpage .. '}}')
	local footerPreprocessed
	if not args.footer or not mw.ustring.find(args.footer, '%S') then
		footerPreprocessed = footerClosingDiv
	else
		footerPreprocessed = frame:preprocess('{{/box-footer|' .. boxFooterArg .. '}}')
	end
	return headerPreprocessed .. '\n' .. componentPreprocessed .. '\n' .. footerPreprocessed
end

return p