मोड्युल:Random portal component
स्वरूप
मोड्युल कागजात[हेर्नुहोस्] [सम्पादन गर्नुहोस्] [इतिहास] [ताजा गर्नुहोस्]
This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
This module implements {{random portal component}}. Please see the template page for documentation.
-- This module implements [[Template:Random portal component]]
local p = {}
local mRandom = require('Module:Random')
local numeral_converter = require('Module:Numeral converter')
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 .. '/' .. numeral_converter.convert( "bn", getRandomNumber(args.max))
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(
'<strong class="error">त्रुटी: [[%s]]- उपलब्ध छैन ।</strong>',
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)
return tryExpandTemplate(
frame,
pages.root .. '/बक्स-हेडर',
{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.root .. '/बक्स-फुटर',
{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 'उपपृष्ठ')
ret[#ret + 1] = getRandomSubpageContent(frame, pages)
if not args.footer or not args.footer:find('%S') then
ret[#ret + 1] = '<div style="clear:both;"></div></div>'
else
ret[#ret + 1] = getFooter(frame, pages, string.format(
'[[%s|%s]]',
pages.subpage,
expandArg(args, 'footer')
))
end
return table.concat(ret, '\n')
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'))
ret[#ret + 1] = getRandomSubpageContent(frame, pages)
ret[#ret + 1] = getFooter(frame, pages, string.format(
'[[/नामाङ्कन/%s|सल्लाह]] • [[%s|%s]] ',
expandArg(args, 'subpage'),
pages.subpage,
args.footer or 'सङ्ग्रह'
))
return table.concat(ret, '\n')
end
local function makeInvokeFunction(func)
return function (frame)
local args = require('Module:Arguments').getArgs(frame, {
trim = false,
removeBlanks = false,
wrappers = {
'Template:अनियमित पोर्टल भाग',
'Template:अनियमित पोर्टल भाग नामाङ्कन'
}
})
return func(args, frame)
end
end
p.main = makeInvokeFunction(p._main)
p.nominate = makeInvokeFunction(p._nominate)
return p