Module:Random portal component

From 'City of Adelaide' History and Genealogy Site
Jump to navigation Jump to search

-- 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 = '

'

local footerArg = args.footer or '{{{footer}}}' local boxFooterArg = '' .. footerArg .. ''

-- Assemble the text snippets together. local headerPreprocessed = frame:preprocess(mw.ustring.format('Template:%s', boxHeader, header, componentSubpage)) local componentPreprocessed = frame:preprocess('Template:' .. componentSubpage .. '') local footerPreprocessed if not args.footer or not mw.ustring.find(args.footer, '%S') then footerPreprocessed = footerClosingDiv else footerPreprocessed = frame:preprocess('Module:Random portal component/box-footer') end return headerPreprocessed .. '\n' .. componentPreprocessed .. '\n' .. footerPreprocessed end

return p