پودمان:Lua banner/تمرین

از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[ایجاد] [پاکسازی]
-- This module implements the {{lua}} template.

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
			-- replace "Module" namespace prefix with "پودمان"
			if mw.ustring.find(module, '[Mm]odule', 1, false) then
				module = mw.ustring.gsub(module, '[Mm]odule', 'پودمان', 1)
			end
			
			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" or title.subpageText == "توضیحات" then
			title = title.basePageTitle
		end
		if title.contentModel == "Scribunto" then
			boxArgs.text = 'این پودمان از پودمان‌های زیر استفاده می‌کند:' .. moduleList
		else
			boxArgs.text = 'این الگو از [[ویکی‌پدیا:لوآ|لوآ]] استفاده می‌کند:\n' .. moduleList
		end
	end
	boxArgs.type = 'notice'
	boxArgs.small = true
	boxArgs.image = '[[پرونده:Lua-logo-nolabel.svg|30px|جایگزین=نماد لوآ|پیوند=ویکی‌پدیا:لوآ]]'
	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] = 'الگو:لوآ همراه خطا'
	end

	-- Lua templates category
	titleObj = titleObj or mw.title.getCurrentTitle()
	local subpageBlacklist = {
		doc = true,
		sandbox = true,
		sandbox2 = true,
		testcases = true,
		['توضیحات'] = true,
		['تمرین'] = true,
		['آزمایشی'] = true,
	}
	local subPageName = subpageBlacklist[titleObj.subpageText]
	if not subPageName then
		local protCatName
		if titleObj.namespace == 10 then
			local category = args.category
			if not category then
				local categories = {
					['Module:String'] = 'الگو:مبتنی بر لوآ رشته',
					['Module:Math'] = 'الگو:مبتنی بر لوآ ریاضی',
					['Module:BaseConvert'] = 'الگو:مبتنی بر لوآ تبدیل مبنا',
					['Module:Citation'] = 'الگو:ارجاع‌دهی مبتنی بر لوآ'
				}
				categories['Module:Citation/CS1'] = categories['Module:Citation']
				category = modules[1] and categories[modules[1]]
				category = category or 'الگو:مبتنی بر لوآ'
			end	
			cats[#cats + 1] = category
			protCatName = "الگو:مبتنی بر لوآ با سطح محافظت پایین‌تر"
		elseif titleObj.namespace == 828 then
			protCatName = "پودمان‌های وابسته به پودمان‌های دیگر با سطح محافظت پایین‌تر"
		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 is 0 (page does not exist) if am previewing before creating a template.
				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