Difference between revisions of "Module:Demography"
Jump to navigation
Jump to search
blackwiki>Mr. Stradivarius (don't use the rules attribute) |
blackwiki>Mr. Stradivarius (set border for all the cells) |
||
| Line 44: | Line 44: | ||
function p._main(args) | function p._main(args) | ||
| − | local | + | local years = p.getArgNums(args) |
local isEmbedded = yesno(args.embed) | local isEmbedded = yesno(args.embed) | ||
| + | local border = not isEmbedded and '1px solid #999' | ||
local padding = not isEmbedded and '4px' | local padding = not isEmbedded and '4px' | ||
| + | local dateRows = p.renderDateRows(args, years, border, padding) | ||
| + | local dateRowLength = #years + 1 | ||
| + | local root | ||
if isEmbedded then | if isEmbedded then | ||
root = htmlBuilder.create() | root = htmlBuilder.create() | ||
| Line 53: | Line 57: | ||
root | root | ||
.attr('align', 'center') | .attr('align', 'center') | ||
| − | .css('border', | + | .css('border', border) |
.css('border-collapse', 'collapse') | .css('border-collapse', 'collapse') | ||
.css('background-color', '#f3fff3') | .css('background-color', '#f3fff3') | ||
| Line 69: | Line 73: | ||
end | end | ||
end | end | ||
| − | root.wikitext( | + | root.wikitext(dateRows) |
local noDoubleYear = args.sansdoublescomptes or args.withoutdoublecount | local noDoubleYear = args.sansdoublescomptes or args.withoutdoublecount | ||
if noDoubleYear then | if noDoubleYear then | ||
| Line 75: | Line 79: | ||
root.tag('tr') | root.tag('tr') | ||
.tag('td') | .tag('td') | ||
| − | .attr('colspan', | + | .attr('colspan', dateRowLength) |
.attr('align', 'center') | .attr('align', 'center') | ||
| + | .css('border', border) | ||
.css('padding', padding) | .css('padding', padding) | ||
.tag('small') | .tag('small') | ||
| Line 93: | Line 98: | ||
end | end | ||
| − | function p.renderDateRows(args, padding | + | function p.renderDateRows(args, years, border, padding) |
| − | |||
local root = htmlBuilder.create() | local root = htmlBuilder.create() | ||
local hrow = root.tag('tr') | local hrow = root.tag('tr') | ||
| Line 102: | Line 106: | ||
.tag('th') | .tag('th') | ||
.attr('scope', 'row') | .attr('scope', 'row') | ||
| + | .css('border', border) | ||
.css('padding', padding) | .css('padding', padding) | ||
.wikitext('Year') | .wikitext('Year') | ||
| Line 111: | Line 116: | ||
end | end | ||
hrow.tag('th') | hrow.tag('th') | ||
| + | .css('border', border) | ||
.css('padding', padding) | .css('padding', padding) | ||
.wikitext(year) | .wikitext(year) | ||
| Line 119: | Line 125: | ||
.tag('th') | .tag('th') | ||
.attr('scope', 'row') | .attr('scope', 'row') | ||
| + | .css('border', border) | ||
.css('padding', padding) | .css('padding', padding) | ||
.wikitext('Population') | .wikitext('Population') | ||
for i, year in ipairs(years) do | for i, year in ipairs(years) do | ||
drow.tag('td') | drow.tag('td') | ||
| + | .css('border', border) | ||
.css('padding', padding) | .css('padding', padding) | ||
.wikitext(args[year]) | .wikitext(args[year]) | ||
Revision as of 03:17, 20 January 2014
This module implements {{demography}}. Please see the template page for documentation.
-- This module implements {{demography}}.
local getArgs = require('Module:Arguments').getArgs
local htmlBuilder = require('Module:HtmlBuilder')
local yesno = require('Module:Yesno')
local p = {}
-- Often-used functions.
local floor = math.floor
----------------------------------------------------------------------------
-- Helper functions
----------------------------------------------------------------------------
function p.isInteger(v)
if type(v) == 'number' and floor(v) == v then
return true
else
return false
end
end
function p.getArgNums(args)
local isInteger = p.isInteger
local nums = {}
for k, v in pairs(args) do
if isInteger(k) then
nums[#nums + 1] = k
end
end
table.sort(nums)
return nums
end
----------------------------------------------------------------------------
-- Main functions
----------------------------------------------------------------------------
function p.main(frame)
local args = getArgs(frame)
return p._main(args)
end
function p._main(args)
local years = p.getArgNums(args)
local isEmbedded = yesno(args.embed)
local border = not isEmbedded and '1px solid #999'
local padding = not isEmbedded and '4px'
local dateRows = p.renderDateRows(args, years, border, padding)
local dateRowLength = #years + 1
local root
if isEmbedded then
root = htmlBuilder.create()
else
root = htmlBuilder.create('table')
root
.attr('align', 'center')
.css('border', border)
.css('border-collapse', 'collapse')
.css('background-color', '#f3fff3')
if not args.noheader then
local currentTitle = mw.title.getCurrentTitle()
local source = args.source
root.tag('caption')
.css('margin-bottom', '0.5em')
.css('font-size', '1.1em')
.css('font-weight', 'bold')
.wikitext(
(args.caption or 'Historical population of ' .. currentTitle.prefixedText)
.. (source and ' <br /><small>(Source : ' .. source .. ')</small>' or '')
)
end
end
root.wikitext(dateRows)
local noDoubleYear = args.sansdoublescomptes or args.withoutdoublecount
if noDoubleYear then
local annualSurvey = args['enquêteannuelle'] or args.annualsurvey
root.tag('tr')
.tag('td')
.attr('colspan', dateRowLength)
.attr('align', 'center')
.css('border', border)
.css('padding', padding)
.tag('small')
.wikitext(
'From the year ' .. noDoubleYear .. ' on: No double counting—'
.. 'residents of multiple communes (e.g. students and military personnel)'
.. ' are counted only once.'
.. (
annualSurvey
and ' <br />' .. annualSurvey .. ': Provisional population (annual survey).'
or ''
)
)
end
return tostring(root)
end
function p.renderDateRows(args, years, border, padding)
local root = htmlBuilder.create()
local hrow = root.tag('tr')
hrow
.css('background', '#ddffdd')
.css('text-align', 'center')
.tag('th')
.attr('scope', 'row')
.css('border', border)
.css('padding', padding)
.wikitext('Year')
for i, year in ipairs(years) do
if year < 0 then
year = '−' .. tostring(year * -1)
else
year = tostring(year)
end
hrow.tag('th')
.css('border', border)
.css('padding', padding)
.wikitext(year)
end
local drow = root.tag('tr')
drow
.css('text-align', 'center')
.tag('th')
.attr('scope', 'row')
.css('border', border)
.css('padding', padding)
.wikitext('Population')
for i, year in ipairs(years) do
drow.tag('td')
.css('border', border)
.css('padding', padding)
.wikitext(args[year])
end
return tostring(root)
end
return p