Difference between revisions of "Module:Infobox/utilities"

From blackwiki
Jump to navigation Jump to search
test>Trappist the monk
m (Trappist the monk moved page Module:Sandbox/trappist the monk/ethnicity sort to Module:Infobox/utilities without leaving a redirect: get it out of my sandbox)
test>Trappist the monk
Line 21: Line 21:
  
 
local function ethnicity_sort (frame)
 
local function ethnicity_sort (frame)
local args=getArgs(frame);
+
local args=getArgs(frame);
 +
 +
if 0 == #args then -- if there are no args, nothing to do
 +
return ''; -- so abandon, returning empty string
 +
end
 +
 
local result = { -- initialize; table will be sorted according to values in result[n][1]
 
local result = { -- initialize; table will be sorted according to values in result[n][1]
 
{args[1], '% [[White people|white]]'},
 
{args[1], '% [[White people|white]]'},
Line 43: Line 48:
 
result[i] = table.concat (result[i]); -- make each table in result{} a string
 
result[i] = table.concat (result[i]); -- make each table in result{} a string
 
end
 
end
+
 
 
result[1] = table.concat ({result[1], args[9] and args[9] or ''});
 
result[1] = table.concat ({result[1], args[9] and args[9] or ''});
 
 

Revision as of 17:41, 22 July 2020

This module is intended to be a repository for miscellaneous utility functions that support various infoboxes.

  • for {{Infobox U.S. congressional district}}
    • distribution_sort() – sorts the various distribution parameters and renders an unbulleted list of same
    • ethnicity_sort() – sorts the various ethnicity parameters and renders an unbulleted list of same
    • occupation_sort() – sorts the various occupation parameters and renders an unbulleted list of same
  • for {{Infobox book}}
    • set_italics() – used to ensure that book-titles in cjk scripts are not italicized
      • is_cjk_code() – (private) returns true when language code represents a cjk language

require('Module:No globals');
local getArgs = require ('Module:Arguments').getArgs;


--[[--------------------------< C O M P >----------------------------------------------------------------------

compare function for result{} table descending sort

]]

local function comp (a, b)
	return tonumber (a[1]) > tonumber (b[1]);
end
	

--[[--------------------------< E T H N I C I T Y _ S O R T >--------------------------------------------------

{{#invoke:Sandbox/trappist the monk/ethnicity sort|ethnicity_sort|{{{percent white|}}}|{{{percent black|}}}|{{{percent asian|}}}|{{{percent hispanic|}}}|{{{percent native american|}}}|{{{percent native hawaiian|}}}|{{{percent more than one race|}}}|{{{percent other race|}}}|{{{ethnicity ref|}}} }}

]]

local function ethnicity_sort (frame)
	local args=getArgs(frame);
	
	if 0 == #args then															-- if there are no args, nothing to do
		return '';																-- so abandon, returning empty string
	end
	
	local result = {															-- initialize; table will be sorted according to values in result[n][1]
		{args[1], '% [[White people|white]]'},
		{args[2], '% [[Black people|black]]'},
		{args[3], '% [[Asian Americans|Asian]]'},
		{args[4], '% [[Hispanic]]'},
		{args[5], '% [[Native Americans in the United States|Native American]]'},
		{args[6], '% [[Pacific Islands Americans]]'},
		{args[7], '% [[Two or more races]]'},
		{args[8], '% other'},
	};
	
	for i=8, 1, -1 do
		if not tonumber (result[i][1]) then										-- if cannot be converted to a number
			table.remove (result, i);											-- delete
		end
	end
	table.sort (result, comp);													-- sort what remains

	for i, v in ipairs (result) do
		result[i] = table.concat (result[i]);									-- make each table in result{} a string
	end

	result[1] = table.concat ({result[1], args[9] and args[9] or ''});
	
	return frame:expandTemplate { title = 'Unbulleted list', args = result};	-- render the unbulleted list
end


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

]]

return {
	ethnicity_sort = ethnicity_sort,
	}