Module:CorpsTdM/Bac à sable

La documentation pour ce module peut être créée à Module:CorpsTdM/Bac à sable/Documentation

local p = {}
local szMsg = ''
local szCode = ''

-- Fonction recopiée de module:Table
-- https://fr.wikisource.org/w/index.php?title=Module:Table&oldid=10679048
function p.out_arg( arg, default )
    if arg and arg ~= '' then
        return arg
    end
    return default
end

function p.Construire( frame )

    -- Chaque ligne comprend 3 éléments par défaut
    local nNbreColonnes = 3

    --------------------------------------------------------
    -- Noter les paramètres nommés dans l'appel du modèle :
    -- 
    -- Il s'agit de tous les paramètres du module:Table
    -- (voir documentation de modèle:Table)
    --------------------------------------------------------
    local args = mw.getCurrentFrame():getParent().args

    local tArgs = {}
    if args['espace']           ~=nil then tArgs['espace'] = args['espace'] end
    if args['largeur']          ~=nil then tArgs['largeur'] = args['largeur'] end
    if args['align']            ~=nil then tArgs['align'] = args['align'] end
    if args['couleur']          ~=nil then tArgs['couleur'] = args['couleur'] end
    if args['couleur_fond']     ~=nil then tArgs['couleur_fond'] = args['couleur_fond'] end
    if args['largeurs']         ~=nil then tArgs['largeurs'] = args['largeurs'] end
    if args['aligns']           ~=nil then tArgs['aligns'] = args['aligns'] end
    if args['indentation']      ~=nil then tArgs['indentation'] = args['indentation'] end
    if args['indentation_unit'] ~=nil then tArgs['indentation_unit'] = args['indentation_unit'] end
    if args['text_align']       ~=nil then tArgs['text_align'] = args['text_align'] end
    -- Le paramètre args['nodots'] est traité plus bas dans le code
    if args['largeurp']         ~=nil then tArgs['largeurp'] = args['largeurp'] end

    -- Sert à passer les arguments à la fonction p.Table()
    local tFrame = {}
    tFrame['args'] = tArgs

    -------------------------------------------------------
    -- Noter les paramètres sans nom dans l'appel du modèle
    -- qui doivent être inscrits en trio : section, titre et page (même vide)
    -------------------------------------------------------
    local aParamSansNom = {}
    local nNbreParamSansNom = 0
    local szInfo = ''
    for i, v in ipairs(args) do
      aParamSansNom[i] = mw.text.trim(v)
      nNbreParamSansNom = nNbreParamSansNom + 1
      szInfo = szInfo .. 'Paramètre ' .. i .. ' : ' .. aParamSansNom[i] .. '<br>\n'
      if nNbreParamSansNom % nNbreColonnes == 0 then
        szInfo = szInfo .. '<br>\n'
      end
-- szCode = szCode .. i .. ' : ' .. aParamSansNom[i] .. '<br>\n'
    end

    if nNbreParamSansNom % nNbreColonnes ~= 0 then
      szMsg = '<span style="font-size:200%; color:red;">Prière de saisir ' .. nNbreColonnes .. ' paramètres par ligne.</span>' .. '<br>\n' .. szInfo
    end
    
    ---------------------
    -- Créer le code HTML
    ---------------------
    -- Pour chaque trio définissant une ligne, ajouter le code pertinent
    -- Le tableau tArgs contient les paramètres nommés
    --------------------------------------------------------------------
    local curTitle = mw.title.getCurrentTitle().text
    local texteEntier = mw.title.getCurrentTitle().subpageText == "Texte entier"
    mw.log(Texteentier)
    for n = 1, nNbreParamSansNom, nNbreColonnes do 
      if aParamSansNom[n + 0] ~= nil and aParamSansNom[n + 1] ~= nil and aParamSansNom[n + 2] ~= nil then
        if p.out_arg( args['nodots'], '' ) ~= '' then tArgs[1] = 'nodots' end
        tArgs['section'] = tofragment(aParamSansNom[n + 0], aParamSansNom[n + 2], texteEntier, curTitle)
        tArgs['titre']   = tofragment(aParamSansNom[n + 1], aParamSansNom[n + 2], texteEntier, curTitle)
        tArgs['page']    = aParamSansNom[n + 2]
      end

      -- ctable permet d’utiliser le module Table sans passer par un modèle
      szCode = szCode .. (require 'Module:Table').ctable( tFrame )
    end
    
    if texteEntier and args.ancre ~= nil then -- ancre pour usage futur dans texteEntier
      szCode = '<span id="CorpsTdM">&nbsp;</span>' .. szCode
    end
    
    if string.len( szMsg ) ~= 0 then
      szCode = szMsg
    end

    return szCode
end
    ---------------------
    -- Si la transclusion s’effectue en Texte entier,
    -- construire les liens à partir du nom de page retourné par Page link
    --------------------------------------------------------------------
function tofragment(htmtext, fragment, flag, title)
	if flag then 
		fragment = mw.ustring.match(fragment, '>(.-)</span')
		if fragment and fragment ~= '' then
			htmtext = mw.ustring.gsub(htmtext, '%[%[(.-)|', '[[' .. title .. '#' .. fragment .. '|') 
		else
			htmtext = mw.ustring.gsub(htmtext, '%[%[(.-)|', '[[' .. title .. '|') 
		end
	end
	return htmtext 
end

return p