Module:IndexAlpha

La bibliothèque libre.

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

local p = {}

local function indexOf(array, value)
	for i, v in ipairs(array) do
		if v == value then
			return i
		end
	end
	return nil
end

local function applyModifs(alphabet, modifs)
	for modif in mw.ustring.gmatch(modifs, "([^,]+)") do
		if mw.ustring.match(modif, "^-.+$") ~= nil then
			local letter = mw.ustring.match(modif, "-(.+)")	
			local index = indexOf(alphabet, letter)
			if letter ~= nil then
				table.remove(alphabet, index)	
			else
				error("Lettre inconnue : " .. letter)
			end
		elseif mw.ustring.match(modif, "^.+>.+$") ~= nil then
			local l1, l2 = mw.ustring.match(modif, "^(.+)>(.+)$")
			local i1 = indexOf(alphabet, l1)
			local i2 = indexOf(alphabet, l2)
			if i1 ~= nil and i2 ~= nil then
				table.remove(alphabet, i2)
				i1 = indexOf(alphabet, l1)
				table.insert(alphabet, i1+1, l2)
			else
				error("Problème d’index pour la modif suivante : " .. modif)
			end
		elseif mw.ustring.match(modif, "^.+%+.+$") ~= nil then
			local l1, l2 = mw.ustring.match(modif, "^(.+)%+(.+)$")
			local i1 = indexOf(alphabet, l1)
			local i2 = indexOf(alphabet, l2)
			if i1 ~= nil and i2 == nil then
				table.insert(alphabet, i1+1, l2)
			elseif i1 == nil and i2 ~= nil then
				table.insert(alphabet, i2, l1)
			else
				error("Problème d’index pour la modif suivante : " .. modif)
			end
		else
			error("Modificateur inconnu : " .. modif)
		end
	end
	return alphabet
end

function p.indexMain(frame)
	-- Params
	local args = frame:getParent().args
	local lien = args["lien"] or "Index alphabétique - %s"
	local modifs = args["modifs"] or ""
	if args["restreint"] ~= nil then
		if modifs == "" then
			modifs = "-J,-V"
		else
			modifs = "-J,-V," .. modifs
		end
	end
	
	-- Build alphabet
	local alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }
	alphabet = applyModifs(alphabet, modifs)
	
	-- Build result
	local res = ""
	local half = math.floor(#alphabet / 2)
	for i,c in ipairs(alphabet) do
		res = res .. mw.ustring.format("[[/" .. lien .. "|%s]]", c, c)
		if i ~= half and i ~= #alphabet then
			res = res .. " – "
		end
		if i == half then
			res = res .. "<br>"
		end
	end
	
	return "<small>" .. res .. "</small>"
end

function p.indexNavigation(frame)
	-- Params
	local args = frame:getParent().args
	local lien = args["lien"] or "Index alphabétique - %s"
	local modifs = args["modifs"] or ""
	if args["restreint"] ~= nil then
		if modifs == "" then
			modifs = "-J,-V"
		else
			modifs = "-J,-V," .. modifs
		end
	end
	
	-- Build alphabet
	local alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }
	alphabet = applyModifs(alphabet, modifs)

	-- Build result
	local res = "[[../|Index Général]]"
	for i,c in ipairs(alphabet) do
		res = res .. mw.ustring.format(" [[../" .. lien .. "|%s]]", c, c)
	end
	return '<div style="text-align: center;">' .. res .. "</div>"
end

return p