« Module:Lua banner » : différence entre les versions

De Adadov.net wiki
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 7 : Ligne 7 :
local p = {}
local p = {}


function p.main(args)
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 = args
local modules = args
local box = p.renderBox(modules)
local box = p.renderBox(modules)

Version du 4 février 2015 à 22:22

Documentation icon Documentation Module[créer]
local mMessageBox = require('Module:Message box')
local yesno = require('Module:Yesno')
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType


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 = 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 style="color:red">ERREUR : Aucun module spécifié</strong>]]
	else
		local moduleLinks = {}
		for i, module in ipairs(modules) do
			moduleLinks[i] = string.format('[[:%s]]', module)
		end
	end
	
	boxArgs.type = 'notice'
	boxArgs.small = true
	boxArgs.image = '[[File:Lua-logo-nolabel.svg|30px|alt=Lua logo|link=Wikipedia:Lua]]'
	return mMessageBox.main('mbox', boxArgs)
end

function p.renderTrackingCategories(args, modules, titleObj)
	if yesno(args.nocat) then
		return ''
	end

	local cats = {}

	-- Catégorie pour les erreurs
	if #modules < 1 then
		cats[#cats + 1] = "Modèle avec erreur Lua"
	end

	local subpageBlacklist = {
		doc = true,
		sandbox = true,
		sandbox2 = true,
		testcases = true
	}

	-- Catégorie de modèles Lua
	titleObj = titleObj or mw.title.getCurrentTitle()

	if titleObj.namespace == 10 and not subpageBlacklist then
		local category = args.category
		if not category then
			local categories = {}
			category = modules[1] and categories[modules[1]]
			category = category or "Modèle Lua"
		end
		cats[#cats + 1] = category
	end

	for i, cat in ipairs(cats) do
		cats[i] = string.format('[[Category:%s]]', cat)
	end
	return table.concat(cats)
end

--[[
------------------------------------------------------------------------------------
-- compressSparseArray
--
-- This takes an array with one or more nil values, and removes the nil values
-- while preserving the order, so that the array can be safely traversed with
-- ipairs.
------------------------------------------------------------------------------------
--]]
function p.compressSparseArray(t)
	checkType('compressSparseArray', 1, t, 'table')
	local ret = {}
	local nums = p.numKeys(t)
	for _, num in ipairs(nums) do
		ret[#ret + 1] = t[num]
	end
	return ret
end

return p