Module:Random portal component
Appearance
| This module is rated as beta. It is considered ready for widespread use, but as it is still relatively new, it should be applied with some caution to ensure results are as expected. |
| Editing of this module by new or unregistered users is currently disabled. See the protection policy and protection log for more details. If you cannot edit this module and you wish to make a change, you can submit an edit request, discuss changes on the talk page, request unprotection, log in, or create an account. |
| 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 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