Module:Métro de Toronto/art

 Documentation[créer] [purger]
---- vi: set sw=4 ts=4 ai sm noet:
----
---- Module:Métro de Toronto/art
---- Création : Wikipédia en français, juillet 2024
---- Usage normale par [[Modèle:Métro de Toronto/art/station]]
----
---- Ce module vise à faciliter le mise en page du tableau « Œuvres »

require ('strict');
local p = {};

---
--- Entrée : mise en forme du nom d'une station, et possiblement les lignes associées
--- Normalement exécuté par {{#invoke:Métro de Toronto/art|station}} dans le Modèle:Métro de Toronto/art/station
---
p.station = function (frame)
	local parent = frame:getParent();
	local it;

	local rowspan, nom, cle_de_tri, lignes;
	lignes = {};
	for _, a in pairs({frame, parent}) do
		if a then
			local exposant_prevu = false;
			for k, v in pairs(a.args) do
				v = v:gsub('^%s+', ''):gsub('%s+$', '');
				if type(k) == 'number' then
					if k == 1 then
						nom = v;
					else
						error('Trop de paramètres anonymes, paramètre « ' .. v .. ' » (numéro ' .. k .. ') non prévu');
					end
				elseif k == 'rowspan' then
					rowspan = tonumber(v);
				elseif k == 'nom' or k == 'station' then
					nom = v;
				elseif k == 'clé de tri' then
					cle_de_tri = v;
				elseif k == 'lignes' then
					-- FIXME TODO
				else
					error('Paramètre « ' .. k .. ' » inconnu');
				end
			end
		end
	end
	if rowspan and rowspan < 1 then
		error('Paramètre rowspan=' .. rowspan .. ' non valide');
	elseif not nom then
		error('Nom manquant');
	elseif cle_de_tri and cle_de_tri:match('"') then
		error('Guillemet droit (") dans la clé de tri interdit');
	end
	local proprietes = {};
	if rowspan and rowspan > 1 then
		table.insert(proprietes, {'rowspan', rowspan});
	end
	if cle_de_tri then
		table.insert(proprietes, {'data-sort-value', cle_de_tri});
	end
	it = '';
	if #proprietes > 0 then
		for i = 1, #proprietes do
			local v = proprietes[i][2];
			if i > 1 then
				it = it .. ' ';
			end
			it = it .. proprietes[i][1] .. '=';
			if type(v) == 'string' and v:match(' ') then
				it = it .. '"' .. v .. '"';
			else
				it = it .. v;
			end
		end
		it = it .. '|';
	end
	it = it .. ' ';
	it = it .. '[[' .. nom .. ' (métro de Toronto)' .. '|' .. nom .. ']]';
	return it;
end

---
--- Tests unitaires
--- Normalement executée dans la console de débogage par p.test()
---
--- En cas de succès, aucun résultat se produit
--- En cas de défaut, une erreur se produit par un appel assert()
---
p.test = function ()
	local a, b;

	-- Émuler {{#invoke:Métro de Toronto/art|station}} (aucun paramètre)
	-- Résultat prévu: erreur (Nom manquant)
	a, b = pcall(p.station, {
		['getParent'] = function ()
			return {
				extensionTag = function () return '' end;
				args= {
				};
			};
		end;
		extensionTag = function () return '' end;
		args = {
		};
	});
	assert(not a and b:match('Nom manquant'));

	-- Émuler {{#invoke:Métro de Toronto/art|station|Finch}}
	-- Résultat prévu: ' [[Finch (métro de Toronto)|Finch]]'
	a, b = pcall(p.station, {
		['getParent'] = function ()
			return {
				extensionTag = function () return '' end;
				args= {
				};
			};
		end;
		extensionTag = function () return '' end;
		args = {
			'Finch'
		};
	});
	assert(a and b == ' [[Finch (métro de Toronto)|Finch]]');

	-- Émuler {{#invoke:Métro de Toronto/art|station|station=Finch}}
	-- Résultat prévu: ' [[Finch (métro de Toronto)|Finch]]'
	a, b = pcall(p.station, {
		['getParent'] = function ()
			return {
				extensionTag = function () return '' end;
				args= {
				};
			};
		end;
		extensionTag = function () return '' end;
		args = {
			['station'] = 'Finch'
		};
	});
	assert(a and b == ' [[Finch (métro de Toronto)|Finch]]');

	-- Émuler {{#invoke:Métro de Toronto/art|station|nom=Finch}}
	-- Résultat prévu: ' [[Finch (métro de Toronto)|Finch]]'
	a, b = pcall(p.station, {
		['getParent'] = function ()
			return {
				extensionTag = function () return '' end;
				args= {
				};
			};
		end;
		extensionTag = function () return '' end;
		args = {
			['nom'] = 'Finch'
		};
	});
	assert(a and b == ' [[Finch (métro de Toronto)|Finch]]');

	-- Émuler {{#invoke:Métro de Toronto/art|station|nom=Finch|rowspan=2}}
	-- Résultat prévu: 'rowspan=2| [[Finch (métro de Toronto)|Finch]]'
	a, b = pcall(p.station, {
		['getParent'] = function ()
			return {
				extensionTag = function () return '' end;
				args= {
				};
			};
		end;
		extensionTag = function () return '' end;
		args = {
			['nom'] = 'Finch',
			['rowspan'] = '2'
		};
	});
	assert(a and b == 'rowspan=2| [[Finch (métro de Toronto)|Finch]]');

	-- Émuler {{#invoke:Métro de Toronto/art|station|nom=St. Clair|clé de tri=Saint Clair}}
	-- Résultat prévu: 'data-sort-value="Saint Clair"| [[St. Clair (métro de Toronto)|St. Clair]]'
	a, b = pcall(p.station, {
		['getParent'] = function ()
			return {
				extensionTag = function () return '' end;
				args= {
				};
			};
		end;
		extensionTag = function () return '' end;
		args = {
			['nom'] = 'St. Clair',
			['clé de tri'] = 'Saint Clair'
		};
	});
	assert(a and b == 'data-sort-value="Saint Clair"| [[St. Clair (métro de Toronto)|St. Clair]]');

	-- Émuler {{#invoke:Métro de Toronto/art|station|nom=St. Clair|clé de tri=Saint Clair|rowspan=2}}
	-- Résultat prévu: 'rowspan=2 data-sort-value="Saint Clair"| [[St. Clair (métro de Toronto)|St. Clair]]'
	a, b = pcall(p.station, {
		['getParent'] = function ()
			return {
				extensionTag = function () return '' end;
				args= {
				};
			};
		end;
		extensionTag = function () return '' end;
		args = {
			['nom'] = 'St. Clair',
			['clé de tri'] = 'Saint Clair',
			['rowspan'] = '2'
		};
	});
	assert(a and b == 'rowspan=2 data-sort-value="Saint Clair"| [[St. Clair (métro de Toronto)|St. Clair]]');
end

return p