-- 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
local function makeExpandArgFunc(args)
-- Make a function to emulate how unspecified template parameters appear in
-- wikitext. If the specified argument exists, its value is returned, and
-- if not the argument name is returned inside triple curly braces.
return function (key)
local val = args[key]
if val then
return val
else
return string.format('{{{%s}}}', key)
end
end
end
function p._main(args, frame)
frame = frame or mw.getCurrentFrame()
local expandArg = makeExpandArgFunc(args)
-- Get page names
local rootpage = args.rootpage or currentTitle.prefixedText
local subpage = rootpage .. '/' .. expandArg('subpage')
local randomSubpage = subpage .. '/' .. getRandomNumber(args.max)
local ret = {}
-- Header
do
local header = args.header or 'subpage'
ret[#ret + 1] = frame:expandTemplate{
title = rootpage .. '/box-header',
args = {header, randomSubpage}
}
end
-- Random subpage content
ret[#ret + 1] = frame:expandTemplate{title = randomSubpage}
-- Footer
if not args.footer or not args.footer:find('%S') then
ret[#ret + 1] = '<div style="clear:both;"></div></div>'
else
local footerArg = string.format(
'[[%s|%s]]',
subpage,
expandArg('footer')
)
ret[#ret + 1] = frame:expandTemplate{
title = rootpage .. '/box-footer',
args = {footerArg}
}
end
return table.concat(ret, '\n')
end
function p._nominate(args, frame)
frame = frame or mw.getCurrentFrame()
local expandArg = makeExpandArgFunc(args)
-- Get page names
local rootpage = currentTitle.prefixedText
local subpage = rootpage .. '/' .. expandArg('subpage')
local randomSubpage = subpage .. '/' .. getRandomNumber(args.max)
local ret = {}
-- Header
ret[#ret + 1] = frame:expandTemplate{
title = rootpage .. '/box-header',
args = {expandArg('header'), randomSubpage}
}
-- Random subpage content
ret[#ret + 1] = frame:expandTemplate{title = randomSubpage}
-- Footer
do
local footerArg = string.format(
'[[/Nominate/%s|Suggest]] • [[%s|%s]] ',
expandArg('subpage'),
subpage,
args.footer or 'Archive'
)
ret[#ret + 1] = frame:expandTemplate{
title = rootpage .. '/box-footer',
args = {footerArg}
}
end
return table.concat(ret, '\n')
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