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 getArgs = require('Module:Arguments').getArgs
local randomTools = require('Module:Random')
local currentTitle = mw.title.getCurrentTitle()
local function isFrame(obj)
if type(obj) == 'table' and type(obj.getParent) == 'function' then
return true
else
return false
end
end
local function getRandom(max)
-- gets a random integer between 1 and max; max defaults to 1
max = max or 1
return randomTools.number{max or 1}
end
function p.main(frame)
local args = getArgs(frame, {trim = false, removeBlanks = false})
frame = isFrame(frame) and 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 = getRandom(args.max)
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
function p.nominate(frame)
local args = getArgs(frame, {trim = false, removeBlanks = false})
frame = isFrame(frame) and 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 = getRandom(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
return p