-- 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
s = shading
end
if(offset <= yearcount) then
-- start population row
local prow = t:tag('tr')
-- year cell
addrowcell(prow, 'th', data[offset][1], '', s, style['year'])
-- population cell
addrowcell(prow, 'td', data[offset][2], '', s, style['pop'])
-- percentage cell
if( not isempty(head[3]) ) then
addrowcell(prow, 'td', data[offset][3], '', s, style['pct'])
end
-- end population row
offset = offset + 1
end
end
end  
-- add the graph line (if bottom graph)
if((graphpos == 'bottom' or graphpos == 'b') and graph ~= '') then
row = root:tag('tr')
cell = row:tag('td')
cell
:attr('colspan', colspan)
:css('-top', ' solid black')
:wikitext(graph)
graph = ''
end
-- add the footnote line
if( footnote ~= '') then
row = root:tag('tr')
cell = row:tag('td')
cell
:attr('colspan', colspan)
:css('-top', ' solid black')
:css('font-size', '85%')
:css('text-align', alignfn)
:wikitext(footnote)
end  
return graph .. tostring(root)
end  
-- this function renders the population table in a horizontal format
local function renderhorizontal(data, head, title, footnote, alignfn, class, style, width, shading, perrow, rows, graphpos, graph)
local row
local cell
local yearcount = #data
local argcount = 2*yearcount  
-- override the value of rows if perrow has been specified
if( perrow > 0 ) then
rows = math.floor( (yearcount - 1) / perrow ) + 1
end  
-- compute the number of cols per row
local colsperrow = math.floor( (yearcount - 1) / rows ) + 1  
-- create the outer table
local root = mw.html.create('table')
root
:addClass(class)
:css('font-size', '90%')
:cssText(style['table'])
-- create title row
row = root:tag('tr')
cell = row:tag('th')
cell
:css('padding', '0.25em')
:attr('colspan', colsperrow + 1)
: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', colsperrow + 1)
:css('-bottom', ' solid black')
:wikitext(graph)
graph = ''
end  
-- loop over rows and columns within rows
local offset = 1
for r = 1,rows do
local rowoffset = offset
-- render the years
row = root:tag('tr')
cell = row:tag('th')
cell:wikitext(head[1])
:css('-top', r > 1 and ' solid #000' or nil)
for c = 1,colsperrow do
cell = row:tag('td')
if(offset <= yearcount) then
cell:wikitext(data[offset][1])
:css('text-align', '')
:css('-top', r > 1 and ' solid #000' or nil)
:cssText(style['year'])
else
cell:css('-width', r > 1 and ' 0 0 0' or 0)
:css('-top', r > 1 and ' solid #000' or nil)
end
offset = offset + 1
end
-- render the pop
offset = rowoffset
row = root:tag('tr')
cell = row:tag('th')
cell:wikitext(head[2])
for c = 1,colsperrow do
cell = row:tag('td')
if(offset <= yearcount) then
cell:wikitext(data[offset][2])
:css('text-align', '')
:css('padding-', '')
:cssText(style['pop'])
else
cell:css('-width', 0)
end
offset = offset + 1
end
-- render the percentages
if(head[3] ~= '') then
offset = rowoffset
row = root:tag('tr')
cell = row:tag('th')
cell:wikitext(head[3])
for c = 1,colsperrow do
cell = row:tag('td')
if(offset <= yearcount) then
cell:wikitext(data[offset][3])
:css('text-align', '')
:css('padding-', '')
:cssText(style['pct'])
else
cell:css('-width', 0)
end
offset = offset + 1
end
end
end  
-- add the graph line (if bottom graph)
if((graphpos == 'bottom' or graphpos == 'b') and graph ~= '') then
row = root:tag('tr')
cell = row:tag('td')
cell
:attr('colspan', colsperrow + 1)
:css('-top', ' solid black')
:wikitext(graph)
graph = ''
end  
-- add the footnote line
if( footnote ~= '') then
row = root:tag('tr')
cell = row:tag('td')
cell
:css('-top', ' solid black')
:css('font-size', '85%')
:css('text-align', alignfn)
:attr('colspan', colsperrow + 1)
:wikitext(footnote)
end  
return graph .. tostring(root)
end  
-- this is the main function
function p.poptable
local data = {}
local style = {}
local args = .args[1] and .args or :getParent.args  
local title = args['title'] or ''
local align = args['align'] or ''
local clear = args['clear'] or ''
local direction = args['direction'] or ''
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 ''
local subbox = args['subbox'] or ''
local popname = args['pop_name'] or ''
local yearname = args['year_name'] or ''
local percentname = args['percent_name'] or ''
local footnote = args['footnote'] or ''
local alignfn = args['align-fn'] or ''
local source = args['source'] or ''
local graphpos = args['graph-pos'] or ''
local graphwidth = args['graph-width'] or ''
local graphheight = args['graph-height'] or ''
local graphtype = args['graph-type'] or 'line'
local percol = tonumber(args['percol']) or 0
local cols = tonumber(args['cols']) or 1
local perrow = tonumber(args['perrow']) or 0
local rows = tonumber(args['rows']) or 1
style['year'] = args['year_style']
style['pop'] = args['pop_style']
style['pct'] = args['pct_style']  
-- setup classes and styling for outer table
local class = direction == 'horizontal' and 'wikitable' or 'toccolours'
if( state == 'collapsed' ) then
class = class .. ' collapsible collapsed'
end  
if( isempty(title) ) then
title = 'மக்கள்தொகை வளர்ச்சி'
end  
if( isempty(align) ) then
align = direction ~= 'horizontal' and * or *
end  
if( isempty(alignfn) ) then
alignfn = ''
end  
if( isempty(clear) ) then
clear = align == * and * or align
end  
local margin = '0.5em 0 1em 0.5em'
if( align == '' ) then
margin = '0.5em 1em 0.5em 0'
elseif( align == '' ) then
margin = '0.5em 1em 0.5em 0'
elseif( align == '' ) then
margin = '0.5em auto'
end  
if( isempty(subbox) ) then
style['table'] =
'-spacing: 0;' ..
(align ~= * and 'float:' .. align .. ';' or *) ..
(clear ~= * and 'clear:' .. clear .. ';' or *) ..
'margin:' .. margin .. ';'
else
style['table'] =
'margin:0;' ..
'-collapse:collapse;' ..
':;'
end  
style['table'] = style['table'] .. (args['table_style'] or '')  
-- setup the footer text
if( source ~= '' ) then
source = 'ஆதாரம்: ' .. source
if( footnote ~= '' ) then
footnote = footnote .. '
'
end
end
footnote = footnote .. source  
-- setup the data header cols/rows
local head = getheadrow(percentages, popname, yearname, percentname)  
-- count the total number of population rows
local argcount = 0
local rowcount = 0
for k, v in pairs( args ) do
if ( (type( k ) == 'number') and (not isempty(args[k])) ) then
if( k >= 1 and math.floor(k) == k and k > argcount) then
argcount = k
end
if( math.fmod(k - 1, 2) == 0 ) then
rowcount = rowcount + 1
end
end
end  
-- here is where we build all the data for the table
-- loop over columns and rows within columns
local pyear = ''
local ppop = ''
local offset = 1
local current_year = tonumber(os.date("%Y", os.time))
for r = 1,rowcount do
-- skip blank rows
while(isempty(args[offset]) and offset <= argcount) do
offset = offset + 2
end
-- generate the row if we have not exceeded the rowcount
if(offset <= argcount) then
table.insert(data, getpoprow(args[offset], args[offset + 1] or '', pyear, ppop,
linktype, percentages, current_year) )
pyear = args[offset]
ppop = args[offset+1] or ''
offset = offset + 2
end
end  
local graph = ''
graphpos = graphpos:lower
-- now that we have the data for the table, render it in the requested format  
if (direction == 'horizontal') then
if graphpos ~= '' then
local gwidth = tonumber(graphwidth) or 200
local gheight= tonumber(graphheight) or 170
local gthumb =
(graphpos == 'r' or graphpos == * and *) or
(graphpos == 'l' or graphpos == * and *) or ''
graph = rendergraph(, data, gwidth, gheight, gthumb, graphtype)
end
return renderhorizontal(data, head, title, footnote, alignfn, class, style, width, shading, perrow, rows, graphpos, graph)
else
if graphpos ~= '' then
local gwidth = tonumber(graphwidth) or (170 * cols)