Aller au contenu

Poképédia subit actuellement quelques soucis techniques !
L'équipe est au courant des problèmes et fait le nécessaire pour les arranger le plus rapidement possible. Merci de votre compréhension !

Module:Sommaire

De Poképédia

La documentation pour ce module peut être créée à Module:Sommaire/doc

local p = {}

function p.table(frame)
	local result = {}
	local content = frame.args[1]
	if not content then return "" end
	
	table.insert(result, '<table class="toc" style="white-space:nowrap"><tr><td style="text-align:center; padding-left:1.5em; padding-right:1.5em"><b>Sommaire</b><br><hr></td></tr><td>')
	
	local split_content = mw.text.split(content, "\n")
	local i = 1
	local counters = {}
	local last_depth = nil
	local max_depth = 0
	while split_content[i] do
		local line = split_content[i]
		if line ~= "" then
			local depth = 1
			local len_line = #line
			
			-- Count subsection depth
			while depth < len_line and line:sub(depth, depth) == ">" do
				depth = depth + 1
			end
			
			-- If there is non-empty content
			if depth <= len_line then
				line = line:sub(depth)
				if depth > max_depth then max_depth = depth end
				if last_depth == nil or depth < last_depth then
					-- Multiple first section leap; should not happen normally
					for j = 1, depth + 1 do
						if not counters[j] then counters[j] = 0 end
					end
					-- Reset subsections level after the current one
					for j = depth + 1, max_depth do
						counters[j] = 0
					end
				end
				
				-- Increasing counters
				if counters[depth] then
					counters[depth] = counters[depth] + 1
				else
					counters[depth] = 1
				end
				
				subsection_string = {}
				for j = 2, depth do
					table.insert(result, "&emsp;")
				end
				for j = 1, depth do
					table.insert(subsection_string, counters[j])
				end
				
				table.insert(result, table.concat(subsection_string, ".") .. " [[#" .. line .. "]]<br>")
				
				last_depth = depth
			end
		end
		i = i + 1
	end
	
	table.insert(result, "</td></table>")
	return table.concat(result, "")
end

return p