Aller au contenu

Module:Footnotes

Depi Wikipedya, ansiklopedi lib

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

------
-- Module:Footnotes 
-- 2017-08-13
--
-- Largement inspiré du Module:Footnotes de la Wikipédia en anglais
--
-- https://en.wikipedia.org/w/index.php?title=Module:Footnotes&oldid=790056011
-- 
-- Note : Cantons-de-l'Est a seulement étudié et testé la fonction f.sfn(). 
------
local f = {};

local args_default = {
	bracket_left = "",
	bracket_right = "",
	bracket_year_left = "",
	bracket_year_right = "",
	postscript = "",
	page = "",
	location = "",
	page_sep = ", p. ",
	ref = "",
	gr = "",
	id = "",
	name = "",
	text = "",
	P1 = "",
	P2 = "",
	P3 = "",
	P4 = "",
	P5 = ""
};

local function trim( str )
	if str == nil then
		return nil;
	end
	return str:match( "^%s*(.-)%s*$");
end    

local function is_year (param)
	return param:match ('^%d%d%d%d?%l?$') or param:match ('^n%.d%.%l?$') or param:match ('^nd%l?$') or param:match ('^c%. %d%d%d%d?%l?$');
end


local function core( args )
	local result;

	if args.P5 ~= "" then
		if is_year (args.P5) then
			result = table.concat ({args.P1, ' <i>et al.</i> ', args.bracket_year_left, args.P5, args.bracket_year_right});
		else
			args.P5 = '';							-- when P5 not a year don't include in anchor
			result = table.concat ({args.P1, ' <i>et al.</i>'});		-- and don't render it
		end

	elseif args.P4 ~= "" then
		if is_year (args.P4) then
			result = table.concat ({args.P1, ', ', args.P2, ' et ', args.P3, ' ', args.bracket_year_left, args.P4, args.bracket_year_right});	-- three names and a year
		else
			result = table.concat ({args.P1, ' <i>et al.</i>'});			-- four names
		end

	elseif args.P3 ~= "" then
		if is_year (args.P3) then
			result = table.concat ({args.P1, ' et ', args.P2, ' ', args.bracket_year_left, args.P3, args.bracket_year_right});	-- two names and a year
		else
			result = table.concat ({args.P1, ', ', args.P2, ' ', ' et ', args.P3});	-- three names
		end
			
	elseif args.P2 ~= "" then
		if is_year (args.P2) then
			result = table.concat ({args.P1, ' ', args.bracket_year_left, args.P2, args.bracket_year_right});	-- one name and year
		else
			result = table.concat ({args.P1, ' et ', args.P2});							-- two names
		end
		
	else
		result = args.P1;												-- one name
	end

	if ('.' == result:sub(-1)) and ('' == args.page) and ('' == args.location) then
		args.postscript = '';		-- prevent double periods when date is 'n.d.'
	end
	
	if args.id ~= "" then
		if args.text ~= "" then
			result = "[[#" .. args.id .. "|" .. args.text .. "]]";
		else
			result = "[[#" .. args.id .. "|" .. result .. "]]";
		end
	else
		if args.text ~= "" then
			result = "[[#" .. mw.uri.anchorEncode(args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5) .. "|" .. args.text .. "]]";
		else
			result = "[[#" .. mw.uri.anchorEncode(args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5) .. "|" .. result .. "]]";
		end
	end

	if args.page ~= "" then
		result = result .. args.page_sep .. args.page;
	end      

	if args.location ~= "" then
		result = result .. ", " .. args.location;
	end

	result = args.bracket_left .. result .. args.bracket_right .. args.postscript;
	return result;
end

function f.harvard_core( frame )
	local args = {};
	local pframe = frame:getParent();

	args.bracket_left = pframe.args.BracketLeft or "";
	args.bracket_right = pframe.args.BracketRight or "";
	args.bracket_year_left = pframe.args.BracketYearLeft or "";
	args.bracket_year_right = pframe.args.BracketYearRight or "";
	args.postscript = pframe.args.Postscript or "";
	if 'none' == args.postscript then
		args.postscript = '';
	end

	args.page = pframe.args.Page or pframe.args.Pages or "";
	args.location = pframe.args.Location or "";
	args.page_sep = pframe.args.PageSep or "";
	args.ref = pframe.args.REF or "{{{REF}}}";
	args.id = pframe.args.ID or "{{{ID}}}" or "";
	args.text = pframe.args.TEXT or pframe.args.TEXTE or "{{{TEXT}}}" or "{{{TEXTE}}}" or "";
	args.P1 = trim( pframe.args.P1 ) or "";
	args.P2 = trim( pframe.args.P2 ) or "";
	args.P3 = trim( pframe.args.P3 ) or "";
	args.P4 = trim( pframe.args.P4 ) or "";
	args.P5 = trim( pframe.args.P5 ) or "";

	return core( args );
end

