Módulo:Zona de pruebas/Juan Mayordomo/argumentos

Este módulo no tiene página de documentación[crear]
local z = {}

function z.obtenerArgumentos(frame)
	if frame.args[1] then 
		return frame.args
	end
 
	return frame:getParent().args
end

function z.obtenerArgumentosConValor(frame)
    if frame == mw.getCurrentFrame() then
        argumentos = frame:getParent().args
    else
        argumentos = frame.args or frame
    end
    
    return require('Módulo:Tablas').copiarElementosConValor(argumentos)
end

-- Adaptación de la función argument_wrapper de :en:Module:Citation/CS1

function z.traducir(args, aliases, messages, defaults)
	local origin = {};
	
	local function is_set( var )
		return not (var == nil or var == '');
	end
	
	local function in_array( needle, haystack )
		if needle == nil then
			return false;
		end
		for n,v in ipairs( haystack ) do
			if v == needle then
				return n;
			end
		end
		return false;
	end	
	
	local function safe_for_italics( str )
		if not is_set(str) then
			return str;
		else
			if str:sub(1,1) == "'" then str = "<span></span>" .. str; end
			if str:sub(-1,-1) == "'" then str = str .. "<span></span>"; end
		
			-- Remove newlines as they break italics.
			return str:gsub( '\n', ' ' );
		end
	end	
	
	local function wrap_style (key, str)
		if not is_set( str ) then
			return "";
		elseif in_array( key, { 'italic-title', 'trans-italic-title' } ) then
			str = safe_for_italics( str );
		end

		return substitute( cfg.presentation[key], {str} );
	end	
	
	local function is_alias_used (args, alias, index, enumerated, value, selected, error_list)
		if enumerated then															-- is this a test for an enumerated parameters?
			alias = alias:gsub ('#', index);										-- replace '#' with the value in index
		else
			alias = alias:gsub ('#', '');											-- remove '#' if it exists
		end

		if is_set(args[alias]) then													-- alias is in the template's argument list
			if value ~= nil and selected ~= alias then								-- if we have already selected one of the aliases
				local skip;
				for _, v in ipairs(error_list) do									-- spin through the error list to see if we've added this alias
					if v == alias then
						skip = true;
						break;														-- has been added so stop looking 
					end
				end
				if not skip then													-- has not been added so
					table.insert( error_list, alias );								-- add error alias to the error list
				end
			else
				value = args[alias];												-- not yet selected an alias, so select this one
				selected = alias;
			end
		end
		return value, selected;														-- return newly selected alias, or previously selected alias
	end	
	
	local function select_one( args, aliases_list, error_condition, index )
		local value = nil;															-- the value assigned to the selected parameter
		local selected = '';														-- the name of the parameter we have chosen
		local error_list = {};

		if index ~= nil then index = tostring(index); end

		for _, alias in ipairs( aliases_list ) do									-- for each alias in the aliases list
			if alias:match ('#') then												-- if this alias can be enumerated
				if '1' == index then												-- when index is 1 test for enumerated and non-enumerated aliases
					value, selected = is_alias_used (args, alias, index, false, value, selected, error_list);	-- first test for non-enumerated alias
				end
				value, selected = is_alias_used (args, alias, index, true, value, selected, error_list);		-- test for enumerated alias
			else
				value, selected = is_alias_used (args, alias, index, false, value, selected, error_list);		--test for non-enumerated alias
			end
		end

		if #error_list > 0 and 'none' ~= error_condition then						-- for cases where this code is used outside of extract_names()
			local error_str = "";
			for _, k in ipairs( error_list ) do
				if error_str ~= "" then error_str = error_str .. messages['parameter-separator'] end
				error_str = error_str .. wrap_style ('parameter', k);
			end
			if #error_list > 1 then
				error_str = error_str .. messages['parameter-final-separator'];
			else
				error_str = error_str .. messages['parameter-pair-separator'];
			end
			error_str = error_str .. wrap_style ('parameter', selected);
			table.insert( z.message_tail, { set_error( error_condition, {error_str}, true ) } );
		end
	
		return value, selected;
	end	
	
	return setmetatable({
		ORIGIN = function( self, k )
			local dummy = self[k]; --force the variable to be loaded.
			return origin[k];
		end
	},
	{
		__index = function ( tbl, k )
			if origin[k] ~= nil then
				return nil;
			end
			
			local args, list, v = args, aliases[k];
			
			if type( list ) == 'table' then
				v, origin[k] = select_one( args, list, 'redundant_parameters' );
				if origin[k] == nil then
					origin[k] = ''; -- Empty string, not nil
				end
			elseif list ~= nil then
				v, origin[k] = args[list], list;
			else
				-- maybe let through instead of raising an error?
				-- v, origin[k] = args[k], k;
				error( messages['unknown_argument_map'] );
			end
			
			-- Empty strings, not nil;
			if v == nil then
				v = defaults[k] or '';
				origin[k] = '';
			end
			
			tbl = rawset( tbl, k, v );
			return v;
		end,
	});
	
end

return z