Module:Citation/CS1/Utilities

Documentation for this module may be created at Module:Citation/CS1/Utilities/doc

local z = {
	error_cats_t = {};															
	error_ids_t = {};															
	error_msgs_t = {};															
	maint_cats_t = {};															
	prop_cats_t = {};															
	prop_keys_t = {};															
};

local cfg; -- Module:Citation/CS1/Configuration'dan yüklenecek

--[[--------------------------< I S _ S E T >----------------------------------]]
local function is_set (var)
	return not (var == nil or var == '');
end

--[[--------------------------< I N _ A R R A Y >------------------------------]]
-- Hata aldığı yer burasıydı: haystack nil gelirse ipairs çöküyordu.
local function in_array (needle, haystack)
	if needle == nil or haystack == nil then
		return false;
	end
	for n, v in ipairs (haystack) do
		if v == needle then
			return n;
		end
	end
	return false;
end

--[[--------------------------< H A S _ A C C E P T _ A S _ W R I T T E N >----]]
local function has_accept_as_written (str, allow_empty)
	if not is_set (str) then
		return str, false;
	end
	local count;
	if true == allow_empty then
		str, count = str:gsub ('^%(%((.*)%)%)$', '%1'); 
	else
		str, count = str:gsub ('^%(%((.+)%)%)$', '%1');
	end
	return str, 0 ~= count;
end

--[[--------------------------< S U B S T I T U T E >--------------------------]]
local function substitute (msg, args)
	return args and mw.message.newRawMessage (msg, args):plain() or msg;
end

--[[--------------------------< E R R O R _ C O M M E N T >--------------------]]
local function error_comment (content, hidden)
	if not cfg or not cfg.presentation then return content; end
	return substitute (hidden and cfg.presentation['hidden-error'] or cfg.presentation['visible-error'], content);
end

--[[--------------------------< H Y P H E N _ T O _ D A S H >------------------]]
local function hyphen_to_dash (str)
	if not is_set (str) then return str; end
	str = str:gsub ("(%(%(.-%)%))", function(m) return m:gsub(",", ","):gsub(";", ";") end) 
	str = str:gsub ('&[nm]dash;', {['&ndash;'] = '–', ['&mdash;'] = '—'});
	str = str:gsub ('&#45;', '-');
	str = str:gsub ('&nbsp;', ' ');
	
	local out = {};
	local list = mw.text.split (str, '%s*[,;]%s*');
	local accept;

	for _, item in ipairs (list) do
		item, accept = has_accept_as_written (item);
		if not accept and mw.ustring.match (item, '^%w*[%.%-]?%w+%s*[—–-]%s*%w*[%.%-]?%w+$') then
			if mw.ustring.match (item, '^%a+[%.%-]%d+%s*[—–-]%s*%a+[%.%-]%d+$') or
				mw.ustring.match (item, '^%d+[%.%-]%a+%s*[—–-]%s*%d+[%.%-]%a+$') or
				mw.ustring.match (item, '^%d+[%.%-]%d+%s*[—–-]%s*%d+[%.%-]%d+$') then
					item = mw.ustring.gsub (item, '(%w*[%.%-]%w+)%s*[—–-]%s*(%w*[%.%-]%w+)', '<span class="nowrap">%1 –</span> <span class="nowrap">%2</span>');
			elseif mw.ustring.match (item, '^%d+%a+%s*[—–-]%s*%d+%a+$') or
				mw.ustring.match (item, '^%a+%d+%s*[—–-]%s*%a+%d+$') or
				mw.ustring.match (item, '^%d+%s*[—–-]%s*%d+$') or
				mw.ustring.match (item, '^%a+%s*[—–-]%s*%a+$') then
					item = mw.ustring.gsub (item, '(%w+)%s*[—–-]%s*(%w+)', '<span class="nowrap">%1–</span>%2');
			end
		end
		table.insert (out, item);
	end

	local temp_str = table.concat (out, ', ');
	return temp_str:gsub(",", ","):gsub(";", ";");
end

