وحدة:Cs1 lang lister

من ويكيبيديا، الموسوعة الحرة
اذهب إلى التنقل اذهب إلى البحث
توثيق الوحدة[أنشئ] [استخدامات] [قوالب]
يمكن إنشاء توثيق الوحدة في صفحة الشرح
--[[-------------------------< L I S T E R >-------------------------------------------------------------------

adds code/name pair to code_list and name/code pair to name_list; code/name pairs in override_list replace those
taken from the MediaWiki list; these are marked with a superscripted dagger.

]]

local function add_to_list (code_list, name_list, override_list, code, name)
	if override_list[code] then													-- look in the override table for this code
		code_list[code] = override_list[code] .. '<sup>†</sup>';				-- use the name from the override table; mark with dagger
		name_list[override_list[code]] = code .. '<sup>†</sup>';
	else
		code_list[code] = name;													-- use the MediaWiki name and code
		name_list[name] = code;
	end
end


--[[-------------------------< L I S T _ F O R M A T >---------------------------------------------------------

formats key/value pair into a string for rendering
	['k'] = 'v'	→ k: v

]]

local function list_format (result, list)
	for k, v in pairs (list)	do
		table.insert (result, k .. ': ' .. v);
	end
end


--[[-------------------------< L I S T E R >-------------------------------------------------------------------

Module entry point

Crude documentation tool that returns one of several lists of language codes and names.

{{#invoke:cs1|lang_lister|lister|list=<selector>}}

where <selector> is one of the values:
	2char – list of ISO 639-1 codes and names sorted by code
	3char – list of ISO 639-2, -3 codes and names sorted by code
	ietf – list of IETF language tags and names sorted by tag -- not supported by cs1|2 |language= parameter
	name – list of language names and codes sorted by name -- IETF tags omitted because not supported by cs1|2 |language= parameter

]]

local function lister (frame)
	local source_list = mw.language.fetchLanguageNames(mw.getContentLanguage():getCode(), 'all');
	local override = require ("Module:Citation/CS1/Configuration").lang_code_remap;
	local code_1_list={};
	local code_2_list={};
	local ietf_list={};
	local name_list={};
	
	if not ({['2char']=true, ['3char']=true, ['ietf']=true, ['name']=true})[frame.args.list] then
		return '<span style="font-size:100%" class="error">unknown list selector: ' .. frame.args.list .. '</span>';
	end

--	if 'ietf' == frame.args.list then											-- ietf codes not currently supported by cs1|2 |language= parameter
--		return '<span style="font-size:100%" class="error">ietf language tags not supported by cs1|2</span>'
--	end

	for code, name in pairs (source_list) do
		if 2 == code:len() then
			add_to_list (code_1_list, name_list, override, code, name);
		elseif 3 == code:len() then
			add_to_list (code_2_list, name_list, override, code, name);
		else																	-- ietf codes not currently supported by cs1|2 |language= parameter
			add_to_list (ietf_list, name_list, override, code, name);
		end
	end
	
	local result = {};
	local out = {};

	if '2char' == frame.args.list then
		list_format (result, code_1_list);
	elseif '3char' == frame.args.list then
		list_format (result, code_2_list);
	elseif 'ietf' == frame.args.list then
		list_format (result, ietf_list);
	else																		--must be 'name'
		list_format (result, name_list);
	end
	
	table.sort (result);
	table.insert (result, 1, '<div class="div-col columns column-width" style="column-width:20em">');
	table.insert (out, table.concat (result, '\n*'));
	table.insert (out, '</div>');
	
	return table.concat (out, '\n');

end


--[[-------------------------< E X P O R T E D   F U N C T I O N S >------------------------------------------
]]

return {
	lister = lister,
	};