Module:TableGenerator: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Sur (talk | contribs)
m Blanked the page
Tag: Blanking
Sur (talk | contribs)
mNo edit summary
Line 1: Line 1:
local p = {}


function p.generateTable(frame)
    local args = frame:getParent().args
    local cols = tonumber(args["cols"]) or 1  -- Default to 1 column if no value is passed
    local output = {}
    local row = {}
   
    table.insert(output, '{| class="wikitable"')  -- Start of the table
   
    for i, v in ipairs(args) do
        -- Add the value to the current row
        table.insert(row, string.format('| %s', v))
        -- If we have enough columns, close the row and add it to the output
        if #row == cols then
            table.insert(output, '|-\n' .. table.concat(row, ' || '))
            row = {}  -- Reset the row
        end
    end
    -- Add any remaining data (in case the last row is incomplete)
    if #row > 0 then
        table.insert(output, '|-\n' .. table.concat(row, ' || '))
    end
   
    table.insert(output, '|}')  -- End of the table
   
    return table.concat(output, '\n')
end
return p

Revision as of 21:25, 14 September 2024

Documentation for this module may be created at Module:TableGenerator/doc

local p = {}

function p.generateTable(frame)
    local args = frame:getParent().args
    local cols = tonumber(args["cols"]) or 1  -- Default to 1 column if no value is passed
    local output = {}
    local row = {}
    
    table.insert(output, '{| class="wikitable"')  -- Start of the table
    
    for i, v in ipairs(args) do
        -- Add the value to the current row
        table.insert(row, string.format('| %s', v))

        -- If we have enough columns, close the row and add it to the output
        if #row == cols then
            table.insert(output, '|-\n' .. table.concat(row, ' || '))
            row = {}  -- Reset the row
        end
    end

    -- Add any remaining data (in case the last row is incomplete)
    if #row > 0 then
        table.insert(output, '|-\n' .. table.concat(row, ' || '))
    end
    
    table.insert(output, '|}')  -- End of the table
    
    return table.concat(output, '\n')
end

return p