模組:Lua banner

文出維基大典

本模塊用於執行{{lua}}模板。

通過wikitext使用[]

本模塊不能直接被wikitext的使用,只能通過{{lua}}模板調用。請參閱模板說明書。

通過Lua模塊使用[]

為通過其他的Lua模塊使用本模塊,先要載入本模塊。

<source lang =“lua”> 當地mLuaBanner =要求('模塊:Lua的旗幟') </源>

然後可以通過主程序功能生成邊框。

<source lang =“lua”> mLuaBanner._main(參數) </源> <! - 變量 args 需要作為一個包含參數的表格以通過模塊為參數,可以指定不同的參數以及它們如何影響模塊輸出,請參考{{lua}}模板文檔。 - >

</ includeonly>

--本模組嵌入模板{{lua}}
local yesno = require('Module:Yesno')
local mList = require('Module:List')
local mTableTools = require('Module:TableTools')
local mMessageBox = require('Module:Message box')
 
local p = {}
 
function p.main(frame)
	local origArgs = frame:getParent().args
	local args = {}
	for k, v in pairs(origArgs) do
		v = v:match('^%s*(.-)%s*$')
		if v ~= '' then
			args[k] = v
		end
	end
	return p._main(args)
end
 
function p._main(args)
	local modules = mTableTools.compressSparseArray(args)
	local box = p.renderBox(modules)
	local trackingCategories = p.renderTrackingCategories(args, modules)
	return box .. trackingCategories
end
 
function p.renderBox(modules)
	local boxArgs = {}
	if #modules < 1 then
		boxArgs.text = '<strong class="error">錯誤:未指定模組</strong>'
	else
		local moduleLinks = {}
		for i, module in ipairs(modules) do
			moduleLinks[i] = string.format('[[:%s]]', module)
		end
		local moduleList = mList.makeList('bulleted', moduleLinks)
		boxArgs.text = '使用[[Wikipedia:Lua|Lua語言編寫]]:\n' .. moduleList
	end
	boxArgs.type = 'notice'
	boxArgs.small = true
	boxArgs.image = '[[File:Lua-logo-nolabel.svg|30px|alt=Lua 标志|link=Wikipedia:Lua]]'
	return mMessageBox.main('mbox', boxArgs)
end
 
function p.renderTrackingCategories(args, modules, titleObj)
	if yesno(args.nocat) then
		return ''
	end
 
	local cats = {}
 
	-- Error category
	if #modules < 1 then
		cats[#cats + 1] = '模板未指定Lua模組'
	end
 
	-- Lua templates category
	titleObj = titleObj or mw.title.getCurrentTitle()
	local subpageBlacklist = {
		doc = true,
		sandbox = true,
		sandbox2 = true,
		testcases = true
	}
	if titleObj.namespace == 10 
		and not subpageBlacklist[titleObj.subpageText]
	then
		local category = args.category
		if not category then
			local categories = {
				['Module:String'] = '模板而基於Lua字符串者',
				['Module:Math'] = '模板而基於Lua數學模組者',
				['Module:BaseConvert'] = '模板而基于Lua模組BaseConvert者',
				['Module:Citation'] = '引用模板而基于Lua者'
			}
			categories['Module:Citation/CS1'] = categories['Module:Citation']
			category = modules[1] and categories[modules[1]]
			category = category or '模板而基於Lua語者'
		end
		cats[#cats + 1] = category
	end
 
	for i, cat in ipairs(cats) do
		cats[i] = string.format('[[分類:%s]]', cat)
	end
	return table.concat(cats)
end
 
return p