https://en.wikipedia.org/w/index.php?action=history&feed=atom&title=Module%3ACountry_population%2Fsandbox Module:Country population/sandbox - Revision history 2025-06-01T18:13:52Z Revision history for this page on the wiki MediaWiki 1.45.0-wmf.3 https://en.wikipedia.org/w/index.php?title=Module:Country_population/sandbox&diff=1117367412&oldid=prev WOSlinker: use require('strict') instead of require('Module:No globals') 2022-10-21T10:01:22Z <p>use require(&#039;strict&#039;) instead of require(&#039;Module:No globals&#039;)</p> <table style="background-color: #fff; color: #202122;" data-mw="interface"> <col class="diff-marker" /> <col class="diff-content" /> <col class="diff-marker" /> <col class="diff-content" /> <tr class="diff-title" lang="en"> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 10:01, 21 October 2022</td> </tr><tr> <td colspan="2" class="diff-lineno">Line 1:</td> <td colspan="2" class="diff-lineno">Line 1:</td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>require('<del style="font-weight: bold; text-decoration: none;">Module:No globals</del>')</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>require('<ins style="font-weight: bold; text-decoration: none;">strict</ins>')</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>local fn = require('Module:Formatnum')</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>local fn = require('Module:Formatnum')</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>local mm = require('Module:Math')</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>local mm = require('Module:Math')</div></td> </tr> </table> WOSlinker https://en.wikipedia.org/w/index.php?title=Module:Country_population/sandbox&diff=1022343975&oldid=prev Guarapiranga: A couple of improvements 2021-05-10T00:13:28Z <p>A couple of improvements</p> <p><b>New page</b></p><div>require(&#039;Module:No globals&#039;)<br /> local fn = require(&#039;Module:Formatnum&#039;)<br /> local mm = require(&#039;Module:Math&#039;)<br /> local p ={}<br /> local pargs ={}<br /> local args={}<br /> local data={}<br /> <br /> <br /> p.main = function(frame) -- called from template<br /> pargs = frame:getParent().args<br /> local output <br /> <br /> if output then<br /> return frame:preprocess(output)<br /> end<br /> <br /> return p.errormsg(&quot;No valid options&quot;)<br /> end<br /> function p.getPattern(section)<br /> local pattern = &#039;&lt;section begin=&#039; .. section ..&#039;[ ]*/&gt;(.-)&lt;section end=&#039;..section..&#039;[ ]*/&gt;&#039;<br /> return pattern<br /> end<br /> function p.getPopulationData(frame)<br /> <br /> local page = &quot;List of countries by population (United Nations)&quot;<br /> data[&#039;dates&#039;] = {}<br /> data[&#039;total&#039;] = {}<br /> data.total[&#039;latest&#039;] = 0<br /> data.total[&#039;previous&#039;] = 0<br /> data.total[&#039;projected&#039;] = 0<br /> <br /> --local total = 0<br /> --local totalProjected = 0<br /> local count = 0<br /> local title = mw.title.new( page) -- , ns) -- creates object if page doesn&#039;t exist (and valid page name)<br /> --TODO: could use mw.title.makeTitle(), but that needs ns<br /> <br /> local content = title:getContent()<br /> <br /> -- get dates <br /> for value in string.gmatch( content , p.getPattern(&quot;date_1&quot;) ) do<br /> data.dates[&#039;latest&#039;]=value -- date of latest data <br /> end<br /> for value in string.gmatch( content , p.getPattern(&quot;date&quot;) ) do<br /> data.dates[&#039;previous&#039;]=mw.getContentLanguage():formatDate(&#039;j F Y&#039;, value)<br /> end <br /> data.dates[&#039;today&#039;] = mw.getContentLanguage():formatDate(&#039;j F Y&#039;) -- today&#039;s date (for formatting see https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time)<br /> <br /> -- get population data for each country passes as parameter<br /> for k,v in pairs(args) do<br /> local country = mw.text.trim(v)<br /> <br /> -- get population data from section <br /> local section = country .. &quot;_1&quot;<br /> for value in string.gmatch( content , p.getPattern(section) ) do<br /> <br /> count=count+1<br /> data[count] = {}<br /> data[count][&#039;country&#039;] = country<br /> data[count][&#039;populationString&#039;] = frame:preprocess(value)<br /> local raw = string.gsub(data[count][&#039;populationString&#039;], &quot;,&quot;, &quot;&quot;) -- strip formatting from number string<br /> data[count][&#039;populationNumber&#039;] = tonumber(raw)<br /> data.total[&#039;latest&#039;] = data.total[&#039;latest&#039;] + data[count][&#039;populationNumber&#039;]<br /> <br /> local section = country .. &quot;_0&quot;<br /> for value2 in string.gmatch( content , p.getPattern(section) ) do<br /> data[count][&#039;populationString2&#039;] = frame:preprocess(value2)<br /> local raw = string.gsub(data[count][&#039;populationString2&#039;], &quot;,&quot;, &quot;&quot;) -- strip formatting from number string<br /> data[count][&#039;populationNumber2&#039;] = tonumber(raw)<br /> data.total[&#039;previous&#039;] = data.total[&#039;previous&#039;] + data[count][&#039;populationNumber2&#039;]<br /> <br /> data[count][&#039;populationIncrement&#039;]=data[count][&#039;populationNumber&#039;] - data[count][&#039;populationNumber2&#039;]<br /> data[count][&#039;populationGrowth&#039;] =data[count][&#039;populationIncrement&#039;]/data[count][&#039;populationNumber2&#039;]<br /> data[count][&#039;populationDouble&#039;] = p.getPopulationDoubleTime(data[count][&#039;populationNumber&#039;],data[count][&#039;populationNumber2&#039;])<br /> <br /> data[count][&#039;populationProjected&#039;] = p.getPopulationProjection(data[count][&#039;populationNumber&#039;],data[count][&#039;populationNumber2&#039;])<br /> data.total[&#039;projected&#039;] = data.total[&#039;projected&#039;] + data[count][&#039;populationProjected&#039;] <br /> end<br /> <br /> end<br /> end<br /> <br /> return true<br /> end<br /> <br /> -- estimate time to double population based on latest growth rate<br /> function p.getPopulationDoubleTime(latest,previous)<br /> local growth = (latest - previous) / previous<br /> local doubleTime = math.log( 2 ) / math.log(1 + growth)<br /> return doubleTime<br /> end<br /> <br /> -- estimate today&#039;s population based on latest growth rate<br /> function p.getPopulationProjection(latest,previous)<br /> local ay = ( mw.getCurrentFrame():callParserFunction{ name = &#039;#time&#039;, args = { &quot;U&quot;, data.dates[&#039;today&#039;] } }<br /> - mw.getCurrentFrame():callParserFunction{ name = &#039;#time&#039;, args = { &quot;U&quot;, data.dates[&#039;previous&#039;] } })<br /> /60/60/24/365.2425 -- number of years since first date until today‬<br /> local projected = math.pow(previous, 1 - ay ) * math.pow(latest, ay)<br /> return projected<br /> end<br /> <br /> --[[ sort rows by population (defaults to latest population)<br /> TODO add options for sorting columns<br /> ]]<br /> function p.sortPopulationData(ByColumn)<br /> <br /> local sort_function = function( a,b )<br /> if (tonumber(a.populationNumber) &gt; tonumber(b.populationNumber)) then -- primary sort on &#039;population&#039; -&gt; a before b<br /> return true<br /> end<br /> end<br /> <br /> table.sort(data, sort_function)<br /> end<br /> --[[ Function to get flag icon and handle special cases <br /> 1. There is an issue of non-standard sizes when used with static rank column <br /> The three countries with extra height (and the required size parameter) are <br /> Nepal/NPL (size=12px), Switzerland/CHE (size=15px), Vatican/VAT (size=15px)<br /> a few have lower default heights so it doesn&#039;t matter (Poland, New Caledonia)<br /> 2. Alias, e.g. NEP-&gt;NPL, TRI-&gt;TTO<br /> ]]<br /> function p.getFlagLabel(countryCode)<br /> local output <br /> local templateArgs = { countryCode }<br /> <br /> local size<br /> if countryCode == &quot;CHE&quot; or countryCode == &quot;VAT&quot; then<br /> size=&quot;15px&quot;<br /> elseif countryCode == &quot;NPL&quot; then<br /> size=&quot;12px&quot;<br /> end<br /> if size then templateArgs[&#039;size&#039;] = size end<br /> <br /> -- simple version<br /> --output = mw.getCurrentFrame():expandTemplate{ title = &quot;flaglist&quot;, args = templateArgs } <br /> <br /> -- method with fixed-height div and overflow<br /> output = &#039;&lt;div style=&quot;height:15px;overflow:visible;&quot; &gt;&#039;<br /> .. mw.getCurrentFrame():expandTemplate{ title = &quot;flaglist&quot;, args = templateArgs }<br /> .. &#039;&lt;/div&gt;&#039;<br /> <br /> <br /> return output <br /> end<br /> <br /> --[[ output table of data as Wikitext table<br /> ]]<br /> function p.tabulateDataWikitext(frame)<br /> <br /> local output<br /> local i = 1<br /> -- output table<br /> output = &#039;{| class=&quot;wikitable sortable mw-datatable&quot; style=&quot;text-align:right;&quot; &#039; -- table<br /> output = &#039;|-class=wrap&#039;<br /> output = output --headers (top row)<br /> .. &#039;\n!rowspan=2|#&#039;<br /> .. &#039;\n!rowspan=2|Country&#039;<br /> .. &#039;\n!rowspan=2|Projected population&lt;br/&gt;(&#039; .. data[&#039;dates&#039;][&#039;today&#039;] .. &#039;)&#039; <br /> .. &#039;\n!rowspan=2|Pct of&lt;br&gt;total&#039;<br /> .. &#039;\n!colspan=2|UN Population estimates&#039;<br /> .. &#039;\n!colspan=2|Annual growth&#039;<br /> .. &#039;\n!rowspan=2|Doubling time&lt;br/&gt;(years)&#039;<br /> <br /> .. &#039;\n|-&#039; -- headers (second row)<br /> .. &#039;\n!&#039; .. data.dates[&#039;latest&#039;] <br /> .. &#039;\n!&#039; .. data.dates[&#039;previous&#039;] <br /> .. &#039;\n!Increment&#039;<br /> .. &#039;\n!Rate&#039; <br /> <br /> while (data[i]) do -- add rows<br /> output = output .. &#039;\n|-\n|&#039; .. i <br /> output = output .. &#039;\n|style=&quot;text-align:left;&quot; |&#039; .. frame:expandTemplate{ title = &quot;flaglist&quot;, args = {data[i][&#039;country&#039;] } }<br /> output = output .. &#039;\n| &#039; .. mm._precision_format(data[i][&#039;populationProjected&#039;],0) <br /> output = output .. &#039;\n| &#039; .. mm._precision_format(data[i][&#039;populationProjected&#039;]/data.total[&#039;projected&#039;]*100,2) .. &quot;%&quot; -- projected<br /> output = output .. &#039;\n| &#039; .. data[i][&#039;populationString&#039;] <br /> output = output .. &#039;\n| &#039; .. data[i][&#039;populationString2&#039;] <br /> output = output .. &#039;\n| &#039; .. mm._precision_format(data[i][&#039;populationIncrement&#039;],0) <br /> output = output .. &#039;\n| &#039; .. mm._round(data[i][&#039;populationGrowth&#039;]*100,2) .. &quot;%&quot;<br /> output = output .. &#039;\n| &#039; .. mm._round(data[i][&#039;populationDouble&#039;],0)<br /> i=i+1<br /> end<br /> <br /> local newcell = &#039;\n! style=&quot;text-align:right;&quot; | &#039; <br /> output = output .. &#039;\n|-&#039; -- totals row<br /> .. &#039;\n! !! Total&#039; <br /> .. newcell .. fn.formatNum(mm._round(data.total[&#039;projected&#039;],0),&quot;en&quot;,0) <br /> .. newcell .. &#039;100%&#039;<br /> .. newcell .. fn.formatNum(data.total[&#039;latest&#039;],&quot;en&quot;,0) <br /> .. newcell .. fn.formatNum(data.total[&#039;previous&#039;],&quot;en&quot;,0) <br /> .. newcell .. fn.formatNum(data.total[&#039;latest&#039;]-data.total[&#039;previous&#039;],&quot;en&quot;,0)<br /> .. newcell .. fn.formatNum((data.total[&#039;latest&#039;]-data.total[&#039;previous&#039;])/data.total[&#039;previous&#039;]*100,&quot;en&quot;,2)..&quot;%&quot;<br /> .. newcell .. mm._precision_format(p.getPopulationDoubleTime(data.total[&#039;latest&#039;],data.total[&#039;previous&#039;]),0)<br /> <br /> output = output .. &#039;\n|}&#039;<br /> return output<br /> end<br /> <br /> <br /> --[[ output table of data as use Lua HTML Library<br /> ]]<br /> function p.tabulateData(frame)<br /> <br /> local hideYearsCols = frame.args[&#039;hide_years&#039;] or false<br /> local doublingFootnote = frame.args[&#039;doubling_note&#039;] or &quot;&quot;<br /> local growthFootnote = frame.args[&#039;growth_note&#039;] or &quot;&quot;<br /> <br /> local i = 1<br /> local static = mw.html.create(&#039;table&#039;):addClass(&#039;wikitable&#039;)<br /> <br /> static:tag(&#039;tr&#039;):tag(&#039;th&#039;):attr(&#039;rowspan&#039;, 1):wikitext(&#039;&lt;br/&gt;&#039;):cssText(&#039;border-bottom-color:#eaecf0;&#039;)<br /> static:tag(&#039;tr&#039;):tag(&#039;th&#039;):wikitext(&#039;&lt;br/&gt;&#039;):cssText(&#039;border-top-color:#eaecf0;&#039;)<br /> while (data[i]) do -- add rows<br /> static:tag(&#039;tr&#039;):tag(&#039;td&#039;):wikitext(i)<br /> i=i+1<br /> end<br /> static:tag(&#039;tr&#039;):tag(&#039;th&#039;):wikitext(&#039;&lt;br/&gt;&#039;)<br /> local numRows=i-1<br /> <br /> local tbl = mw.html.create(&#039;table&#039;):addClass(&#039;wikitable&#039;) -- start table<br /> :addClass(&#039;sortable&#039;)<br /> :addClass(&#039;mw-datatable&#039;)<br /> :addClass(&#039;static-row-numbers&#039;) -- prefix with row numbers using css and templatestyles<br /> :addClass(&#039;nowrap&#039;)<br /> :css(&#039;text-align&#039;,&#039;right&#039;)<br /> <br /> local row = tbl:tag(&#039;tr&#039;) -- header row<br /> :addClass(&#039;static-row-header&#039;) -- for templatestyles in some skins<br /> --:tag(&#039;th&#039;):attr(&#039;rowspan&#039;, 2):wikitext(&#039;#&#039;)<br /> row :tag(&#039;th&#039;):attr(&#039;rowspan&#039;, 2):wikitext(&#039;Country&#039;)<br /> :tag(&#039;th&#039;):attr(&#039;rowspan&#039;, 2):wikitext(&#039;Projected population&lt;br/&gt;(&#039; .. data[&#039;dates&#039;][&#039;today&#039;] .. &#039;)&#039; )<br /> :tag(&#039;th&#039;):attr(&#039;rowspan&#039;, 2):wikitext(&#039;Pct of&lt;br&gt;total&#039;)<br /> if not hideYearsCols then<br /> row :tag(&#039;th&#039;):attr(&#039;colspan&#039;, 2):wikitext(&#039;UN Population estimates&#039;)<br /> end<br /> row :tag(&#039;th&#039;):attr(&#039;colspan&#039;, 2):wikitext(&#039;Annual growth&#039;..growthFootnote)<br /> :tag(&#039;th&#039;):attr(&#039;rowspan&#039;, 2):wikitext(&#039;Doubling time&lt;br/&gt;(years)&#039;..doublingFootnote)<br /> <br /> row = tbl:tag(&#039;tr&#039;) -- headers (second row)<br /> :addClass(&#039;static-row-header&#039;) -- for templatestyles in some skins<br /> if not hideYearsCols then<br /> row :tag(&#039;th&#039;):wikitext(data.dates[&#039;latest&#039;] )<br /> :tag(&#039;th&#039;):wikitext(data.dates[&#039;previous&#039;] )<br /> end<br /> row :tag(&#039;th&#039;):wikitext(&#039;Increment&#039;)<br /> :tag(&#039;th&#039;):wikitext(&#039;Rate&#039;) <br /> <br /> <br /> i = 1<br /> while (data[i]) do -- add country rows<br /> <br /> local row=tbl:tag(&#039;tr&#039;) <br /> --row :tag(&#039;td&#039;):wikitext(i) <br /> row :tag(&#039;td&#039;):cssText(&quot;text-align:left;&quot;)<br /> :wikitext( p.getFlagLabel(data[i][&#039;country&#039;]) )<br /> :tag(&#039;td&#039;):wikitext( mm._precision_format(data[i][&#039;populationProjected&#039;],0) )<br /> :tag(&#039;td&#039;):wikitext( mm._precision_format(data[i][&#039;populationProjected&#039;]/data.total[&#039;projected&#039;]*100,2) .. &quot;%&quot; ) -- % of projected <br /> if not hideYearsCols then<br /> row :tag(&#039;td&#039;):wikitext( data[i][&#039;populationString&#039;] )<br /> :tag(&#039;td&#039;):wikitext( data[i][&#039;populationString2&#039;] )<br /> end<br /> row :tag(&#039;td&#039;):wikitext( mm._precision_format(data[i][&#039;populationIncrement&#039;],0) )<br /> :tag(&#039;td&#039;):wikitext( mm._precision_format(data[i][&#039;populationGrowth&#039;]*100,2) .. &quot;%&quot; )<br /> :tag(&#039;td&#039;):wikitext( mm._precision_format(data[i][&#039;populationDouble&#039;],0) )<br /> i=i+1<br /> end<br /> <br /> local style = { [&#039;text-align&#039;]=&#039;right&#039; } <br /> row = tbl:tag(&#039;tr&#039;) -- totals row<br /> :addClass(&#039;static-row-header&#039;) -- for templatestyles in some skins<br /> --:tag(&#039;th&#039;) :wikitext()<br /> row :tag(&#039;th&#039;) :wikitext(&#039;Total&#039;)<br /> :tag(&#039;th&#039;):css(style):wikitext( fn.formatNum(mm._round(data.total[&#039;projected&#039;],0),&quot;en&quot;,0) ) <br /> :tag(&#039;th&#039;):css(style):wikitext( &#039;100%&#039; )<br /> if not hideYearsCols then<br /> row :tag(&#039;th&#039;):css(style):wikitext( fn.formatNum(data.total[&#039;latest&#039;], &quot;en&quot;,0) )<br /> :tag(&#039;th&#039;):css(style):wikitext( fn.formatNum(data.total[&#039;previous&#039;],&quot;en&quot;,0) )<br /> end<br /> row :tag(&#039;th&#039;):css(style):wikitext( fn.formatNum(data.total[&#039;latest&#039;] - data.total[&#039;previous&#039;],&quot;en&quot;,0) )<br /> :tag(&#039;th&#039;):css(style):wikitext( fn.formatNum((data.total[&#039;latest&#039;] - data.total[&#039;previous&#039;]) / data.total[&#039;previous&#039;] * 100,&quot;en&quot;,2)..&quot;%&quot; )<br /> :tag(&#039;th&#039;):css(style):wikitext( mm._precision_format(p.getPopulationDoubleTime(data.total[&#039;latest&#039;],data.total[&#039;previous&#039;]),0) )<br /> <br /> -- return tostring(tbl) -- return table without row numbers<br /> <br /> -- use separate column for static row numbers<br /> -- return &#039;{|\n|style=&quot;vertical-align:top;&quot; |&#039; .. tostring(static) .. &#039;\n|&#039; .. tostring(tbl) .. &#039;\n|}&#039;<br /> <br /> -- use css method and templatestyles to prefix row numbers<br /> return p.templateStyle( frame, &quot;Template:Static_row_numbers/styles.css&quot; ) .. tostring(tbl)<br /> end<br /> <br /> <br /> --[[ currently the main entry function<br /> takes list of country codes<br /> gets population data from &quot;List of countries by population (United Nations)&quot;<br /> outputs sorted table<br /> ]]<br /> function p.populations(frame)<br /> <br /> args = frame.args --TODO handle parent args for template<br /> <br /> local page = &quot;List of countries by population (United Nations)&quot;<br /> <br /> local title = mw.title.new( page) -- , ns) -- creates object if page doesn&#039;t exist (and valid page name)<br /> --TODO: could use mw.title.makeTitle(), but that needs ns<br /> local output = &quot;&quot;<br /> if title and title.exists then <br /> local content = title:getContent()<br /> <br /> if not p.getPopulationData(frame) then<br /> return p.errormsg(&quot;Error retrieving data.&quot;)<br /> end<br /> <br /> p.sortPopulationData(&quot;latest&quot;)<br /> <br /> --output = p.tabulateDataWikitext(frame) -- version building table with Wikitext<br /> output = p.tabulateData(frame) -- version building table with mw.html library<br /> <br /> else<br /> return &#039;&lt;span class=&quot;error&quot;&gt;No page title found&lt;/span&gt;&#039;<br /> end<br /> <br /> local test = &quot;test: &quot; <br /> local number=5435.12345 <br /> test= fn.formatNum(5435.12345,&quot;en&quot;,0)<br /> --test= frame:expandTemplate{ title = &quot;formatnum&quot;, args = { totalProjected ,&quot;en&quot;,0 } }<br /> --test=frame:callParserFunction{ name = &#039;formatnum&#039;, args = { totalProjected, decs=2 } } <br /> <br /> <br /> return output --.. test<br /> end<br /> -- function for pie chart<br /> function p.piechart(frame)<br /> <br /> args = frame.args --TODO handle parent args for template<br /> <br /> local page = &quot;List of countries by population (United Nations)&quot;<br /> <br /> local title = mw.title.new( page) -- , ns) -- creates object if page doesn&#039;t exist (and valid page name)<br /> --TODO: could use mw.title.makeTitle(), but that needs ns<br /> local output = &quot;&quot;<br /> if title and title.exists then <br /> local content = title:getContent()<br /> <br /> if not p.getPopulationData(frame) then<br /> return p.errormsg(&quot;Error retrieving data.&quot;)<br /> end<br /> <br /> p.sortPopulationData(&quot;latest&quot;)<br /> <br /> --output = p.tabulateDataWikitext(frame) -- version building table with Wikitext<br /> output = p.makePieChart(frame) -- version building table with mw.html library<br /> <br /> else<br /> return &#039;&lt;span class=&quot;error&quot;&gt;No page title found&lt;/span&gt;&#039;<br /> end<br /> <br /> <br /> <br /> return output --.. test<br /> end<br /> <br /> function p.makePieChart(frame) <br /> <br /> --local args=frame.args<br /> local templateArgs = {}<br /> <br /> templateArgs[&#039;caption&#039;] = args[&#039;caption&#039;] or &quot;&quot; --&#039;South American population by country&#039; --.. &#039; (top 8)&#039;<br /> templateArgs[&#039;thumb&#039;] = args[&#039;thumb&#039;] or &quot;right&quot;<br /> templateArgs[&#039;other&#039;] = args[&#039;other&#039;] or nil<br /> local maxSlices = tonumber(args[&#039;slices&#039;]) -- nil if not a number<br /> if type(maxSlices) ~= &quot;number&quot; or maxSlices &gt; 30 or maxSlices &lt; 1 then<br /> maxSlices = 30 -- limit of template -- get number from data<br /> end<br /> <br /> <br /> <br /> <br /> local i=1<br /> while data[i] and i &lt;= maxSlices do<br /> --templateArgs[&#039;label&#039;..i] = data[i][&#039;country&#039;] <br /> templateArgs[&#039;label&#039;..i] = mw.getCurrentFrame():expandTemplate{ title = &quot;getalias&quot;, args = { data[i][&#039;country&#039;], raw=&#039;y&#039; } }<br /> templateArgs[&#039;value&#039;..i] = mm._round( data[i][&#039;populationNumber&#039;]/data.total[&#039;latest&#039;]*100,1)<br /> templateArgs[&#039;color&#039;..i] = args[&#039;color&#039;..i] or nil<br /> i=i+1<br /> end<br /> <br /> <br /> --[[{{Pie chart<br /> |caption= South American population by country (top 8)<br /> |other = yes<br /> |label1 = {{getalias|BRA}}<br /> |value1 = {{#expr: {{country population|BRA|raw=y}} / {{xyz|Total}} * 100 round 1}}<br /> |label2 = {{getalias|COL}}<br /> |value2 = {{#expr: {{country population|COL|raw=y}} / {{xyz|Total}} * 100 round 1}}<br /> |label3 = {{getalias|ARG}}<br /> |value3 = {{#expr: {{country population|ARG|raw=y}} / {{xyz|Total}} * 100 round 1}}<br /> |label4 = {{getalias|PER}}<br /> |value4 = {{#expr: {{country population|PER|raw=y}} / {{xyz|Total}} * 100 round 1}}<br /> |label5 = {{getalias|VEN}}<br /> |value5 = {{#expr: {{country population|VEN|raw=y}} / {{xyz|Total}} * 100 round 1}}<br /> |label6 = {{getalias|CHL}}<br /> |value6 = {{#expr: {{country population|CHL|raw=y}} / {{xyz|Total}} * 100 round 1}}<br /> |label7 = {{getalias|ECU}}<br /> |value7 = {{#expr: {{country population|ECU|raw=y}} / {{xyz|Total}} * 100 round 1}}<br /> |label8 = {{getalias|BOL}}<br /> |value8 = {{#expr: {{country population|BOL|raw=y}} / {{xyz|Total}} * 100 round 1}}<br /> }} ]] <br /> <br /> local chart = mw.getCurrentFrame():expandTemplate{ title = &quot;Pie chart&quot;, args = templateArgs }<br /> <br /> return chart<br /> <br /> <br /> end<br /> <br /> <br /> function p.firstToUpper(str)<br /> return (str:gsub(&quot;^%l&quot;, string.upper))<br /> end<br /> p.errormsg = function (message)<br /> return &#039;&lt;span class=&quot;error&quot;&gt;&#039; .. message .. &#039;&lt;/span&gt;&#039; <br /> end <br /> <br /> -- Test why was the sort being applied to the wrong level? Fixed <br /> function p.test(frame) -- meant test()<br /> <br /> local tbl = mw.html.create(&#039;table&#039;):addClass(&#039;wikitable&#039;):addClass(&#039;sortable&#039;):addClass(&#039;mw-datatable&#039;)<br /> :css(&#039;text-align&#039;,&#039;right&#039;)<br /> <br /> tbl:tag(&#039;tr&#039;) -- header row<br /> :tag(&#039;th&#039;):attr(&#039;rowspan&#039;, 2):wikitext(&#039;#&#039;)<br /> :tag(&#039;th&#039;):attr(&#039;rowspan&#039;, 2):wikitext(&#039;A&#039;)<br /> :tag(&#039;th&#039;):attr(&#039;rowspan&#039;, 2):wikitext(&#039;B&#039;)<br /> :tag(&#039;th&#039;):attr(&#039;colspan&#039;, 2):wikitext(&#039;C+D&#039;):addClass(&#039;unsortable&#039;)<br /> :tag(&#039;th&#039;):attr(&#039;colspan&#039;, 2):wikitext(&#039;E+F&#039;):addClass(&#039;unsortable&#039;)<br /> :tag(&#039;th&#039;):attr(&#039;rowspan&#039;, 2):wikitext(&#039;G&#039;)<br /> <br /> tbl:tag(&#039;tr&#039;) -- headers (second row)<br /> :tag(&#039;th&#039;):wikitext(&#039;C&#039;):addClass(&#039;sortable&#039;)<br /> :tag(&#039;th&#039;):wikitext(&#039;D&#039;):addClass(&#039;sortable&#039;)<br /> :tag(&#039;th&#039;):wikitext(&#039;E&#039;):addClass(&#039;sortable&#039;)<br /> :tag(&#039;th&#039;):wikitext(&#039;F&#039;) :addClass(&#039;sortable&#039;) <br /> <br /> <br /> local i = 1<br /> while (i&lt;5) do -- add rows<br /> tbl:tag(&#039;tr&#039;) <br /> :tag(&#039;td&#039;):wikitext(i) <br /> :tag(&#039;td&#039;):wikitext(&quot;A&quot;..i)<br /> :tag(&#039;td&#039;):wikitext(&quot;B&quot;..i)<br /> :tag(&#039;td&#039;):wikitext(tostring(math.fmod(5-i,2)) .. &#039;C&#039; .. i )<br /> :tag(&#039;td&#039;):wikitext(&quot;D&quot;..i)<br /> :tag(&#039;td&#039;):wikitext(&quot;E&quot;..i)<br /> :tag(&#039;td&#039;):wikitext(tostring(math.fmod(5-i,2)) .. &#039;F&#039; .. i )<br /> :tag(&#039;td&#039;):wikitext(&quot;G&quot;..i)<br /> i=i+1<br /> end<br /> <br /> local output = &#039;{| class=&quot;wikitable sortable mw-datatable&quot; style=&quot;text-align:right;&quot; &#039; -- table<br /> output = output<br /> .. &#039;\n!rowspan=2|#&#039;<br /> .. &#039;\n!rowspan=2|A&#039;<br /> .. &#039;\n!rowspan=2|B&#039; <br /> .. &#039;\n!colspan=2|C+D&#039;<br /> .. &#039;\n!colspan=2|E+F&#039;<br /> .. &#039;\n!rowspan=2|G&#039;<br /> <br /> .. &#039;\n|-&#039; -- headers (second row)<br /> .. &#039;\n!C&#039; <br /> .. &#039;\n!D&#039; <br /> .. &#039;\n!E&#039;<br /> .. &#039;\n!F&#039; <br /> i=1<br /> while (i&lt;5) do -- add rows<br /> output = output .. &#039;\n|-\n|&#039; .. i <br /> output = output .. &#039;\n|A&#039; .. i <br /> output = output .. &#039;\n|B&#039; .. i<br /> output = output .. &#039;\n|&#039; .. tostring(math.fmod(5-i,2)) .. &#039;C&#039; .. i <br /> output = output .. &#039;\n|D&#039; .. i <br /> output = output .. &#039;\n|E&#039; .. i <br /> output = output .. &#039;\n|&#039; .. tostring(math.fmod(5-i,2)) .. &#039;F&#039; .. i <br /> output = output .. &#039;\n|G&#039; .. i<br /> i=i+1<br /> end<br /> output = output .. &#039;\n|}&#039;<br /> <br /> return output .. tostring(tbl)<br /> end<br /> <br /> -- function for static rank column<br /> <br /> function p.rank(frame)<br /> <br /> --args = frame.args -- for module TODO allow invoke to work<br /> args = frame:getParent().args -- parent arguments for template<br /> args = frame.args -- invoke arguments for template<br /> <br /> local caption = args[&#039;caption&#039;] <br /> local valign = args[&#039;valign&#039;] or &quot;top&quot;<br /> local rowHeader = args[&#039;row-header&#039;] <br /> local headerPadding = args[&#039;header-padding&#039;] or &quot;0px&quot;<br /> local textAlign = args[&#039;text-align&#039;] or &quot;right&quot;<br /> local style = args[&#039;style&#039;] or &quot;&quot;<br /> local headerHeight = args[&#039;header-height&#039;] or &quot;&quot;<br /> local headerLines = args[&#039;header-lines&#039;] or 1<br /> local headerText = args[&#039;header-text&#039;] or &quot;&quot;<br /> local rows = tonumber(args[&#039;rows&#039;]) or 0<br /> local rowHeader = args[&#039;row-header&#039;] <br /> local rowHeight = args[&#039;row-height&#039;] <br /> <br /> local marginRight = &quot;0px&quot;<br /> if rowHeader then marginRight = &quot;-8px&quot; end<br /> local headerValign = &quot;bottom&quot;<br /> if rowHeader then headerValign = &quot;center&quot; end -- copied from template; should be middle?<br /> local linebreaks = &quot;&quot;<br /> if headerLines then<br /> local i=0<br /> while i&lt;tonumber(headerLines) do<br /> linebreaks = linebreaks .. &quot;&lt;br /&gt;&quot;<br /> i=i+1<br /> end<br /> end<br /> <br /> --[[<br /> {| <br /> |+&#039;&#039;&#039;{{{caption| }}}&#039;&#039;&#039;<br /> | valign={{{valign|top}}} |<br /> {| class=&quot;wikitable&quot; style=&quot;margin-right:{{#if:{{{row-header|}}}|-8px|0px}}; padding:{{{header-padding|0px}}}; text-align:{{{text-align|right}}};{{{style|}}}&quot;<br /> ! style=height:{{{header-height|}}} valign={{{header-valign|{{#if: {{{row-header|}}} | center | bottom}}}}} | {{#if:{{{header-lines|}}}|{{repeat|{{#expr:{{{header-lines}}}-1}}|&lt;br&gt;}}}}{{{header-text|}}}<br /> ]]<br /> local heightClass = &quot;static-rank-col&quot;<br /> if rowHeight and rowHeight == &quot;large&quot; then<br /> heightClass = &quot;static-rank-col-large&quot;<br /> end<br /> <br /> <br /> local output = &#039;\n{| class=&quot;&#039;..heightClass..&#039;&quot;&#039; --start static rank table<br /> if caption then<br /> output = output .. &quot;\n|+&#039;&#039;&#039;&quot; .. caption .. &quot;&#039;&#039;&#039;&quot;<br /> end<br /> output = output .. &#039;\n|valign=&#039; .. valign .. &#039; |&#039;<br /> .. &#039;\n{| class=&quot;wikitable&quot; style=&quot;margin-right:&#039;..marginRight<br /> ..&#039;; padding:&#039;..headerPadding<br /> ..&#039;; text-align:&#039;..textAlign <br /> ..&#039;;&#039;.. style<br /> .. &#039;\n! style=&quot;height:&#039;..headerHeight..&#039;;&quot; valign=&quot;&#039;..headerValign ..&#039;;&quot; | &#039; <br /> .. linebreaks .. headerText<br /> <br /> --[[ {{#ifexpr:{{{rows}}}=0|&lt;br /&gt;<br /> {{end}}}}{{#ifexpr:{{{rows}}}&gt;=1|{{Static column row |row-height={{{row-height|}}} |number=1 |row-header={{{row-header| }}} }}}}{{#ifexpr:{{{rows}}}=1|&lt;br /&gt;<br /> {{end}}}}{{#ifexpr:{{{rows}}}&gt;=2|{{Static column row |row-height={{{row-height|}}} |number=2 |row-header={{{row-header| }}} }}}}{{#ifexpr:{{{rows}}}=2|&lt;br /&gt;<br /> {{end}}}<br /> ]]<br /> <br /> local i=0<br /> while i&lt;rows do<br /> i=i+1<br /> --output = output .. &#039;\n|-\n|&#039; .. tostring(i) -- simple unformatted version<br /> <br /> --[[ &lt;br /&gt;<br /> |- {{#if: {{{row-height|}}}|style=&quot;height:{{{row-height|}}}&quot;|}}<br /> {{#if: {{{row-header|}}} | ! | {{!}} }} {{{number}}}<br /> ]] <br /> -- version emulating Template:Static column row <br /> local rowStyle = &quot;&quot;<br /> if rowHeight then rowStyle = &#039;style=&quot;height:&#039;..rowHeight..&#039;;&quot; |&#039; end<br /> local cellType = &quot;|&quot;<br /> if rowHeader then cellType = &quot;!&quot; end<br /> output = output .. &#039;\n|-&#039; .. rowStyle <br /> .. &#039;\n&#039; .. cellType .. tostring(i) .. &#039;&lt;br /&gt;&#039;<br /> end<br /> <br /> output = output .. &#039;\n|}&#039; -- close the static rank table<br /> <br /> <br /> output = output .. &#039;\n|&#039; -- new cell for the main table<br /> <br /> --output = output .. &#039;\n|}&#039; -- unnecessary: the table will be closed with an {{end}} template<br /> <br /> return p.templateStyle( frame, &quot;Static column begin/styles.css&quot; ) .. output<br /> <br /> end<br /> function p.templateStyle( frame, src )<br /> return frame:extensionTag( &#039;templatestyles&#039;, &#039;&#039;, { src = src } );<br /> end<br /> return p</div> Guarapiranga