Difference between revisions of "Module:Infobox/utilities"
Jump to navigation
Jump to search
test>Trappist the monk |
test>Trappist the monk (+distribution_sort() & occupation_sort();) |
||
| Line 13: | Line 13: | ||
end | end | ||
| + | |||
| + | --[[--------------------------< S O R T _ C O M M O N >-------------------------------------------------------- | ||
| + | |||
| + | common function to render sorted distribution, ethnicity, and occupation lists. | ||
| + | |||
| + | inputs: | ||
| + | result - table of percentages and labels | ||
| + | ref - value from |distribution ref=, |ethnicity ref=, or |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 (result[i][1]) 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 | ||
| + | result[i] = table.concat (result[i]); -- make each table in result{} a string | ||
| + | end | ||
| + | |||
| + | result[1] = table.concat ({result[1], ref and ref or ''}); -- add reference(s) from |<list> ref= to first item in the list | ||
| + | |||
| + | return frame:expandTemplate { title = 'Unbulleted list', args = result}; -- render the unbulleted list | ||
| + | end | ||
| + | |||
| + | |||
| + | --[[--------------------------< D I S R I B U T I O N _ S O R T >---------------------------------------------- | ||
| + | |||
| + | {{#invoke:Infobox/utilities|distribution_sort|{{{percent urban|}}}|{{{percent rural|}}}|{{{distribution ref|}}} }} | ||
| + | |||
| + | ]] | ||
| + | |||
| + | local function distribution_sort (frame) | ||
| + | local args=getArgs(frame); | ||
| + | |||
| + | local result = { -- initialize; table will be sorted according to values in result[n][1] | ||
| + | {args[1], '% urban'}, | ||
| + | {args[2], '% rural'}, | ||
| + | }; | ||
| + | |||
| + | return sort_common (result, args[#result+1], frame); | ||
| + | end | ||
| + | |||
--[[--------------------------< E T H N I C I T Y _ S O R T >-------------------------------------------------- | --[[--------------------------< E T H N I C I T Y _ S O R T >-------------------------------------------------- | ||
| − | {{#invoke: | + | {{#invoke:Infobox/utilities|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|}}} }} |
]] | ]] | ||
| Line 34: | Line 88: | ||
}; | }; | ||
| − | + | return sort_common (result, args[#result+1], frame); | |
| − | + | end | |
| − | + | ||
| − | + | ||
| − | + | --[[--------------------------< O C C U P A T I O N _ S O R T >------------------------------------------------ | |
| − | + | ||
| − | + | {{#invoke:Infobox/utilities|distribution_sort|{{{percent blue collar|}}}|{{{percent white collar|}}}|{{{percent grey collar|}}}|{{{occupation ref|}}} }} | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | ]] | |
| − | |||
| − | |||
| − | + | local function occupation_sort (frame) | |
| + | local args=getArgs(frame); | ||
| − | + | local result = { -- initialize; table will be sorted according to values in result[n][1] | |
| + | {args[1], '% [[Blue-collar worker|Blue-collar]]'}, | ||
| + | {args[2], '% [[White-collar worker|White-collar]]'}, | ||
| + | {args[3], '% [[Gray-collar]]'}, | ||
| + | }; | ||
| + | |||
| + | return sort_common (result, args[#result+1], frame) | ||
end | end | ||
| Line 61: | Line 116: | ||
return { | return { | ||
| + | distribution_sort = distribution_sort, | ||
ethnicity_sort = ethnicity_sort, | ethnicity_sort = ethnicity_sort, | ||
| + | occupation_sort = occupation_sort, | ||
} | } | ||
Revision as of 11:36, 23 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 sameethnicity_sort()– sorts the various ethnicity parameters and renders an unbulleted list of sameoccupation_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 italicizedis_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
--[[--------------------------< S O R T _ C O M M O N >--------------------------------------------------------
common function to render sorted distribution, ethnicity, and occupation lists.
inputs:
result - table of percentages and labels
ref - value from |distribution ref=, |ethnicity ref=, or |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 (result[i][1]) 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
result[i] = table.concat (result[i]); -- make each table in result{} a string
end
result[1] = table.concat ({result[1], ref and ref or ''}); -- add reference(s) from |<list> ref= to first item in the list
return frame:expandTemplate { title = 'Unbulleted list', args = result}; -- render the unbulleted list
end
--[[--------------------------< D I S R I B U T I O N _ S O R T >----------------------------------------------
{{#invoke:Infobox/utilities|distribution_sort|{{{percent urban|}}}|{{{percent rural|}}}|{{{distribution ref|}}} }}
]]
local function distribution_sort (frame)
local args=getArgs(frame);
local result = { -- initialize; table will be sorted according to values in result[n][1]
{args[1], '% urban'},
{args[2], '% rural'},
};
return sort_common (result, args[#result+1], frame);
end
--[[--------------------------< E T H N I C I T Y _ S O R T >--------------------------------------------------
{{#invoke:Infobox/utilities|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);
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'}, -- TODO: make other always last?
};
return sort_common (result, args[#result+1], frame);
end
--[[--------------------------< O C C U P A T I O N _ S O R T >------------------------------------------------
{{#invoke:Infobox/utilities|distribution_sort|{{{percent blue collar|}}}|{{{percent white collar|}}}|{{{percent grey collar|}}}|{{{occupation ref|}}} }}
]]
local function occupation_sort (frame)
local args=getArgs(frame);
local result = { -- initialize; table will be sorted according to values in result[n][1]
{args[1], '% [[Blue-collar worker|Blue-collar]]'},
{args[2], '% [[White-collar worker|White-collar]]'},
{args[3], '% [[Gray-collar]]'},
};
return sort_common (result, args[#result+1], frame)
end
--[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
]]
return {
distribution_sort = distribution_sort,
ethnicity_sort = ethnicity_sort,
occupation_sort = occupation_sort,
}