Module:CorpsTdM

La bibliothèque libre.
Documentation du module [voir] [modifier] [purger]
La documentation de ce module Scribunto écrit en Lua est incluse depuis sa sous-page de documentation.


Le module:CorpsTdM est conçu pour créer le corps d'une table des matières (TdM).

Exemples[modifier]

Voyez ces exemples : [1] et [2].

Paramètres dans l'appel de modèle[modifier]

À l'exception des paramètres section, titre et page, il accepte les mêmes paramètres que ceux du modèle:Table. Ces derniers s'appliquent à chaque ligne du corps de la TdM. En ce qui concerne les trois premiers paramètres, il faut les saisir les uns à la suite des autres, par triplets.

Le paramètre nodots est traité d'une façon différente ici. Il faut inscrire |nodots=1 ou |nodots=oui, alors qu'écrire |nodots (sans le signe égal) crée un échec de traitement.

Paramètres dans le code du modèle[modifier]

Aucun

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
	if args[1] == nil then args = mw.getCurrentFrame().args end
    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 Module:Table.ctable
    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)
    if p.out_arg( args['nodots'], '' ) ~= '' then tArgs[1] = 'nodots' end
    for n = 1, nNbreParamSansNom, nNbreColonnes do 
      if aParamSansNom[n + 0] ~= nil and aParamSansNom[n + 1] ~= nil and aParamSansNom[n + 2] ~= nil then
        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 string.len( szMsg ) ~= 0 then
      szCode = szMsg
    end

    return szCode
end
    ------------------------------------------------------
    -- Si la transclusion s’effectue dans un texte entier,
    -- construire les liens à partir du title
    ------------------------------------------------------
function tofragment(htmtext, fragment, flag, title)
	if flag then 
		fragment = mw.ustring.match(fragment, '>(.-)</span',-40)
		if fragment and fragment ~= '' then
			htmtext = mw.ustring.gsub(htmtext, '%[%[(.-)|', '[[' .. title .. '#' .. fragment .. '|')
			--htmtext = mw.ustring.gsub(htmtext, '%[%[('.. mw.text.split(title,"/")[1] ..'.-)|', '[[' .. title .. '#' .. fragment .. '|') 
		else
			htmtext = mw.ustring.gsub(htmtext, '%[%[(.-)|', '[[' .. title .. '|') 
		end
	end
	return htmtext 
end

return p