词条 | Module:Random portal component |
释义 | -- This module implements Random portal component local p = {} local mRandom = require('Module:Random') local currentTitle = mw.title.getCurrentTitle() local function getRandomNumber(max) -- gets a random integer between 1 and max; max defaults to 1 return mRandom.number{max or 1} end local function expandArg(args, key) -- Emulate how unspecified template parameters appear in wikitext. If the -- specified argument exists, its value is returned, and if not the argument -- name is returned inside triple curly braces. local val = args[key] if val then return val else return string.format('{{{%s}}}', key) end end local function getPages(args) local pages = {} pages.root = args.rootpage or currentTitle.prefixedText pages.subpage = pages.root .. '/' .. expandArg(args, 'subpage') pages.random = pages.subpage .. '/' .. getRandomNumber(args.max) pages.footer = 'Template:Box-footer' return pages end local function tryExpandTemplate(frame, title, args) local success, result = pcall(frame.expandTemplate, frame, {title = title, args = args}) if success then return result else local msg = string.format( 'The page "%s" does not exist.', title ) if mw.title.getCurrentTitle().namespace == 100 then -- is in the portal namespace msg = msg .. end return msg end end local function getHeader(frame, pages, header, template) return tryExpandTemplate( frame, template or pages.root .. '/box-header', {header, pages.random} ) end local function getRandomSubpageContent(frame, pages) return tryExpandTemplate( frame, pages.random ) end local function getFooter(frame, pages, link) return tryExpandTemplate( frame, pages.footer, {link} ) end function p._main(args, frame) frame = frame or mw.getCurrentFrame() local pages = getPages(args) local ret = {} ret[#ret + 1] = getHeader(frame, pages, args.header or 'subpage', args.headertemplate) ret[#ret + 1] = getRandomSubpageContent(frame, pages) if not args.footer or not args.footer:find('%S') then ret[#ret + 1] = ' 'else ret[#ret + 1] = getFooter(frame, pages, string.format( '%s', pages.subpage, expandArg(args, 'footer') )) end return table.concat(ret, '\') end function p._nominate(args, frame) frame = frame or mw.getCurrentFrame() local pages = getPages(args)
local ret = {} ret[#ret + 1] = getHeader(frame, pages, expandArg(args, 'header'), args.headertemplate) ret[#ret + 1] = getRandomSubpageContent(frame, pages) ret[#ret + 1] = getFooter(frame, pages, string.format( 'Suggest • %s ', expandArg(args, 'subpage'), pages.subpage, args.footer or 'Archive' )) return table.concat(ret, '\') end local function makeInvokeFunction(func) return function (frame) local args = require('Module:Arguments').getArgs(frame, { trim = false, removeBlanks = false, wrappers = { 'Template:Random portal component', 'Template:Random portal component with nominate' } }) return func(args, frame) end end p.main = makeInvokeFunction(p._main) p.nominate = makeInvokeFunction(p._nominate) return p 1 : Portals needing attention |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。