Module:Historical populations
Appearance
![]() | This module uses TemplateStyles: |
This module implements {{historical populations}}. Please see the template page for documentation.
--
-- This template implements {{Historical populations}}
--
local p = {}
local function page_exists( title )
-- This function implements #ifexist
local noError, titleObject = pcall(mw.title.new, title)
if not noError then
return false
else
if titleObject then
return titleObject.exists
else
return false
end
end
end
local function formatnum(num)
return frame:callParserFunction{ name = 'formatnum', args = num }
end
local function formatnumR(num)
return frame:callParserFunction{ name = 'formatnum', args = {num, 'R'} }
end
local function isempty( s )
return not s or s:match( '^%s*(.-)%s*$' ) == ''
end
local function popheader(percentages, popname)
local percent = ''
local yearwidth = '20%'
if( popname == '' ) then
popname = '<abbr title="Population">Pop.</abbr>'
end
if( percentages == 'off' ) then
yearwidth = 'auto'
else
local pstr = ''
if( percentages == 'pagr' ) then
pstr = '<abbr title="Per annum growth rate">±% p.a.</abbr>'
else
pstr = '<abbr title="Percent change">±%</abbr>'
end
percent = '<th style="border-bottom: 1px solid black; padding: 1px; text-align: right;">' .. pstr .. ' </th>'
end
return mw.ustring.formt( [==[
<tr style="font-size: 95%%"><th style="border-bottom: 1px solid black; padding: 1px; width: %s;">Year</th>
<th style="border-bottom: 1px solid black; padding: 1px; text-align: right;">%s </th>%s
</tr>]==], yearwidth, popname, percent)
end
local function poprow( year, pop, pyear, ppop, linktype, percentages, shading)
local percent = ''
local yearnum = tonumber(formatnumR(mw.ustring.gsub(year, '^([%d]+).*$', '%1') or ''))
local pyearnum = tonumber(formatnumR(mw.ustring.gsub(pyear, '^([%d]+).*$', '%1') or ''))
local popnum = tonumber(formatnumR(pop or ''))
local ppopnum = tonumber(formatnumR(ppop or ''))
if( linktype == 'US' or linktype == 'USA' ) then
if( page_exists(tostring(yearnum) .. ' United States Census') ) then
year = '[[' .. tostring(yearnum) .. ' United States Census|' .. tostring(yearnum) .. ']]'
end
end
if( shading == 'off' ) then
shading = ''
else
shading = ' border-bottom: 1px solid #bbbbbb;'
end
if(percentages ~= 'off') then
local pstr = '— '
if(popnum ~= nil and ppopnum ~= nil) then
if(percentages == 'pagr') then
pstr = mw.ustring.format('%f.2', 100*(math.pow(popnum/ppopnum,1/(yearnum-pyearnum)) - 1))
else
pstr = mw.ustring.format('%f.1', 100*math.abs(popnum/ppopnum - 1))
if( popnum < ppopnum ) then
pstr = '−' .. pstr
else
pstr = '+' .. pstr
end
end
end
percent = '<td style="text-align: right; padding: 1px;' .. shading .. '">' .. pstr .. '%</td>'
end
return mw.ustring.format( [==[
<tr>
<th style="text-align: center; padding: 1px;%s">%s</th>
<td style="text-align: right; padding: 1px;%s">%s</td>%s
</tr>]==], shading, year, shading, formatnum(tonumber(pop)) or pop, percent)
end
function p.poptable(frame)
local t = {}
local args = frame:getParent().args
local title = args['title'] or 'Historical population'
local align = args['align'] or 'right'
local clear = args['clear'] or align
local percentages = args['percentages'] or ''
local state = args['state'] or ''
local linktype = args['type'] or ''
local shading = args['shading'] or 'on'
local width = args['width'] or '15em'
local widthnum = mw.ustring.gsub( width, '^%s*([%d]+)([%w]+)%s*$', '%1' )
local widthunit = mw.ustring.gsub( width, '^%s*([%d]+)([%w]+)%s*$', '%2' )
local popname = args['pop_name'] or ''
local footnote = args['footnote'] or ''
local source = args['source'] or ''
local cols = tonumber(args['cols']) or 1
local colspan = 3
local class = 'toccolours'
if( state == 'collapsed' ) then
class = class .. ' collapsible collapsed'
end
local margin = '0 0 1em 1em'
if( align == 'left' ) then
margin = '0 1em 1em 0'
else
if( align == 'none' ) then
margin = '0.5em 1em 0.5em 0'
end
end
if( source ~= '' ) then
source = 'Source: ' .. source
if( footnote ~= '' ) then
footnote = footnote .. '<br/>'
end
end
if( cols > 1 ) then
colspan = cols
else
if (percentages == 'off') then
colspan = 2
else
colspan = 3
end
end
local rowcount = 0
local row = 1
while( not isempty(args[2*row - 1]) ) do
rowcount = rowcount + 1
row = row + 1
end
local head = mw.ustring.format([==[
<table class="%s" style="clear: %s; width: %d%s; text-align: center; border-spacing: 0; float: %s; margin: %s">
<tr>
<th colspan="%d" class="navbox-title" style="font-size:110%;">%s</th>
</tr>]==], class, clear, widthnum*cols, widthunit, align, margin, colspan, title)
local foot = ''
if( footnote ~= '' or source ~= '' ) then
foot = mw.ustring.format([==[
<tr>
<td colspan="%d" style="border-top: 1px solid black; font-size: 85%; text-align: left;">%s%s</td>
</tr>]==], colspan, footnote, source)
end
local perrow = math.floor( (rowcount - 1) / cols ) + 1
if( cols > 1 ) then
t.insert(t, '<tr valign="top">')
t.insert(t, '<td style="padding:0 0.5em;"><table style="border-spacing:0; width:' .. width .. '">')
end
t.insert(t, popheader(percentages, popname))
for c = 1,cols do
if( c > 1 ) then
t.insert(t, '<td style="padding:0 0.5em; border-left:solid 1px #aaa"><table style="border-spacing:0; width:' .. width .. '">')
t.insert(t, popheader(percentages, popname) )
end
for r = 1,perrow do
row = perrow*(c - 1) + r
if(row <= rowcount) then
local s = 'off'
if( math.mod(row, 5) == 0 ) then
local s = shading
end
t.insert(t, poprow( args[2*row - 1], args[2*row], args[2*row - 3] or '', args[2*row - 2] or '', linktype, percentages, s) )
else
if( percentages == 'off' ) then
t.insert(t, '<tr><th></th><td></td></tr>')
else
t.insert(t, '<tr><th></th><td></td><td></td></tr>')
end
end
end
if( c > 1 ) then
t.insert(t, '</table></td>')
end
end
return head .. '\n' .. table.concat( t ) .. '\n' .. foot
end