--[[-------------------------< M A K E _ W I K I L I N K >---------------------]]
local function make_wikilink (link, display)
	if not is_set (link) then return '' end
	if is_set (display) and link ~= display then			
		return table.concat ({'[[', link, '|', display, ']]'});			
	else
		return table.concat ({'[[', link, ']]'});
	end
end

--[[--------------------------< S E T _ M E S S A G E >------------------------]]
local added_maint_cats = {}
local function set_message (error_id, arguments, raw, prefix, suffix)
	if not cfg or not cfg.error_conditions then return ''; end
	local error_state = cfg.error_conditions[error_id];
	
	prefix = prefix or '';
	suffix = suffix or '';
	
	if error_state == nil then
		error (cfg.messages['undefined_error'] .. ': ' .. error_id);
	elseif is_set (error_state.category) then
		if error_state.message then
			table.insert (z.error_cats_t, error_state.category);
		else
			if not added_maint_cats[error_id] then
				added_maint_cats[error_id] = true;
				table.insert (z.maint_cats_t, substitute (error_state.category, arguments));
			end
			return;
		end
	end

	local message = substitute (error_state.message, arguments);
	message = table.concat ({message, ' (', make_wikilink (table.concat ({cfg.messages['help page link'], '#', error_state.anchor}), cfg.messages['help page label']), ')'});

	z.error_ids_t[error_id] = true;
	if z.error_ids_t['err_citation_missing_title'] and in_array (error_id, {'err_bare_url_missing_title', 'err_trans_missing_title'}) then
			return '', false;
	end
	
	message = table.concat ({prefix, message, suffix});
	if true == raw then return message, error_state.hidden; end		

	message = error_comment (message, error_state.hidden);
	table.insert (z.error_msgs_t, message);
	return message;
end

--[[-------------------------< I S _ A L I A S _ U S E D >---------------------]]
local function is_alias_used (args, alias, index, enumerated, value, selected, error_list)
	if enumerated then
		alias = alias:gsub ('#', index);
	else
		alias = alias:gsub ('#', '');
	end

	if is_set (args[alias]) then
		if value ~= nil and selected ~= alias then
			local skip;
			-- Buradaki ipairs(error_list) kısmına koruma eklendi
			for _, v in ipairs (error_list or {}) do
				if v == alias then skip = true; break; end
			end
			if not skip then table.insert (error_list, alias); end
		else
			value = args[alias];
			selected = alias;
		end
	end
	return value, selected;
end

--[[--------------------------< A D D _ M A I N T _ C A T >--------------------]]
local function add_maint_cat (key, arguments)
	if not cfg or not cfg.maint_cats then return; end
	if not added_maint_cats [key] then
		added_maint_cats [key] = true;
		table.insert (z.maint_cats_t, substitute (cfg.maint_cats [key], arguments));
	end
end

--[[--------------------------< A D D _ P R O P _ C A T >----------------------]]
local added_prop_cats = {};
local function add_prop_cat (key, arguments, key_modifier)
	if not cfg or not cfg.prop_cats then return; end
	local key_modified = key .. ((key_modifier and key_modifier) or '');
	if not added_prop_cats [key_modified] then
		added_prop_cats [key_modified] = true;
		table.insert (z.prop_cats_t, substitute (cfg.prop_cats [key], arguments));
		table.insert (z.prop_keys_t, 'cs1-prop-' .. key);
	end
end

--[[--------------------------< S A F E _ F O R _ I T A L I C S >--------------]]
local function safe_for_italics (str)
	if not is_set (str) then return str end
	if str:sub (1, 1) == "'" then str = "<span></span>" .. str; end
	if str:sub (-1, -1) == "'" then str = str .. "<span></span>"; end
	return str:gsub ('\n', ' ');
end

--[[--------------------------< W R A P _ S T Y L E >--------------------------]]
local function wrap_style (key, str)
	if not is_set (str) then return ""; end
	if in_array (key, {'italic-title', 'trans-italic-title'}) then
		str = safe_for_italics (str);
	end
	return substitute (cfg.presentation[key], {str});
end

