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

local p = {}
local args = {}

function p.remplacePar(frame)
    local textContent = ""
    local nopage = ""
    local cible = ""
	args = frame.args
	if args['page'] == nil then
		args = frame:getParent().args
	end
    nopage = tostring(args['page'])
    if nopage ~= nil and (nopage ~= '') then
    	nopage = mw.text.trim(nopage)
        titre  = mw.title.getCurrentTitle()
        ns     = titre.nsText
        if ns == "" then --récupération de l’index dans l’espace principal 
        	textContent = titre.getContent(titre)
        	index = mw.ustring.match( textContent, 'index%="(.-)"')
		    cible = "Page:".. index .. "/" .. nopage
		    titre = mw.title.new(cible)
		    textContent = titre.getContent(titre)  
        else
        	--voir = (args.voir == "oui") or (frame.args.voir == "oui")
        	textContent = titre.getContent(titre)
        	textContent = mw.ustring.match( textContent, '%{%{#invoke:Util|remplacePar|(.+)%}%}')      --'%{%{#invoke:Util|remplacePar|page=[^|]-|(.+)%}%}')
        	textContent = mw.ustring.gsub(textContent,'page=[^|]-|', '')
        	textContent = mw.ustring.gsub(textContent,'|page=[^|]-', '')
	    	mw.addWarning( "{{rouge|!! Cette page sera remplacée par la page " .. nopage .. " lors de la transclusion !!}}")
        end
		if mw.ustring.find(textContent, 'Util|remplacePar') then
        	textContent = mw.ustring.match( textContent, '%{%{#invoke:Util|remplacePar|(.+)%}%}')      --'%{%{#invoke:Util|remplacePar|page=[^|]-|(.+)%}%}')
        	textContent = mw.ustring.gsub(textContent,'page=[^|]-|', '')
        	textContent = mw.ustring.gsub(textContent,'|page=[^|]-', '')		    	
		end
    end  
	return  frame:preprocess(textContent)
end	


--Permet de récupérer la 2e portion d’un paragraphe lorsque la fluidité
--du texte est compromise par un tableau ou une illustration centré à 
--l’intérieur de ce paragraphe
function p.noinclude(frame)
	parentFrame = frame:getParent()
    args = parentFrame.args
    local textenoinclude = ""
    local nopage = ""
    local id
    local voir = true --pour voir le texte récupéré dans l’espace Page
    if not args[1] and frame.args[1] then
		args = frame.args
    end
    	nopage = tostring(args[1])
    	id = args["id"]
    if nopage then
    	nopage = mw.text.trim(nopage)
        titre  = mw.title.getCurrentTitle()
        ns     = titre.nsText
        if ns == "" then --récupération de l’index dans l’espace principal 
        	texte = titre.getContent(titre)
        	index = mw.ustring.match( texte, 'index%="(.-)"')
        else
        	voir = args.voir == "oui"
        	index =	titre.baseText
        end
	    cible = "Page:".. index .. "/" .. nopage
	    titre = mw.title.new(cible)
	    texte = titre.getContent(titre)
	    if texte then
		    if id then
		    	textenoinclude = mw.ustring.match( texte, '<noinclude.- id="-' ..
		    		id .. '"-.->(.-)</noinclude>')	
		    else
		    	textenoinclude = mw.ustring.match( texte, '<noinclude>(.-)</noinclude>',10)
			    if mw.ustring.find(textenoinclude, "<references/>") then
			    	textenoinclude = ""
			    	mw.addWarning( "{{rouge|!! Le modèle noinclude n’a pas " ..
			    		"trouvé le texte à récupérer en page" .. nopage .. " !!}}")
			    end
		    end
		    if not voir then
		    	textenoinclude = ""
		    end
		 end
    end  
return frame:preprocess(textenoinclude)
end	

--Permet d’éliminer le liens avec fragment pour qu’ils ne soient pas
--considérés dans la séquence de lien servant à déterminer l’ordre des 
--sous-pages ou des chapitres
function p.stripLinks (frame)
	args = frame.args
	if args[1] == nil then
		args = frame:getParent().args
	end
	local strCopy = mw.ustring.gsub( args[1], '(%[%[([^#^|]-)#([^|]-)|([^%]]-)%]%])','%4')
	return strCopy
