Module:Country population and Module:Country population/sandbox: Difference between pages
Appearance
(Difference between pages)
Content deleted Content added
Repair |
use require('strict') instead of require('Module:No globals') |
||
Line 3: | Line 3: | ||
local mm = require('Module:Math') |
local mm = require('Module:Math') |
||
local p ={} |
local p ={} |
||
local pargs ={} |
|||
local args={} |
local args={} |
||
local data={} |
local data={} |
||
p.getArgs =function(frameArgs) |
|||
for k,v in pairs(frameArgs) do |
|||
args[k]=v |
|||
⚫ | |||
end |
|||
p.main = function(frame) -- |
p.main = function(frame) -- called from template |
||
pargs = frame:getParent().args |
|||
⚫ | |||
⚫ | |||
p.getArgs(frame:getParent().args) -- need to copy arguments because of the way frame arguments are handled |
|||
if args['list'] then p.getCountriesFromList(args['list']) end |
|||
if args['mode'] == "piechart" then |
|||
return p._piechart(frame) |
|||
else |
|||
return p._populations(frame) |
|||
end |
|||
end |
|||
function p.getCountriesFromList(list) |
|||
local country_lists = { |
|||
['South America'] = { "ARG","BOL","BRA","CHL","COL","ECU","GUY","PRY","PER","SUR","URY","VEN","BVT","FLK","SGS","GUF" }, |
|||
['North America'] = { "ATG","BHS","BLZ","BRB","CAN","CRI","CUB","DMA","DOM","GRD","GTM","HND","HTI","JAM","KNA","LCA","MEX","NIC","PAN","SLV","TTO","USA","VCT" }, |
|||
['Arab countries'] = { "EGY","DZA","SDN","IRQ","MAR","SAU","YEM","SYR","TUN","JOR","ARE","LBN","LBY","PSE","OMN","KWT","MRT","QAT","BHR","DJI","COM" } |
|||
} |
|||
if output then |
|||
---local i=1 |
|||
return frame:preprocess(output) |
|||
for k,v in pairs( country_lists[list] ) do |
|||
--args[i] = v |
|||
args[k] = v |
|||
--i=i+1 |
|||
end |
end |
||
⚫ | |||
return p.errormsg("No valid options") |
|||
end |
end |
||
function p.getPattern(section) |
function p.getPattern(section) |
||
local pattern = '<section begin= |
local pattern = '<section begin=' .. section ..'[ ]*/>(.-)<section end='..section..'[ ]*/>' |
||
return pattern |
return pattern |
||
end |
end |
||
Line 67: | Line 43: | ||
data.dates['latest']=value -- date of latest data |
data.dates['latest']=value -- date of latest data |
||
end |
end |
||
for value in string.gmatch( content , p.getPattern(" |
for value in string.gmatch( content , p.getPattern("date") ) do |
||
data.dates['previous']=mw.getContentLanguage():formatDate('j F Y', value) |
data.dates['previous']=mw.getContentLanguage():formatDate('j F Y', value) |
||
end |
end |
||
Line 73: | Line 49: | ||
-- get population data for each country passes as parameter |
-- get population data for each country passes as parameter |
||
for k,v in |
for k,v in pairs(args) do |
||
local country = mw.text.trim(v) |
local country = mw.text.trim(v) |
||
Line 106: | Line 82: | ||
end |
end |
||
mw.logObject(data) |
|||
return true |
return true |
||
end |
end |
||
Line 159: | Line 134: | ||
-- simple version |
-- simple version |
||
--output = mw.getCurrentFrame():expandTemplate{ title = " |
--output = mw.getCurrentFrame():expandTemplate{ title = "flaglist", args = templateArgs } |
||
-- method with fixed-height div and overflow |
-- method with fixed-height div and overflow |
||
output = '<div style="height:15px;overflow:visible;" >' |
output = '<div style="height:15px;overflow:visible;" >' |
||
.. mw.getCurrentFrame():expandTemplate{ title = " |
.. mw.getCurrentFrame():expandTemplate{ title = "flaglist", args = templateArgs } |
||
.. '</div>' |
.. '</div>' |
||
Line 183: | Line 158: | ||
.. '\n!rowspan=2|Country' |
.. '\n!rowspan=2|Country' |
||
.. '\n!rowspan=2|Projected population<br/>(' .. data['dates']['today'] .. ')' |
.. '\n!rowspan=2|Projected population<br/>(' .. data['dates']['today'] .. ')' |
||
.. '\n!rowspan=2|Pct of<br |
.. '\n!rowspan=2|Pct of<br>total' |
||
.. '\n!colspan=2|UN Population estimates' |
.. '\n!colspan=2|UN Population estimates' |
||
.. '\n!colspan=2|Annual growth' |
.. '\n!colspan=2|Annual growth' |
||
Line 196: | Line 171: | ||
while (data[i]) do -- add rows |
while (data[i]) do -- add rows |
||
output = output .. '\n|-\n|' .. i |
output = output .. '\n|-\n|' .. i |
||
output = output .. '\n|style="text-align:left;" |' .. frame:expandTemplate{ title = " |
output = output .. '\n|style="text-align:left;" |' .. frame:expandTemplate{ title = "flaglist", args = {data[i]['country'] } } |
||
output = output .. '\n| ' .. mm._precision_format(data[i]['populationProjected'],0) |
output = output .. '\n| ' .. mm._precision_format(data[i]['populationProjected'],0) |
||
output = output .. '\n| ' .. mm._precision_format(data[i]['populationProjected']/data.total['projected']*100,2) .. "%" -- projected |
output = output .. '\n| ' .. mm._precision_format(data[i]['populationProjected']/data.total['projected']*100,2) .. "%" -- projected |
||
Line 227: | Line 202: | ||
function p.tabulateData(frame) |
function p.tabulateData(frame) |
||
local hideYearsCols = false |
local hideYearsCols = frame.args['hide_years'] or false |
||
⚫ | |||
if args['hide_years'] == "true" then hideYearsCols = true end -- note the frame argument is a string, not a boolean |
|||
local |
local growthFootnote = frame.args['growth_note'] or "" |
||
⚫ | |||
local i = 1 |
local i = 1 |
||
Line 246: | Line 220: | ||
local tbl = mw.html.create('table'):addClass('wikitable') -- start table |
local tbl = mw.html.create('table'):addClass('wikitable') -- start table |
||
:addClass('sortable') |
:addClass('sortable') |
||
:addClass(' |
:addClass('mw-datatable') |
||
:addClass('static-row-numbers') -- prefix with row numbers using css and templatestyles |
:addClass('static-row-numbers') -- prefix with row numbers using css and templatestyles |
||
:addClass('nowrap') |
|||
:css('text-align','right') |
:css('text-align','right') |
||
Line 256: | Line 230: | ||
row :tag('th'):attr('rowspan', 2):wikitext('Country') |
row :tag('th'):attr('rowspan', 2):wikitext('Country') |
||
:tag('th'):attr('rowspan', 2):wikitext('Projected population<br/>(' .. data['dates']['today'] .. ')' ) |
:tag('th'):attr('rowspan', 2):wikitext('Projected population<br/>(' .. data['dates']['today'] .. ')' ) |
||
:tag('th'):attr('rowspan', 2):wikitext('Pct of<br |
:tag('th'):attr('rowspan', 2):wikitext('Pct of<br>total') |
||
if not hideYearsCols then |
if not hideYearsCols then |
||
row :tag('th'):attr('colspan', 2):wikitext('UN Population estimates') |
row :tag('th'):attr('colspan', 2):wikitext('UN Population estimates') |
||
Line 277: | Line 251: | ||
local row=tbl:tag('tr') |
local row=tbl:tag('tr') |
||
if p.norank(i) then |
|||
row:addClass('static-row-numbers-norank') -- add class to suppress display of rank number |
|||
end |
|||
--row :tag('td'):wikitext(i) |
--row :tag('td'):wikitext(i) |
||
row :tag('td'):cssText("text-align:left;") |
row :tag('td'):cssText("text-align:left;") |
||
:wikitext( |
:wikitext( p.getFlagLabel(data[i]['country']) ) |
||
:tag('td'):wikitext( mm._precision_format(data[i]['populationProjected'],0) ) |
:tag('td'):wikitext( mm._precision_format(data[i]['populationProjected'],0) ) |
||
:tag('td'):wikitext( mm._precision_format(data[i]['populationProjected']/data.total['projected']*100,2) .. "%" ) -- % of projected |
:tag('td'):wikitext( mm._precision_format(data[i]['populationProjected']/data.total['projected']*100,2) .. "%" ) -- % of projected |
||
Line 321: | Line 290: | ||
end |
end |
||
--[[ function p.norank(i) |
|||
⚫ | |||
check norank parameter for territories that won't display rank number in first column |
|||
]] |
|||
function p.norank(i) |
|||
if args['norank'] then -- if norank variable |
|||
if string.find( args['norank'], data[i]['country'], 1, true ) ~= nil then -- contains the country code |
|||
return true -- return true |
|||
end |
|||
--if data[i]['country'] == "FLK" or data[i]['country'] == "GUF" then return true end |
|||
end |
|||
return false -- end return false |
|||
end |
|||
⚫ | |||
takes list of country codes |
takes list of country codes |
||
gets population data from "List of countries by population (United Nations)" |
gets population data from "List of countries by population (United Nations)" |
||
outputs sorted table |
outputs sorted table |
||
]] |
]] |
||
function p.populations(frame |
function p.populations(frame) |
||
args = frame.args --TODO handle parent args for template |
args = frame.args --TODO handle parent args for template |
||
⚫ | |||
return p._populations(frame) |
|||
end |
|||
function p._populations(frame) |
|||
local page = "List of countries by population (United Nations)" |
local page = "List of countries by population (United Nations)" |
||
Line 379: | Line 334: | ||
args = frame.args --TODO handle parent args for template |
args = frame.args --TODO handle parent args for template |
||
return p._piechart(frame) |
|||
end |
|||
function p._piechart(frame) |
|||
local page = "List of countries by population (United Nations)" |
local page = "List of countries by population (United Nations)" |
||
Line 429: | Line 380: | ||
while data[i] and i <= maxSlices do |
while data[i] and i <= maxSlices do |
||
--templateArgs['label'..i] = data[i]['country'] |
--templateArgs['label'..i] = data[i]['country'] |
||
templateArgs['label'..i] = mw.getCurrentFrame():expandTemplate{ title = "getalias", args = { data[i]['country'], raw='y |
templateArgs['label'..i] = mw.getCurrentFrame():expandTemplate{ title = "getalias", args = { data[i]['country'], raw='y' } } |
||
templateArgs['value'..i] = mm._round( data[i]['populationNumber']/data.total['latest']*100,1) |
templateArgs['value'..i] = mm._round( data[i]['populationNumber']/data.total['latest']*100,1) |
||
templateArgs['color'..i] = args['color'..i] or nil |
templateArgs['color'..i] = args['color'..i] or nil |
||
Line 630: | Line 581: | ||
return p.templateStyle( frame, "Static column begin/styles.css" ) .. output |
return p.templateStyle( frame, "Static column begin/styles.css" ) .. output |
||
end |
|||
-- function to generate table cells (must be invoked) |
|||
function p.cells( frame) |
|||
⚫ | |||
local country = frame.args[1] |
|||
local year0 = frame.args[2] or "" |
|||
local year1 = frame.args[3] or "" |
|||
-- if (1==1) then return tonumber(year1.gsub(",","")) end |
|||
output = country .. "|" .. year0 .. "|" .. year1 .. "|" |
|||
-- need to output the secton tags, but this may not be possible (see {{phab|T39256}}) |
|||
--output = output .. frame:extensionTag( 'section', 'value', { begin = "test" } ) |
|||
--output = output .. frame:preprocess('<section begin=' .. country .. '_0 />' .. year0 .. '<section end=' .. country .. '_0 />') |
|||
--output = output .. '<section begin=' .. country .. '_1 />' .. year0 .. '<section end=' .. country .. '_1 />' |
|||
--output = output .. frame:expandTemplate{ title = 'section/sandbox', args = { title="test", content="out" } } |
|||
--output = output .. frame:callParserFunction{ name = '#tag', args = { ['1'] = "", section', '', begin = 'some text' } } |
|||
local grab = frame:callParserFunction{ name = '#lst', args = { mw.title.getCurrentTitle().prefixedText, country } } |
|||
local result = mw.text.split( grab, "||", true ) |
|||
year0 = result[1] |
|||
year1 = result[2] |
|||
mw.addWarning ( grab ) |
|||
local change = frame:expandTemplate{ title = 'change', args = { year0, year1, disp="out" } } |
|||
output = output .. "|" .. change |
|||
--return output |
|||
return change |
|||
end |
end |
||
function p.templateStyle( frame, src ) |
function p.templateStyle( frame, src ) |