Module:CGTBox

De USAC-CGT
Aller à la navigation Aller à la recherche

La documentation pour ce module peut être créée à Module:CGTBox/doc

local p = {} --p stands for package

--
-- Retourne un tableau contenant tous les paramètres commençant par startName
--
local function extractParamWithKeyStartBy(params, startName)
	local ret = {}
	local paramSelected = {}
	local lenStartName = string.len(startName)
	--mw.log("startName=", startName)
	--mw.logObject(params)
	
	for k, v in pairs(params) do
		if string.sub(k, 1, lenStartName) == startName and v ~= '' then -- empty parameters are ignored
			--On garde le paramètre
			paramSelected[#paramSelected + 1] = {
				-- On garde ce qu'il y a derrière la chaine commune
				forSorting = string.sub(k, lenStartName+1, string.len(k)),
				val = v
			}
		end
	end
	
	--mw.logObject(paramSelected)
	
	--On ordonne le tout car pairs éxécute dans un ordre indéfini
	table.sort(paramSelected, function(a, b)
		return a.forSorting < b.forSorting
	end)
	
	-- On prend les valeurs ordonnées
	for k, v in ipairs(paramSelected) do
		--mw.logObject(v)
		ret[#ret + 1] = v.val
	end

	return ret
end

--
-- Ajoute un sous titre
--
local function addsubSection(title)
	return mw.html.create('div')
		:addClass('subSectionTitle')
		:wikitext(title)
		:done()
end

--
-- Ajoute une liste
--
local function addList(datas)
	local h = mw.html.create('ul')
	
	for k, v in ipairs(datas) do
		h:node(mw.html.create('li'):wikitext(v):done())
	end
	
	return h:allDone()
end

--
-- Retourne le titre sous forme html
--
local function addTitle(title)
	return mw.html.create('div')
		:addClass('titre')
		:wikitext(title)
		:done()
end

--
-- Génère la boite à partir du titre (title) et de la table contents (contient une suite d'élément html)
--
local function returnBox(title, contents)
	--box
	local box = mw.html.create('div')
	  :addClass('CGTBox')
	
	--Title
	box:node(addTitle(title))

	--contents
	for i, v in ipairs(contents) do
		box:node(v)
	end
	
	box:done()
	
	return box
end	  

--
-- Créer une box générique (un titre (title) et du contenu (contents))
--
function p.boxGeneric( frame )
	local params = frame:getParent().args or frame.args
	
	-- Titre
	local title = params.title or 'Titre'
	local contents = {} -- table for html element for the contents of the box

	contents[#contents + 1] = mw.html.create('span')
	  :wikitext(params.contents or 'Contenu')
	  :done()

	
    return returnBox(title, contents)
end

function p.boxTest( frame )
	local params = frame:getParent().args or frame.args
	
	-- Titre
	local title = params.title or 'Titre'
	local contents = {} -- table for html element for the contents of the box

	contents[#contents + 1] = addsubSection('Références Législatives')
	
	local datas = {
		'[https://www.legifrance.gouv.fr/jorf/id/JORFTEXT000047749211 Le Décret n° 2023-519 du 28 juin 2023]',
		'[https://www.legifrance.gouv.fr/jorf/id/JORFTEXT000047749211 Le Décret n° 2023-519 du 28 juin 2023]',
		'[https://www.legifrance.gouv.fr/jorf/id/JORFTEXT000047749211 Le Décret n° 2023-519 du 28 juin 2023]',
		'[https://www.legifrance.gouv.fr/jorf/id/JORFTEXT000047749211 Le Décret n° 2023-519 du 28 juin 2023]'
	}
	contents[#contents + 1] = addList(datas)
	
    return returnBox(title, contents)
end

return p