Module:Random portal component

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

'

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

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('Suggest', subpageArg) local archiveDisplay = args.footer or 'Archive' local archiveLink = mw.ustring.format('%s', subpageArg, archiveDisplay)

-- Assemble the text snippets together. local headerPreprocessed = frame:preprocess(mw.ustring.format('Module:Random portal component/box-header', header, componentSubpage)) local componentPreprocessed = frame:preprocess('Template:' .. componentSubpageNoRoot .. '') local footerPreprocessed = frame:preprocess(mw.ustring.format('Module:Random portal component/box-footer', nominateLink, archiveLink)) return headerPreprocessed .. '\n' .. componentPreprocessed .. '\n' .. footerPreprocessed end

return p