FANDOM


-- Loops module, used for element iterators
-- Fork of w:c:undertale:Module:Loops by KockaAdmiralac
-- <nowiki>
local p = {}
 
function template_args()
    return mw.getCurrentFrame():getParent().args
end
 
function DUMP(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. DUMP(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end
 
function p.numeric(frame)
    local args = template_args()
    local cols = frame.args[1]
    local template = frame.args[2]
    local ret = ''
    local data = {}
    local del = 0
    if frame.args[3] then
        del = tonumber(frame.args[3])
    end
    for i, v in ipairs(args) do
        local ind = i - del
        local index = math.ceil(ind / cols)
        if ind > 0 then
            if not(data[index]) then
                data[index] = {}
            end
            data[index][((ind - 1) % cols) + 1] = v
        end
    end
    for i, v in ipairs(data) do
        ret = ret .. '{' .. '{' .. template .. '/element|'
        for i2, v2 in ipairs(v) do
            ret = ret .. mw.text.trim(v2) ..  '|'
        end
        ret = ret .. '}}'
    end
    return frame:preprocess(ret)
end
 
function p.labeled(frame)
    local args = template_args()
    local template = frame.args[1]
    local ret = ''
    local data = {}
    for i, v in ipairs(args) do
        local ind = mw.text.trim(string.match(i, '%d+'))
        local par = mw.text.trim(string.match(i, '%D+'))
        if not(data[ind]) then
            data[ind] = {}
        end
        data[ind][par] = v
    end
    for i, v in ipairs(data) do
        ret = ret .. '{' .. '{' .. template .. '/element'
        for i2, v2 in ipairs(v) do
            ret = ret .. mw.text.trim(i2) .. '=' .. mw.text.trim(v2) .. '|'
        end
        ret = ret .. '}}'
    end
    return DUMP(data)
end
 
return p
-- </nowiki>