Module:LienSsPgeTitre

La bibliothèque libre.

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

--[[  
Le module LienSsPgeTitre permet de générer des liens à toutes les sous-pages
d’un chapitre de premier niveau. Il retourne une liste sous la forme
[ [ /nom de la souspage|Titre du chapitre] ]

Condition d’application :
- la page fait appel à <pages index="nom de l’index"
- le sommaire de l’index contient les liens et titres des chapitres recherchés 

EXEMPLE d’appel dans l’espace principal :
{{#invoke:LienSsPgeTitre|construire}}
]]

local p = {}
local t = {}       --nom de la sous-page:titre du chapitre
local pageRoot 
local ns
local pageNom
local pageObj
local pageIndex =''

function initSpace() 
	pageObj = mw.title.getCurrentTitle()
	pageNom = pageObj.text
	ns = pageObj.nsText

	if ns == '' then
		content = pageObj:getContent(pageObj)
		pageIndex = 'Livre:' .. mw.ustring.match(content, 'index="(.-)"')
		pageRoot = mw.title.new(pageIndex)
		content = pageRoot:getContent(pageRoot)
		pageRoot = mw.ustring.match( content, '|Titre=%[%[(.-)[|%]]',1)
	--pageRoot = mw.ustring.gsub(pageRoot, '([%^%$%(%)%%%.%*%+%-%?])', '%%' .. '%1')
	end	
end

function p.compose(frame)

	local titre
	local tdm
	local wss = ''
	
	local args = frame.args
	if args[1] == nil then
		args = frame:getParent().args
	end	
	
	if pageIndex == '' then initSpace() end		
	
	if ns == '' then
		titre = mw.title.new(pageIndex)
		textContent = titre:getContent(titre)
		sommaire = mw.ustring.match( textContent, '|Sommaire=(.-)|Epigraphe=',1)

		tdm = frame:preprocess(sommaire)

	--ajouter % devant tout caractère magique dans le nom de la page 
	   titre = mw.ustring.gsub(pageNom, '([%^%$%(%)%%%.%*%+%-%?])', '%%' .. '%1')

		index = ""


		for spage, chap in mw.ustring.gmatch(tdm, '%[%[' .. titre .. '/(.-)|(.-)%]%]') do  
				--table.insert(t, { spage, chap })
				wss = wss .. '\n[[/' .. spage .. '|' .. chap .. ']]'
		end	
		--for i, ch in ipairs(t) do
		--	wss = wss .. '\n[[/' .. ch[1] .. '|' .. ch[2] .. ']]'
		--end
		if args[1] == 'cach' then
			wss = '<div class="ws-summary" style="visibility:hidden; ' .. 
			  'color: transparent; font-size: ' .. (args['fs'] or '30%') .. '">' ..
			  wss .. '</div>'
		end
		
	end
	return wss
	--return tdm
	--return mw.dumpObject(t)
end

return p