Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:ESTable

From Teriock
Revision as of 18:45, 23 January 2025 by Gpe (talk | contribs) (// via Wikitext Extension for VSCode)

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

local p = {}

-- Column mappings
local columns = {
    op = 1,
    flame_verbs = 2,
    flame_nouns = 3,
    storm_verbs = 4,
    storm_nouns = 5,
    life_verbs = 6,
    life_nouns = 7,
    necro_verbs = 8,
    necro_nouns = 9,
    nature_verbs = 10,
    nature_nouns = 11
}

-- Helper function to parse the source table
local function parseTable(frame)
    local source = mw.text.split(frame:preprocess('{{:MediaWiki:ESTable}}'), '\n')
    local data = {}
    
    for i, line in ipairs(source) do
        if line:match('^|%-') then
            -- Skip separator lines
        elseif line:match('^|') then
            local row = {}
            for cell in line:gmatch('||%s*([^||]+)%s*') do
                table.insert(row, cell:match('^%s*(.-)%s*$'))
            end
            if #row > 0 then
                table.insert(data, row)
            end
        end
    end
    
    return data
end

-- Helper function to trim whitespace and new lines
local function trim(s)
    return s:match('^%s*(.-)%s*$')
end

-- Main function to generate filtered table
function p.filter(frame)
    local args = frame.args
    local data = parseTable(frame)
    -- if true then
    --     return data
    -- end
    local selectedCols = {}

    -- Trim whitespace from args
    for k, v in pairs(args) do
        args[k] = trim(v)
    end

    -- Remove empty arguments
    for k, v in pairs(args) do
        if v == '' then
            args[k] = nil
        end
    end
    
    -- Determine which columns to show
    for arg, _ in pairs(args) do
        local col = arg:lower()
        if columns[col] then
            table.insert(selectedCols, {name = col, index = columns[col]})
        end
    end
    
    -- Sort columns by their original index
    table.sort(selectedCols, function(a, b) return a.index < b.index end)
    
    -- Generate output table
    local output = '{| class="wikitable"\n'
    
    -- Header row
    -- output = output .. '! OP'
    -- for _, col in ipairs(selectedCols) do
    --     local header = col.name:gsub("_", " "):gsub("^%l", string.upper)
    --     output = output .. ' !! ' .. header
    -- end
    -- output = output .. '\n'
    
    -- Data rows
    for _, row in ipairs(data) do
        if args.op and row[1] == args.op then
            output = output .. '|-\n| ' .. row[1]
            for _, col in ipairs(selectedCols) do
                output = output .. ' || ' .. row[col.index]
            end
            output = output .. '\n'
        end
    end
    
    output = output .. '|}'
    return output
end

return p