function f.harvard_citation( frame )
	local args = args_default;
	pframe = frame:getParent();

	args.bracket_left = "(";
	args.bracket_right = ")";
	args.page = pframe.args.p or pframe.args.page or pframe.args.pp or pframe.args.pages or "";
	args.location = pframe.args.loc or "";
	args.ref = pframe.args.ref or pframe.args.Ref or "";
	args.id = pframe.args.id or pframe.args.Id or "";
	args.text = pframe.args.text or pframe.args.Text
		or pframe.args.texte or pframe.args.Texte
		or "";
	args.P1 = trim( pframe.args[1] ) or "";
	args.P2 = trim( pframe.args[2] ) or "";
	args.P3 = trim( pframe.args[3] ) or "";
	args.P4 = trim( pframe.args[4] ) or "";
	args.P5 = trim( pframe.args[5] ) or "";

	return core( args );
end

function f.harvard_citation_no_bracket( frame )
	local args = args_default;
	pframe = frame:getParent();

	args.page = pframe.args.p or pframe.args.page or pframe.args.pp or pframe.args.pages or "";
	args.location = pframe.args.loc or "";
	args.ref = pframe.args.ref or pframe.args.Ref or "";
	args.id = pframe.args.id or pframe.args.Id or "";
	args.text = pframe.args.text or pframe.args.Text
		or pframe.args.texte or pframe.args.Texte
		or "";
	args.P1 = trim( pframe.args[1] ) or "";
	args.P2 = trim( pframe.args[2] ) or "";
	args.P3 = trim( pframe.args[3] ) or "";
	args.P4 = trim( pframe.args[4] ) or "";
	args.P5 = trim( pframe.args[5] ) or "";

	return core( args );
end

function f.sfn( frame )
	local args = args_default;
	for k, v in pairs( frame.args ) do			-- for {{sfnp}}, override default with values provided in the #invoke:
		args[k] = v;	   
	end
	
	pframe = frame:getParent();
	
	-- Paramètres pour {{harvsp
	---------------------------
	args.postscript = pframe.args.postscript or pframe.args.ps or ".";
	if 'none' == args.postscript then
		args.postscript = '';
	end
	args.page = pframe.args.p or pframe.args.page or pframe.args.pp or pframe.args.pages or "";
	args.location = pframe.args.loc or "";
	args.ref = pframe.args.ref or pframe.args.Ref or "";
	args.id = pframe.args.id or pframe.args.Id or "";
	args.text = pframe.args.text or pframe.args.Text 
		or pframe.args.texte or pframe.args.Texte 
		or "";
	args.P1 = trim( pframe.args[1] ) or "";
	args.P2 = trim( pframe.args[2] ) or "";
	args.P3 = trim( pframe.args[3] ) or "";
	args.P4 = trim( pframe.args[4] ) or "";
	args.P5 = trim( pframe.args[5] ) or "";

	local result = core( args );
	
	-- Paramètres pour <ref
	-----------------------
	args.gr = pframe.args.gr or pframe.args.Gr 
		or pframe.args.group or pframe.args.Group 
		or pframe.args.groupe or pframe.args.Groupe 
		or "";
	args.name = pframe.args.nom or pframe.args.Nom
		or pframe.args.name or pframe.args.Name
		or "";
	
	local RefName = "";
	if args.name ~= "" then
		RefName = args.name;
	else
		RefName = args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5 .. args.page .. args.location .. args.id;
	end
	
	
	-- Si les paramètres sont "groupe", "loc" et/ou "page", alors créer "<ref group=...>p. ...</ref>"
	-- sinon, créer l'équivalent de "<ref ...>{{harvsp|... </ref>"
	if      (args.gr ~= "") and (args.page ~= "") and (args.location == "")
		and (args.postscript == "" or args.postscript == ".") and (args.ref == "") 
		and (args.id == "") and (args.text == "") 
		and (args.P1 == "") and (args.P2 == "") and (args.P3 == "") and (args.P4 == "") and (args.P5 == "")
	then
		result = frame:extensionTag{ name = "ref", args = {name='\"' .. 'p' .. args.page .. '\"', group = args.gr}, content="p.&nbsp;" .. args.page };
	elseif (args.gr ~= "") and (args.page == "") and (args.location ~= "")
		and (args.postscript == "" or args.postscript == ".") and (args.ref == "")
		and (args.id == "") and (args.text == "") 
		and (args.P1 == "") and (args.P2 == "") and (args.P3 == "") and (args.P4 == "") and (args.P5 == "")
	then
		result = frame:extensionTag{ name = "ref", args = {name=RefName, group = args.gr}, content=args.location};
	elseif (args.gr ~= "") and (args.page ~= "") and (args.location ~= "")
		and (args.postscript == "" or args.postscript == ".") and (args.ref == "") 
		and (args.id == "") and (args.text == "") 
		and (args.P1 == "") and (args.P2 == "") and (args.P3 == "") and (args.P4 == "") and (args.P5 == "")
	then
		result = frame:extensionTag{ name = "ref", args = {name='\"' .. 'p' .. args.page .. '\"', group = args.gr}, content=args.location .. args.page_sep .. args.page };
	else
		result = frame:extensionTag{ name = "ref", args = {name=RefName, group = args.gr}, content=result };
	end

	return result;
end

return f;