Module:Sommaire
Apparence
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, " ")
end
for j = 1, depth do
table.insert(subsection_string, counters[j])
end
if not line:find("|") then
line = line .. "|" .. line
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
function p.triAlphabetique(frame)
local pageTitle
local isLocal = true
if (frame.args['page'] ~= nil) then
pageTitle = mw.title.new(frame.args['page'])
isLocal = false
else
pageTitle = mw.title.getCurrentTitle()
end
local content = pageTitle:getContent()
local aimedTitleLevel = 2 --à modifier si l'on souhaite un jour utiliser ça pour un autre niveau de titres
local sections = {}
local foundTitles = {}
for line in content:gmatch("[^\n]+") do
local hashes, title = line:match("^(=+)%s(.*)%s%1$")
if hashes then
title = frame:preprocess(title)
title = title:gsub("''", ""):gsub("%[%[(.+)|", ""):gsub("%[%[", ""):gsub("%]%]", "")
local titleLevel = #hashes
local id
if foundTitles[title] == nil then
foundTitles[title] = 1
id = mw.uri.anchorEncode(title)
else
foundTitles[title] = foundTitles[title] + 1
id = mw.uri.anchorEncode(title) .. "_" .. foundTitles[title]
end
if (titleLevel == aimedTitleLevel) then
id = '#' .. id
if not isLocal then id = pageTitle.text .. id end
table.insert(sections, {
title = title,
id = id,
titleLevel = titleLevel
})
end
end
end
local function sortKey(s)
s = mw.ustring.lower(s)
s = mw.ustring.gsub(s, "[àâä]", "a")
s = mw.ustring.gsub(s, "[éèêë]", "e")
s = mw.ustring.gsub(s, "[îï]", "i")
s = mw.ustring.gsub(s, "[ôö]", "o")
s = mw.ustring.gsub(s, "[ùûü]", "u")
s = mw.ustring.gsub(s, "ç", "c")
s = mw.ustring.gsub(s, "œ", "oe")
return s
end
table.sort(sections, function(a, b)
return sortKey(a.id) < sortKey(b.id)
end)
local result = {}
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>')
for _, section in ipairs(sections) do
table.insert(result, '[[' .. section.id .. '|' .. section.title .. ']]<br>')
end
table.insert(result, "</td></table>")
return table.concat(result, '')
end
return p