« Module:CGTBox » : différence entre les versions

De USAC-CGT
Aller à la navigation Aller à la recherche
([CGTBox] Ajout d'une fonction pour extraire l'ensemble des paramètres dont le nom commence par une chaine de caractère. (pour le passage de tableau...))
Aucun résumé des modifications
 
(6 versions intermédiaires par le même utilisateur non affichées)
Ligne 36 : Ligne 36 :


return ret
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
end


Ligne 70 : Ligne 93 :


--
--
-- Créer une box générique (un titre (title) et du contenu (contents))
-- Crée une box générique (un titre (title) et du contenu (contents))
--
--
function p.boxGeneric( frame )
function p.boxGeneric( frame )
Ligne 83 : Ligne 106 :
  :done()
  :done()


    return returnBox(title, contents)
end
--
-- Crée une box MAJ, c'est à dire contenant les références législatives et la date de dernière modif
--
function p.boxRefLegis( frame )
local params = frame:getParent().args or frame.args
local contents = {} -- table for html element for the contents of the box
-- Titre
local title = params.title or mw.title.getCurrentTitle().text
-- description
if (params.desc ~= nil and params.desc ~= '') then
contents[#contents + 1] = mw.html.create('div')
:wikitext(params.desc)
:done()
end
-- Références législatives
contents[#contents + 1] = addsubSection('Références Législatives')
local refs = extractParamWithKeyStartBy(params, 'ref')
if next( refs ) == nil then
refs = {'à compléter'}
end
contents[#contents + 1] = addList(refs)
--Dernières vérifs
contents[#contents + 1] = addsubSection('Dernière Vérification le')
if (params.verif == nil or params.verif == '') then
params.verif = "''jamais vérifié''"
end
contents[#contents + 1] = mw.html.create('div')
:addClass('center')
:wikitext(params.verif)
:done()
     return returnBox(title, contents)
     return returnBox(title, contents)

Dernière version du 23 septembre 2023 à 14:26

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ée 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

--
-- Crée une box MAJ, c'est à dire contenant les références législatives et la date de dernière modif
--
function p.boxRefLegis( frame )
	local params = frame:getParent().args or frame.args
	local contents = {} -- table for html element for the contents of the box
	
	-- Titre
	local title = params.title or mw.title.getCurrentTitle().text
	
	-- description
	if (params.desc ~= nil and params.desc ~= '') then
		contents[#contents + 1] = mw.html.create('div')
			:wikitext(params.desc)
			:done()	
	end

	-- Références législatives
	contents[#contents + 1] = addsubSection('Références Législatives')
	
	local refs = extractParamWithKeyStartBy(params, 'ref')
	if next( refs ) == nil then
		 refs = {'à compléter'}
	end
	contents[#contents + 1] = addList(refs)
	
	--Dernières vérifs
	contents[#contents + 1] = addsubSection('Dernière Vérification le')
	
	if (params.verif == nil or params.verif == '') then
		params.verif = "''jamais vérifié''"	
	end
	contents[#contents + 1] = mw.html.create('div')
		:addClass('center')
		:wikitext(params.verif)
		:done()
	
    return returnBox(title, contents)
end

return p