Module:Infobox/utilities
Appearance
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('strict');localgetArgs=require('Module:Arguments').getArgs;--[=[--------------------------< I N T E R L A N G _ W I K I S O U R C E _ L I N K _ M A K E >------------------created as a test function to figure out why the wikitext for |data36= in [[Special:Permalink/1236589613|this version]]of the template doesn't work in [[Salammbô]] when |title_orig= exists but does not have a value. When thathappened, the infobox returned plain wikitext [[s:fr:Salammbô|]] which should have been a functioning link.Placing that wikilink in the article body and previewing produced a working inter-language/inter-project link.{{#invoke:Infobox/utilities|interlang_wikisource_link_make|{{{orig_lang_code|}}}|{{{native_wikisource|}}}|{{{title_orig|}}}|{{{name|}}} }}]=]localfunctioninterlang_wikisource_link_make(frame)locallang_mod=require('Module:Lang');-- used to wrap non-English wikisource links in proper html markuplocalargs_t=getArgs(frame);-- parameter values that are emptyspace or only white space are converted to nillocalorig_lang_code=args_t[1];-- 'all of them sensible everyday names'localnative_wikisource=args_t[2];localtitle_orig=args_t[3];localname=args_t[4];localorig_lang_name=mw.language.fetchLanguageName(orig_lang_code,'en');-- get the English name associated with <orig_lang_code>; only language tags known to MediaWiki allowedif''==orig_lang_namethen-- empty string when <orig_lang_code> invalidreturn'<span style="color:#d33">invalid language tag: <span style="font-family:monospace">'..orig_lang_code..'</span></span>';endlocalout_t={};-- output goes heretable.insert(out_t,'[[s:');-- open inter-language/inter-project wikilinktable.insert(out_t,orig_lang_code);-- the language tagtable.insert(out_t,':');-- add the required separatortable.insert(out_t,native_wikisource);-- insert wikisource titletable.insert(out_t,'|');-- done with link; start labeltable.insert(out_t,title_origornameormw.title.getCurrentTitle().baseText);-- insert the labeltable.insert(out_t,']]');-- close inter-language/inter-project wikilinkout_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 {{lang}}table.insert(out_t,' at ');-- begin other language wikisource wikilinklinktable.insert(out_t,orig_lang_name);-- add the MediaWiki-known language name associated with |orig_lang_code=table.insert(out_t,' [[Wikisource]]');-- insert wikisource link static textreturntable.concat(out_t);-- make a big string of all and doneend--[[--------------------------< I S _ C J K _ C O D E >--------------------------------------------------------return true if code is one of the listed Chinese, Japanese, Korean ISO 639 codes, false else.]]localfunctionis_cjk_code(code)localcjk={['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}returncjk[code]orfalse;end--[[--------------------------< S E T _ I T A L I C S >--------------------------------------------------------Created for use with Template:Infobox book and Template:Infobox document and perhaps others to replace hard-codeditalic markup in the call to {{lang}}. This module attempts to make sure that {{lang}} correctly applies italicmarkup according to MOS:FOREIGNITALIC.|italics={{#invoke:Infobox/utilities|set_italics|{{{orig_lang_code|}}}|{{{title_orig}}}}}}}]]localfunctionset_italics(frame)localargs=getArgs(frame);localcode=args[1]orargs['code']or'';-- empty string causes 'yes' return; {{lang}} will handle the missing code errorlocaltext=args[2]orargs['text']or'';-- empty string causes 'yes' return; {{lang}} will handle the missing text errorlocalis_latn=require("Module:Unicode data").is_Latin;code=code:gsub('^(%a+).*','%1');-- strip subtags from IETF tag to leave just the language subtagifis_cjk_code(code)andnotis_latn(text)then-- is_latn() is in Module:Unicode datareturn'no';-- only case for 'no' endreturn'yes';-- everything else is yesend--[[--------------------------< C O M P >----------------------------------------------------------------------compare function for result{} table descending sort]]localfunctioncomp(a,b)returntonumber(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]]localfunctionsort_common(result,ref,frame)fori=#result,1,-1doifnottonumber(result[i][1])then-- if cannot be converted to a numbertable.remove(result,i);-- deleteendendif0==#resultthen-- if we get here and the result table is emptyreturn'';-- abandon returning empty stringendtable.sort(result,comp);-- sort what remainsfori,vinipairs(result)doresult[i]=table.concat(result[i]);-- make each table in result{} a stringendresult[1]=table.concat({result[1],refandrefor''});-- add reference(s) from |<list> ref= to first item in the listreturnframe:expandTemplate{title='Unbulleted list',args=result};-- render the unbulleted listend--[[--------------------------< 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|}}} }}]]localfunctiondistribution_sort(frame)localargs=getArgs(frame);localresult={-- initialize; table will be sorted according to values in result[n][1]{args[1],'% urban'},{args[2],'% rural'},};returnsort_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|}}} }}]]localfunctionethnicity_sort(frame)localargs=getArgs(frame);localresult={-- initialize; table will be sorted according to values in result[n][1]{args[1],'% [[White Americans|White]]'},{args[2],'% [[African Americans|Black]]'},{args[3],'% [[Asian Americans|Asian]]'},{args[4],'% [[Hispanic and Latino Americans|Hispanic]]'},{args[5],'% [[Native Americans in the United States|Native American]]'},{args[6],'% [[Pacific Islander Americans]]'},{args[7],'% [[Multiracial Americans|Two or more races]]'},{args[8],'% other'},-- TODO: make other always last?};returnsort_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|}}} }}]]localfunctionoccupation_sort(frame)localargs=getArgs(frame);localresult={-- 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]]'},};returnsort_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,-- {{Infobox U.S. congressional district}}ethnicity_sort=ethnicity_sort,occupation_sort=occupation_sort,set_italics=set_italics,-- {{Infobox book}}interlang_wikisource_link_make=interlang_wikisource_link_make,}