Module:Random portal component
Appearance
![]() | This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module depends on the following other modules: |
This module implements {{random portal component}}. Please see the template page for documentation.
See also
-- This module implements [[Template:Random portal component]]
local p = {}
local getArgs = require('Module:Arguments').getArgs
local randomTools = require('Module:Random')
function p.main(frame)
local args = getArgs(frame, {trim = false, removeBlanks = false})
frame = frame or mw.getCurrentFrame()
-- 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