Mô đun:Random portal component

-- Mô đun này thực hiện [[Bản mẫu:Random portal component]]

local p = {}

local mRandom = require('Mô đun: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 = 'Bản mẫu: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(
			'<strong class="error">Trang "[[%s]]" không tồn tại.</strong>',
			title
		)
		if mw.title.getCurrentTitle().namespace == 100 then -- is in the portal namespace
			msg = msg .. '[[Thể loại:Cổng thông tin cần chú ý]]'
		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] = '<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'), args.headertemplate)
	ret[#ret + 1] = getRandomSubpageContent(frame, pages)
	ret[#ret + 1] = getFooter(frame, pages, string.format(
		'[[/Đề cử/%s|Đề nghị]] • [[%s|%s]] ',
		expandArg(args, 'subpage'),
		pages.subpage,
		args.footer or 'Archive'
	))

	return table.concat(ret, '\n')
end

local function makeInvokeFunction(func)
	return function (frame)
		local args = require('Mô đun:Arguments').getArgs(frame, {
			trim = false,
			removeBlanks = false,
			wrappers = {
				'Bản mẫu:Random portal component',
				'Bản mẫu:Random portal component with nominate'
			}
		})
		return func(args, frame)
	end
end

p.main = makeInvokeFunction(p._main)
p.nominate = makeInvokeFunction(p._nominate)

return p
Chúng tôi bán
Bài viết liên quan
Những con quỷ không thể bị đánh bại trong Kimetsu no Yaiba
Những con quỷ không thể bị đánh bại trong Kimetsu no Yaiba
Nếu Akaza không nhớ lại được quá khứ nhờ Tanjiro, anh sẽ không muốn tự sát và sẽ tiếp tục chiến đấu
Chân dung Drew Gilpin Faust - Hiệu trưởng Đại học Harvard
Chân dung Drew Gilpin Faust - Hiệu trưởng Đại học Harvard
Đó là những lời khẳng định đanh thép, chắc chắn và đầy quyền lực của người phụ nữ đang gánh trên vai ngôi trường đại học hàng đầu thế giới
Hướng dẫn build Albedo - Genshin Impact
Hướng dẫn build Albedo - Genshin Impact
Làm SP DPS ngon, build Dmg theo Hoa Khoảnh Khắc (DEF) không cần vũ khí 5 sao mới mạnh
Những nhân vật Black Myth sẽ khai thác tiếp sau Wukong
Những nhân vật Black Myth sẽ khai thác tiếp sau Wukong
Sau Wukong, series Black Myth sẽ khai thác tiếp Thiện Nữ U Hồn, Phong Thần Bảng, Khu Ma Đế Chân Nhân, Sơn Hải Kinh, Liêu Trai Chí Dị…