--[[--------------------------< M A K E _ S E P _ L I S T >--------------------]]
local function make_sep_list (count, list_seq, sep_list, sep_list_pair, sep_list_end)
	local list = '';
	if not sep_list and cfg then
		sep_list = cfg.presentation['sep_list'];
		sep_list_pair = cfg.presentation['sep_list_pair'];
		sep_list_end = cfg.presentation['sep_list_end'];
	end
	if 2 >= count then
		list = table.concat (list_seq, sep_list_pair or ' and ');
	elseif 2 < count then
		list = table.concat (list_seq, sep_list or ', ', 1, count - 1);
		list = table.concat ({list, list_seq[count]}, sep_list_end or ', and ');
	end
	return list;
end

--[[--------------------------< S E L E C T _ O N E >--------------------------]]
local function select_one (args, aliases_list, error_condition, index)
	local value = nil;
	local selected = '';
	local error_list = {};
	if index ~= nil then index = tostring(index); end
	-- aliases_list koruması
	for _, alias in ipairs (aliases_list or {}) do
		if alias:match ('#') then
			if '1' == index then
				value, selected = is_alias_used (args, alias, index, false, value, selected, error_list);
			end
			value, selected = is_alias_used (args, alias, index, true, value, selected, error_list);
		else
			value, selected = is_alias_used (args, alias, index, false, value, selected, error_list);
		end
	end
	if #error_list > 0 and 'none' ~= error_condition then
		for i, v in ipairs (error_list) do error_list[i] = wrap_style ('parameter', v); end
		table.insert (error_list, wrap_style ('parameter', selected));
		set_message (error_condition, {make_sep_list (#error_list, error_list)});
	end
	return value, selected;
end

--[[-------------------------< R E M O V E _ W I K I _ L I N K >---------------]]
local function remove_wiki_link (str)
	return (str:gsub ("%[%[([^%[%]]*)%]%]", function(l)
		return l:gsub ("^[^|]*|(.*)$", "%1" ):gsub ("^%s*(.-)%s*$", "%1");
	end));
end

--[[-------------------------< I S _ W I K I L I N K >-------------------------]]
local function is_wikilink (str)
	local wl_type = 2;
	if not str:match ('^%[%[[^%]]+%]%]$') then return 0, str, ''; end
	local L, D = str:match ('^%[%[([^|]+)|([^%]]+)%]%]$');
	if not is_set (D) then
		D = str:match ('^%[%[([^%]]*)|*%]%]$');
		wl_type = 1; 
	end
	D = mw.text.trim (D, '%s|');
	return wl_type, D, L or '';
end

--[[--------------------------< S T R I P _ A P O S T R O P H E _ M A R K U P >]]
local function strip_apostrophe_markup (argument)
	if not is_set (argument) or nil == argument:find ("''", 1, true) then
		return argument, nil;
	end
	local flag;
	while true do
		if argument:find ("'''''", 1, true) then argument, flag = argument:gsub ("%'%'%'%'%'", "");
		elseif argument:find ("''''", 1, true) then argument, flag=argument:gsub ("%'%'%'%'", "");
		elseif argument:find ("'''", 1, true) then argument, flag=argument:gsub ("%'%'%'", "");
		elseif argument:find ("''", 1, true) then argument, flag = argument:gsub ("%'%'", "");
		else break; end
	end
	return argument, flag;
end

--[[--------------------------< S E T _ S E L E C T E D _ M O D U L E S >------]]
local function set_selected_modules (cfg_table_ptr)
	cfg = cfg_table_ptr;
end

return {
	add_maint_cat = add_maint_cat,
	add_prop_cat = add_prop_cat,
	error_comment = error_comment,
	has_accept_as_written = has_accept_as_written,
	hyphen_to_dash = hyphen_to_dash,
	in_array = in_array,
	is_set = is_set,
	is_wikilink = is_wikilink,
	make_sep_list = make_sep_list,
	make_wikilink = make_wikilink,
	remove_wiki_link = remove_wiki_link,
	safe_for_italics = safe_for_italics,
	select_one = select_one,
	set_message = set_message,
	set_selected_modules = set_selected_modules,
	strip_apostrophe_markup = strip_apostrophe_markup,
	substitute = substitute,
	wrap_style = wrap_style,
	z = z,
}