Modül:Nüfus

Vikipedi, özgür ansiklopedi
Modül belgelemesi[oluştur]
local tbl = {}

local function format_int(number)
	local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
    -- reverse the int-string and append a comma to all blocks of 3 digits
	int = int:reverse():gsub("(%d%d%d)", "%1.")
	
	if int:sub(-1) == '.'
	then
		int = int:sub(1, -2)
	end
	
	-- reverse the int-string back remove an optional comma and put the 
	-- optional minus and fractional part back
	return minus .. int:reverse():gsub("^,", "") .. fraction
end

function string:split(delimiter)
  local result = { }
  local from  = 1
  local delim_from, delim_to = string.find( self, delimiter, from  )
  while delim_from do
    table.insert( result, string.sub( self, from , delim_from-1 ) )
    from  = delim_to + 1
    delim_from, delim_to = string.find( self, delimiter, from  )
  end
  table.insert( result, string.sub( self, from  ) )
  return result
end

local function spairs(t, order)
    -- collect the keys
    local keys = {}
    for k in pairs(t) do keys[#keys+1] = k end

    -- if order function given, sort by it by passing the table and keys a, b,
    -- otherwise just sort the keys 
    if order then
        table.sort(keys, function(a, b) return order(t, a, b) end)
    else
        table.sort(keys)
    end

    -- return the iterator function
    local i = 0
    return function()
        i = i + 1
        if keys[i] then
            return keys[i], t[keys[i]]
        end
    end
end

function tbl.nufus(frame)
	local veri = require("Modül:Nüfus/veri")
	local tablo = {}
	local dunya_nufus = frame:expandTemplate{
				title = 'Worldpop',
				}
	
	for k, v in pairs(veri) do
		local nufus = v['nufus']
		
		if v['nufus_baz_tarihi'] ~= nil then
			local arr = string.split(v['nufus_baz_tarihi'], "-")
			
			local gun = frame:expandTemplate{
				title = 'Age in days',
				args = {arr[1], arr[2], arr[3]}
				}
				
			nufus = nufus + (v['nufus_artisi'] * gun)
		end
		
		tablo[k] = math.floor(nufus)
	end
	
	local bagimli_renk = '#CCCCCC'
	local sinirli_renk = '#F0E891'
	
	local res = frame:expandTemplate{
				title = 'Lejant',
				args = {bagimli_renk, 'Bağımlı bölge'}
				}
	
	res = res .. frame:expandTemplate{
				title = 'Lejant',
				args = {sinirli_renk, 'Sınırlı tanınan ülke'}
				}
	
	res = res .. '\n'
	res = res .. '{| class="wikitable sortable" width="100%"\n'
	res = res .. '|-\n'
	res = res .. '! Sıra\n'
	res = res .. '! Ülke\n'
	res = res .. '! Nüfus\n'
	res = res .. '! Tarih\n'
	res = res .. '! Oran\n'
	res = res .. '! Notlar\n'
	
	t = os.date("%Y-%m-%d")
	local bugun = frame:expandTemplate{
				title = 'Tarih',
				args = {t}
				}
	
	--Dünya
	res = res .. '|-\n'
	res = res .. '|  \n'
	res = res .. '| style="font-weight: bold" | Dünya\n'
	res = res .. '| style="font-weight: bold" align="right" | ' .. format_int(dunya_nufus) .. '\n'
	res = res .. '| style="font-weight: bold; white-space: nowrap" align="right" | ' .. bugun .. '\n'
	res = res .. '| style="font-weight: bold" align="right" | ' .. '%100\n'
	res = res .. '| ' .. '[[Şablon:Worldpop|bkz.]]' .. '\n'
	
	local i = 0
	for k, v in spairs(tablo,
		function(t, a, b) return tonumber(t[b]) < tonumber(t[a]) end) do
		
		local tarih = ""
		
		if veri[k]['nufus_baz_tarihi'] ~= nil then
			tarih = bugun
		else
			tarih = veri[k]['tarih']
		end
	
		local ulke = frame:expandTemplate{
				title = 'Bayrakülke',
				args = {k}
				}
		if veri[k]['bagli_ulke'] ~= nil then
			sira = '-'
			if veri[k]['bagli_ulke'] ~= '-' then
				ulke = ulke .. ' ' .. '([[' .. veri[k]['bagli_ulke'] .. ']])'
			end
		else
			i = i + 1
			sira = i
		end
	
		local oran = tonumber(string.format("%.2f", (100 * v) / dunya_nufus))
	
		res = res .. '|-'
		
		if veri[k]['bagli_ulke'] ~= nil then
			res = res .. ' style="background:'
			
			if veri[k]['bagli_ulke'] == '-' then
				res = res .. sinirli_renk
			else
				res = res .. bagimli_renk
			end
			
			res = res .. '"'
		end
		
		res = res .. '\n'
		res = res .. '| ' .. sira .. '\n'
		res = res .. '| ' .. ulke .. '\n'
		res = res .. '| align="right" | ' .. format_int(v) .. '\n'
		res = res .. '| style="white-space: nowrap" align="right" | ' .. tarih .. '\n'
		res = res .. '| align="right" | ' .. '%' .. oran .. '\n'
		res = res .. '| <small>' .. veri[k]['kaynak'] .. '</small>\n'
	end
	
	res = res .. '|}'
	
	return res
end

return tbl