Модуль:Банэр Lua

Зьвесткі зь Вікіпэдыі — вольнай энцыкляпэдыі

Дакумэнтацыю да гэтага модуля можна стварыць у Модуль:Банэр Lua/Дакумэнтацыя

-- Гэты модуль выкарыстоўваецца шаблёнам {{На Lua}}.
local yesno = require('Модуль:ТакНе')
local mList = require('Модуль:Сьпіс')
local mTableTools = require('Модуль:TableTools')
local mMessageBox = require('Модуль:Вакно паведамленьняў')

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)
			local maybeSandbox = mw.title.new(module .. '/пясочніца')
			if maybeSandbox.exists then
				moduleLinks[i] = moduleLinks[i] .. string.format(' ([[:%s|пясочніца]])', maybeSandbox.fullText)
			end
		end
		local moduleList = mList.makeList('bulleted', moduleLinks)
		local title = mw.title.getCurrentTitle()
		if title.subpageText == "doc" then
			title = title.basePageTitle
		end
		if title.contentModel == "Scribunto" then
			boxArgs.text = 'Гэты модуль залежыць ад іншых такіх модуляў:' .. moduleList
		else
			boxArgs.text = 'Гэты шаблён выкарыстоўвае [[Вікіпэдыя:Lua|Lua]]:\n' .. moduleList
		end
	end
	boxArgs.type = 'notice'
	boxArgs.small = true
	boxArgs.image = '[[Файл:Lua-Logo.svg|30пкс|alt=|link=]]'
	return mMessageBox.main('mbox', boxArgs)
end

function p.renderTrackingCategories(args, modules, titleObj)
	if yesno(args.nocat) then
		return ''
	end
	
	local cats = {}
	
	-- Катэгорыя памылак
	if #modules < 1 then
		cats[#cats + 1] = 'Вікіпэдыя:Шаблёны Lua з памылкамі'
	end
	
	-- Катэгорыя шаблёнаў Lua
	titleObj = titleObj or mw.title.getCurrentTitle()
	local subpageBlacklist = {
		doc = true,
		sandbox = true,
		sandbox2 = true,
		testcases = true
	}
	if not subpageBlacklist[titleObj.subpageText] then
		local protCatName
	if titleObj.namespace == 10 
		and not subpageBlacklist[titleObj.subpageText]
	then
		local category = args.category
			if not category then
				local categories = {
					['Модуль:String'] = 'Вікіпэдыя:Шаблёны:На базе модулю String',
					['Модуль:Матэматычны'] = 'Вікіпэдыя:Шаблёны:На базе модулю Матэматычны',
					['Модуль:ПераводСыстэмыЗьлічэньня'] = 'Вікіпэдыя:Шаблёны:На базе модулю ПераводСыстэмыЗьлічэньня',
					['Модуль:Цытата'] = 'Вікіпэдыя:Шаблёны:На базе модулю Цытата'
				}
				categories['Модуль:Цытата/Цыт1'] = categories['Модуль:Цытата']
				category = modules[1] and categories[modules[1]]
				category = category or 'Вікіпэдыя:Шаблёны на Lua'
		end
		cats[#cats + 1] = category
		local protLevels = {
			autoconfirmed = 1,
			extendedconfirmed = 2,
			templateeditor = 3,
			sysop = 4
		}
			protCatName = "Вікіпэдыя:Шаблёны:На базе абароненых модуляў Lua"
		elseif titleObj.namespace == 828 then
			protCatName = "Вікіпэдыя:Модулі:На базе слабаабароненых модуляў Lua"
		end
		if not args.noprotcat and protCatName then
			local protLevels = {
				autoconfirmed = 1,
				extendedconfirmed = 2,
				templateeditor = 3,
				sysop = 4
			}
			local currentProt
			if titleObj.id ~= 0 then
				-- id = 0 (старонка не існуе) калі прагляд да стварэньня шаблёну.
				currentProt = titleObj.protectionLevels["edit"][1]
			end
			if currentProt == nil then currentProt = 0 else currentProt = protLevels[currentProt] end
			for i, module in ipairs(modules) do
				if module ~= "WP:libraryUtil" then
					local moduleProt = mw.title.new(module).protectionLevels["edit"][1]
					if moduleProt == nil then moduleProt = 0 else moduleProt = protLevels[moduleProt] end
					if moduleProt < currentProt then
						cats[#cats + 1] = protCatName
						break
					end
				end
			end
		end
	end
	for i, cat in ipairs(cats) do
		cats[i] = string.format('[[Катэгорыя:%s]]', cat)
	end
	return table.concat(cats)
end

return p