end

--Permet de retarder la transclusion jusqu’à ce que 
--l’année du décès(2) + droit(3) soit dépassée 
function p.dpchrono(frame)
	args = frame.args
	if args[1] == nil then
		args = frame:getParent().args
	end
	local res = args[1]
	local dateDeces = args[2]
	local droit = args[3]
	local dp = (os.date("*t", Timestamp).year > (dateDeces + droit))
	if not dp then
		titre  = mw.title.getCurrentTitle()
        ns     = titre.nsText
        if ns == '' then 
        	if (args[4] or 'début') == 'début'  then
	        	res = '<div class="ws-noexport"><div style="position:relative;" >' ..
					'<div class="noprint" style= "position: ' ..
					'absolute; right:0em; top:0em;" >' ..
					'<div style="float: right; padding: 10px; text-align: left; ' ..
					'background-color: #F7F8FF; border: 1px solid #3300FF; ' ..
					'width: 180px; margin: 1em; ">' ..
					'<div style="float: left; padding-right: 5px;">' ..
					'[[File:Info icon 001.svg|left|48px|link=]]</div>' ..
					'Cet objet, en tout ou en partie, est encore soumis aux ' .. 
	        		'droits d’auteur et ne sera affiché qu’à partir de janvier ' .. 
	        		(dateDeces + droit + 1) .. '.</div></div></div>' ..
	        		'<p style="padding:40px 0; width:calc(100% - 220px)">' ..
	        		'[[File:OOjs_UI_icon_alert_destructive_black-darkred.svg|80px]]' ..
	        		' <span style="margin-left:0.1em;' ..
	        		'width:font-size:120%">Publication&nbsp;' ..
	        		'retardée.</span></p></div>'
	        else res = ''
	        end
        	return frame:preprocess(res)
        end
	end 
	return res
end

--Permet de rendre insécable les éléments abbr
--et d’espacer les abbréviation 
--petit espace après le point 
function p.abri (frame)
	args = frame.args
	if args[1] == nil then
		args = frame:getParent().args
	end
	local abbr = mw.ustring.gsub( args[1], '(%.)(%a)','%1<span style=' .. 
		'"display:inline-block;width:.2em">&#65279;</span>%2')
	abbr = '<abbr class="abbr" style="white-space:nowrap;" title="' ..
		(args[2] or '') .. '">' .. abbr .. '</abbr>'
	return abbr
end

--Permet de récupérer une section à l’intérieur d’une page du même livre
-- ou d’une page dont on a fourni le titre en argument
function p.section(frame)
	parentFrame = frame:getParent()
    args = parentFrame.args
    local textesection = ""
    local nopage = ""
    local section
    local voir = true --pour voir le texte récupéré dans l’espace Page
    if not args["section"] and frame.args["section"] then
		args = frame.args
    end
    nopage = args["page"]
    section = args["section"]
    cible = args["titre"]
    if cible == nil then
	    if nopage then
	    	nopage = mw.text.trim(nopage)
	        titre  = mw.title.getCurrentTitle()
	        ns     = titre.nsText
	        if ns == "" then --récupération de l’index dans l’espace principal 
	        	texte = titre.getContent(titre)
	        	index = mw.ustring.match( texte, 'index%="(.-)"')
	        else
	        	--voir = args.voir == "oui"
	        	index =	titre.baseText
	        end
		    cible = "Page:".. index .. "/" .. nopage
		else return ''
	    end
	end
    titre = mw.title.new(cible)
    texte = titre.getContent(titre)
    if texte then
    		--ajouter % devant tout caractère magique dans le nom de la section 
		section = mw.ustring.gsub(section, '([%^%$%(%)%%%.%*%+%-%?])', '%%' .. '%1')
    	textesection = mw.ustring.match( texte, '<section begin="' .. section .. '" -/>' .. '(.-)<section end="' .. section .. '" -/>')

	 end

return frame:preprocess(textesection)

end	

return p