---
-- This template implements
---
local p = {}
local lang = mw.getContentLanguage
local Date -- lazy initialization  
local function ifexist(page)
if not page then return false end
if mw.title.new(page).exists then return true end
return false
end  
local function isempty( s )
return not s or s:match( '^%s*(.-)%s*$' ) == ''
end  
local function splitnumandref( s )
s = s:match( '^%s*(.-)%s*$' )
local t1 = mw.text.unstrip(s)
local t2 = s:match( '^([%d][%d,]*)' )
if( t1 == t2 ) then
local t3 = s:match( '^[%d][%d,]*(.-)$' )
return t1, t3
else
return s, ''
end
end  
local function formatnumR(num)
return tonumber(lang:parseFormattedNumber(num))
end  
local function formatnum(num)
return lang:parseFormattedNumber(num) and lang:formatNum(lang:parseFormattedNumber(num)) or num
end  
-- this function creates an array with the {year, population, percent change}
local function getpoprow(year, popstr, pyear, ppopstr, linktype, percentages, current_year)
local pop, popref = splitnumandref( popstr or '')
local ppop, ppopref = splitnumandref( ppopstr or '')
local percent = ''
local yearnum = formatnumR(mw.ustring.gsub(year or *, '^%s*([%d][%d][%.%d]+).*$', '%1') or *)
local pyearnum = formatnumR(mw.ustring.gsub(pyear or *, '^%s*([%d][%d][%.%d]+).*$', '%1') or *)
local popnum = formatnumR(pop)
local ppopnum = formatnumR(ppop)
if( linktype == 'US' or linktype == 'USA' ) then
if( (yearnum or 0) >= 1790 and yearnum <= current_year and math.fmod(math.floor(yearnum), 10) == 0) then
if( yearnum < current_year ) then
year = * .. year .. *
elseif( ifexist(tostring(yearnum) .. ' United States census') ) then
year = * .. year .. *
end
end
end
if(percentages ~= 'off') then
local pstr = '—&nbsp;&nbsp;&nbsp;&nbsp;'
if(popnum ~= nil and ppopnum ~= nil and (ppopnum > 0)) then
if(percentages == 'pagr') then
pstr = mw.ustring.format('%.2f', 100*math.abs(math.pow(popnum/ppopnum,1/(yearnum-pyearnum)) - 1))
elseif(percentages == 'monthly') then
if Date == nil then Date = require('Module:Date')._Date end
local date1 = Date(year)
local date2 = Date(pyear)
local diff = date1 - date2
local months = (diff.age_days/(365.25/12))
pstr = mw.ustring.format('%.2f', 100*math.abs(math.pow(popnum/ppopnum,1/months) - 1))
else
pstr = mw.ustring.format('%.1f', 100*math.abs(popnum/ppopnum - 1))
end
if( popnum < ppopnum ) then
pstr = '−' .. pstr .. '%'
else
pstr = '+' .. pstr .. '%'
end
elseif(popnum ~= nil and ppopnum ~= nil and (ppopnum == popnum)) then
pstr = mw.ustring.format('%.2f', 0) .. '%'
end
percent = pstr
end
-- strip the fractional part of the year, if there is one
year = mw.ustring.gsub(year or '', '^%s*([%d][%d][%d]+)%.[%d]*', '%1')  
return {year, formatnum(pop) .. popref, percent }
end  
-- this function creates an array with table header labels
local function getheadrow(percentages, popname, yearname, percentname)
-- year cell
if(yearname == '') then
yearname = 'ஆண்டு'
end
-- population cell
if(popname == '') then
popname = '<abbr title="மக்கள்தொகை">ம.தொ.</abbr>'
end  
-- percentages cell
if( percentages ~= 'off' and percentname == '') then
if( percentages == 'pagr' ) then
percentname = '<abbr title="ஆண்டுக்கு வளர்ச்சி விகிதம்">ஆ. ±%</abbr>'
elseif( percentages == 'monthly' ) then
percentname = '<abbr title=" மாதம் ஒன்றுக்கு வளர்ச்சி விகிதம்">மா. ±%</abbr>'
else
percentname = '<abbr title="விழுக்காடு மாற்றம்">±%</abbr>'
end
end
return {yearname, popname, percentname}
end  
-- this function builds the json for the population graph
local function graphjson(data, gwidth, gheight, gtype)
local yearcount = #data
local graphargs = {
['width'] = gwidth,
['height'] = gheight,
['type'] = gtype or 'line',
['yAxisTitle'] = 'மக்கள்தொகை',
['yAxisMin'] = 0,
['xAxisTitle'] = 'ஆண்டு',
['xAxisAngle'] = '-45',
['yGrid'] = 'y',
['yAxisFormat'] = ',d',
['x'] = '',
['y'] = ''
}
local firstpoint = true
for offset = 1,yearcount do
local x,y = data[offset][1], data[offset][2]
-- delink if necessary
if x:match('^%s*%[%^%[%*%|([^%[%]]*)%]%]') then
x = x:match('^%s*%[%^%[%*%|([^%[%]]*)%]%]')
end
y = formatnumR(y)
if x and y then
graphargs['x'] = graphargs['x'] .. (firstpoint and '' or ', ') .. x
graphargs['y'] = graphargs['y'] .. (firstpoint and '' or ', ') .. y
firstpoint = false
end
end
local Graph = require('Module:Graph')  
return Graph.chart({args = graphargs})
end  
local function rendergraph(, data, gwidth, gheight, gthumb, gtype)
local graph = :extensionTag{
name = 'templatestyles',
args = { src = 'Graph:Chart/styles.css'}
} .. :extensionTag{name = 'graph', content = graphjson(data, gwidth, gheight, gtype)}  
if(gthumb ~= '') then
local graphdiv = mw.html.create('div')
:addClass('')
:addClass(gthumb == '' and 'tright' or 'tleft')
:css('clear', '')
graphdiv
:tag('div')
:addClass('thumbinner')
:wikitext(graph)
return tostring(graphdiv)
end  
return '<div>' .. graph .. '</div>'
end  
-- this function renders the population table in a vertical format
local function rendervertical(data, head, title, footnote, alignfn, class, style, width, shading, percol, cols, graphpos, graph)
-- define a couple helper functions
local function addrowcell(trow, tag, text, align, shading, style)
cell = trow:tag(tag)
cell
:css('text-align', align)
:css('padding', '')
:wikitext(text)
:css('-bottom', shading ~= 'off' and ' solid #bbbbbb' or nil)
:cssText(style)
end
local function addheadcell(trow, text, align, width, pad)
cell = trow:tag('th')
cell
:css('-bottom', ' solid black')
:css('padding', pad and (' ' .. pad) or '')
:css('text-align', align)
:css('width', width)
:wikitext(text)
end  
local colspan = 3
local yearcount = #data
local argcount = 2*yearcount  
if( isempty(width) ) then
end  
-- override the value of cols if percol has been specified
if( percol > 0 ) then
cols = math.floor( (yearcount - 1) / percol ) + 1
end  
-- compute the number of rows per col
local rowspercol = math.floor( (yearcount - 1) / cols ) + 1  
-- specify the colspan for the title and footer lines
if( cols > 1 ) then
colspan = cols
else
if (head[3] == '') then
colspan = 2
else
colspan = 3
end
end  
-- compute outer table width
local twidth = width
if( (cols > 1) and width:match('^%s*[%d]+[%w]+%s*$') ) then
local widthnum = mw.ustring.gsub( width, '^%s*([%d]+)([%w]+)%s*$', '%1' )
local widthunit = mw.ustring.gsub( width, '^%s*([%d]+)([%w]+)%s*$', '%2' )
twidth = tostring(widthnum*cols) .. widthunit
end  
-- create the outer table
local root = mw.html.create('table')
root
:addClass(class)
:css('width', twidth)
:css('-top-width', '0')
:cssText(style['table'])
-- add title
local caption = root:tag('caption')
caption
:css('-top', ' #aaa solid')
:css('-', ' #aaa solid')
:css('-', ' #aaa solid')
:css('background-color', 'lavender')
:css('padding', '0.25em')
:css('font-weight', 'bold')
:wikitext(title)  
-- add the graph line (if top graph)
if((graphpos == 'top' or graphpos == 't') and graph ~= '') then
row = root:tag('tr')
cell = row:tag('td')
cell
:attr('colspan', colspan)
:css('-bottom', ' solid black')
:wikitext(graph)
graph = ''
end  
-- loop over columns and rows within columns
local offset = 1
local t = root
for c = 1,cols do
-- add inner tables if we are rendering more than one column
if( cols > 1) then
if (c == 1) then
row = root:tag('tr')
row:attr('valign', 'top')
cell = row:tag('td')
cell:css('padding', '0 0.5em')
else
cell = row:tag('td')
cell
:css('padding', '0 0.5em')
:css('-', 'solid #aaa')
end
t = cell:tag('table')
t
:css('-spacing', '0')
:css('width', width)
end
-- start column headers
local hrow = t:tag('tr')
hrow:css('font-size', '95%')
-- year header
addheadcell(hrow, head[1], nil, head[3] ~= '' and '3em' or 'auto', nil, nil)
-- population header
addheadcell(hrow, head[2], *, nil, *)
-- percentages header
if( head[3] ~= '' ) then
addheadcell(hrow, head[3], '', nil, nil)
end
-- end column headers
-- start population rows
for r = 1,rowspercol do
-- generate the row if we have not exceeded the rowcount
-- shade every fifth row, unless shading = off
local s = 'off'
if( math.fmod((c - 1)*rowspercol + r, 5) == 0 and r ~= rowspercol) then