มอดูล:Infobox/utilities

require('strict');
local getArgs = require ('Module:Arguments').getArgs;
--=--------------------------------------------
created as a test function to figure out why the wikitext for |data36= in this version
of the template doesn't work in Salammbô when |title_orig= exists but does not have a value.  When that
happened, the infobox returned plain wikitext  which should have been a functioning link.
Placing that wikilink in the article body and previewing produced a working inter-language/inter-project link.
**
local function interlang_wikisource_link_make (frame)
	local lang_mod = require ('Module:Lang');									-- used to wrap non-English wikisource links in proper html markup
	local args_t = getArgs (frame);												-- parameter values that are emptyspace or only white space are converted to nil
	local orig_lang_code = args_t1;											-- 'all of them sensible everyday names'
	local native_wikisource = args_t2;
	local title_orig = args_t3;
	local name = args_t4;
	local orig_lang_name = mw.language.fetchLanguageName (orig_lang_code, 'en');	-- get the English name associated with ; only language tags known to MediaWiki allowed
	if  == orig_lang_name then												-- empty string when  invalid
		return 'แท็กภาษาไม่ถูกต้อง: ' .. orig_lang_code .. ;
	end
	local out_t = {};															-- output goes here
	table.insert (out_t, );													-- done with link; start label
	table.insert (out_t, title_orig or name or mw.title.getCurrentTitle().baseText);	-- insert the label
	table.insert (out_t, );													-- close inter-language/inter-project wikilink
	
	out_t = {lang_mod._lang ({orig_lang_code, table.concat (out_t)})};			-- replace content of out_t with a big string of its contents wrapped in 
	table.insert (out_t, ' ที่ ');												-- begin other language wikisource wikilinklink
	table.insert (out_t, orig_lang_name);										-- add the MediaWiki-known language name associated with |orig_lang_code=
	table.insert (out_t, ' วิกิซอร์ซ');									-- insert wikisource link static text
	
	return table.concat (out_t);												-- make a big string of all and done
end
------------------------------------------------------------------------------------
return true if code is one of the listed Chinese, Japanese, Korean ISO 639 codes, false else.
local function is_cjk_code (code)
local cjk =
		{
		'zh' = true, 'cdo' = true, 'cjy' = true, 'cmn' = true,			-- Chinese language codes
		'cpi' = true, 'cpx' = true, 'czh' = true, 'czo' = true,
		'gan' = true, 'hak' = true, 'hsn' = true, 'ltc' = true,
		'lzh' = true, 'mnp' = true, 'nan' = true, 'och' = true,
		'wuu' = true, 'yue' = true, 'zhx' = true,
		'ja' = true, 'jpx' = true, 'ojp' = true,							-- Japanese language codes
		'ko' = true, 'okm' = true, 'oko' = true,							-- Korean language codes
		}
	return cjkcode or false;
end
--italics=}}
local function set_italics (frame)
	local args=getArgs(frame); 
	local code = args1 or args'code' or ;									-- empty string causes 'yes' return;  will handle the missing code error
	local text = args2 or args'text' or ;									-- empty string causes 'yes' return;  will handle the missing text error
	local is_latn = require ("Module:Unicode data").is_Latin;
	
	code = code:gsub ('^(%a+).*', '%1');										-- strip subtags from IETF tag to leave just the language subtag
	if is_cjk_code (code) and not is_latn (text) then							-- is_latn() is in Module:Unicode data
		return  'no';															-- only case for 'no' 
	end
	return 'yes';																-- everything else is yes
end
--------------------------------------------------------------------------------------------------
compare function for result{} table descending sort
local function comp (a, b)
	return tonumber (a1) > tonumber (b1);
end
	
--occupation ref= as appropriate
	frame - calling frame required for expandTemplate()
	
returns sorted list on success; empty string else
local function sort_common (result, ref, frame)
	for i=#result, 1, -1 do
		if not tonumber (resulti1) then										-- if cannot be converted to a number
			table.remove (result, i);											-- delete
		end
	end
	
	if 0 == #result then														-- if we get here and the result table is empty
		return ;																-- abandon returning empty string
	end
	
	table.sort (result, comp);													-- sort what remains
	for i, v in ipairs (result) do
		resulti = table.concat (resulti);									-- make each table in result{} a string
	end
	result1 = table.concat ({result1, ref and ref or });					-- add reference(s) from | ref= to first item in the list
	
	return frame:expandTemplate { title = 'Unbulleted list', args = result};	-- render the unbulleted list
end
--------------------------------------------------------------------------
local function distribution_sort (frame)
	local args=getArgs(frame);
	
	local result = {															-- initialize; table will be sorted according to values in resultn1
		{args1, '% urban'},
		{args2, '% rural'},
	};
	
	return sort_common (result, args#result+1, frame);
end
------------------------------------------------------------------------------
local function ethnicity_sort (frame)
	local args=getArgs(frame);
	
	local result = {															-- initialize; table will be sorted according to values in resultn1
		{args1, '% White'},
		{args2, '% Black'},
		{args3, '% Asian'},
		{args4, '% Hispanic'},
		{args5, '% Native American'},
		{args6, '% Pacific Islander Americans'},
		{args7, '% Two or more races'},
		{args8, '% other'},													-- TODO: make other always last?
	};
	
	return sort_common (result, args#result+1, frame);
end
----------------------------------------------------------------------------
local function occupation_sort (frame)
	local args=getArgs(frame);
	
	local result = {															-- initialize; table will be sorted according to values in resultn1
		{args1, '% Blue-collar'},
		{args2, '% White-collar'},
		{args3, '% Gray-collar'},
	};
	
	return sort_common (result, args#result+1, frame)
end
----------------------------------------------------------------------
return {
	distribution_sort = distribution_sort,										-- 
	ethnicity_sort = ethnicity_sort,
	occupation_sort = occupation_sort,
	
	set_italics = set_italics,													-- 
	interlang_wikisource_link_make = interlang_wikisource_link_make,
	}