Jump to content

Module:Random portal component/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 23:46, 3 December 2014 (make functions to access this from Lua). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- This module implements [[Template:Random portal component]]

local p = {}

local mRandom = require('Module:Random')
local currentTitle = mw.title.getCurrentTitle()

local function getRandomNumber(max)
	-- gets a random integer between 1 and max; max defaults to 1
	return mRandom.number{max or 1}
end

function p._main(args, frame)
	frame = frame or mw.getCurrentFrame()

	-- Gather together all the text snippets used in the template.
	local rootpage = args.rootpage or currentTitle.prefixedText
	local boxHeader = rootpage .. '/box-header'
	local header = args.header or 'subpage'
	local rand = getRandomNumber(args.max)
	local subpage = rootpage .. '/' .. (args.subpage or '{{{subpage}}}')
	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

function p._nominate(args, frame)
	frame = frame or mw.getCurrentFrame()
	
	-- Gather together the text snippets used in the template.
	local header = args.header or '{{{header}}}'
	local rootpage = currentTitle.prefixedText
	local subpageArg = args.subpage or '{{{subpage}}}'
	local subpage = rootpage .. '/' .. subpageArg
	local rand = getRandomNumber(args.max)
	rand = tostring(rand)
	local componentSubpage = subpage .. '/' .. rand
	local subpageNoRoot = '/' .. subpageArg
	local componentSubpageNoRoot = subpageNoRoot .. '/' .. rand
	local nominateLink = mw.ustring.format('[[/Nominate/%s|Suggest]]', subpageArg)
	local archiveDisplay = args.footer or 'Archive'
	local archiveLink = mw.ustring.format('[[/%s|%s]]', subpageArg, archiveDisplay)
	
	-- Assemble the text snippets together.
	local headerPreprocessed = frame:preprocess(mw.ustring.format('{{/box-header |%s|%s }}', header, componentSubpage))
	local componentPreprocessed = frame:preprocess('{{' .. componentSubpageNoRoot .. '}}')
	local footerPreprocessed = frame:preprocess(mw.ustring.format('{{/box-footer|%s • %s }}', nominateLink, archiveLink))
	return headerPreprocessed .. '\n' .. componentPreprocessed .. '\n' .. footerPreprocessed
end

local function makeInvokeFunction(func)
	return function (frame)
		local args = require('Module:Arguments').getArgs(frame, {
			trim = false,
			removeBlanks = false,
			wrappers = 'Template:Random portal component'
		})
		return func(args, frame)
	end
end

p.main = makeInvokeFunction(p._main)
p.nominate = makeInvokeFunction(p._nominate)

return p