Ugrás a tartalomhoz

Modul:Demográfiai változások

Ellenőrzött
A Wikipédiából, a szabad enciklopédiából

Demográfiai változások[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: létrehozás]

local p = {}

-- function to create and print wikitable from template data
function p.f(frame)
    parent = frame:getParent()
    args = parent.args
    t={}
    head = ''
    row = ''

    for n,v in parent:argumentPairs() do
        if n ~= 'source' then
            table.insert (t,n,v)
        end
    end
    
    for n, v in p.orderedPairs(t) do
        if v ~= nil and p.trim12(v) ~= '' then
            head = head..'!'..n..'\n'
            row = row..'|'..v..'\n'
        end
    end

    wikitable = '{|class="wikitable"\n'
    wikitable = wikitable..'|+ Demográfiai változások'
    if args["source"] ~= nil then
        wikitable = wikitable.."<br><small>Forrás: "..args["source"].."</small>"
    end
    wikitable = wikitable..'\n'..head..'|-\n'..row
    wikitable = wikitable..'\n|}'
    
    return wikitable
    
end


-- here come the sorting (and trim) functions needed because of the defections of this shit called Lua

function p.trim12(s)
 local from = s:match"^%s*()"
 return from > #s and "" or s:match(".*%S", from)
end

function p.__genOrderedIndex( t )
    local orderedIndex = {}
    for key in pairs(t) do
        table.insert( orderedIndex, key )
    end
    table.sort( orderedIndex )
    return orderedIndex
end

function p.orderedNext(t, state)
    -- Equivalent of the next function, but returns the keys in the alphabetic
    -- order. We use a temporary ordered key table that is stored in the
    -- table being iterated.

    --print("orderedNext: state = "..tostring(state) )
    if state == nil then
        -- the first time, generate the index
        t.__orderedIndex = p.__genOrderedIndex( t )
        key = t.__orderedIndex[1]
        return key, t[key]
    end
    -- fetch the next value
    key = nil
    for i = 1,table.getn(t.__orderedIndex) do
        if t.__orderedIndex[i] == state then
            key = t.__orderedIndex[i+1]
        end
    end

    if key then
        return key, t[key]
    end

    -- no more value to return, cleanup
    t.__orderedIndex = nil
    return
end

function p.orderedPairs(t)
    -- Equivalent of the pairs() function on tables. Allows to iterate
    -- in order
    return p.orderedNext, t, nil
end

return p