మాడ్యూల్:Random portal component
Jump to navigation
Jump to search
-- 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