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 23:03, 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,
    celestial_nouns = 2,
    celestial_verbs = 3,
    adjectives = 4,
    adverbs = 5,
    pronouns = 6,
    propositions = 7,
    conjunctives = 8,
    articles = 9,
    flame_verbs = 10,
    flame_nouns = 11,
    storm_verbs = 12,
    storm_nouns = 13,
    life_verbs = 14,
    life_nouns = 15,
    necro_verbs = 16,
    necro_nouns = 17,
    nature_verbs = 18,
    nature_nouns = 19
}

local backgrounds = {
    op = '#f9f06b1A',
    celestial_nouns = '#dc8add1A',
    celestial_verbs = '#dc8add1A',
    adjectives = '#dc8add1A',
    adverbs = '#dc8add1A',
    pronouns = '#dc8add1A',
    propositions = '#dc8add1A',
    conjunctives = '#dc8add1A',
    articles = '#dc8add1A',
    flame_verbs = '#f661511A',
    flame_nouns = '#f661511A',
    storm_verbs = '#99c1f11A',
    storm_nouns = '#99c1f11A',
    life_verbs = '#ffffff1A',
    life_nouns = '#ffffff1A',
    necro_verbs = '#77767b1A',
    necro_nouns = '#77767b1A',
    nature_verbs = '#8ff0a41A',
    nature_nouns = '#8ff0a41A'
}

local backgrounds_even = {
    op = '#e5a50a1A',
    celestial_nouns = '#6135831A',
    celestial_verbs = '#6135831A',
    adjectives = '#6135831A',
    adverbs = '#6135831A',
    pronouns = '#6135831A',
    propositions = '#6135831A',
    conjunctives = '#6135831A',
    articles = '#6135831A',
    flame_verbs = '#a51d2d1A',
    flame_nouns = '#a51d2d1A',
    storm_verbs = '#1a5fb41A',
    storm_nouns = '#1a5fb41A',
    life_verbs = '#9a99961A',
    life_nouns = '#9a99961A',
    necro_verbs = '#0000001A',
    necro_nouns = '#0000001A',
    nature_verbs = '#26a2691A',
    nature_nouns = '#26a2691A'
}

local links = {
    flame_verbs = '{{L|Class|Flame Mage|Flame Verbs}}',
    flame_nouns = '{{L|Class|Flame Mage|Flame Nouns}}',
    storm_verbs = '{{L|Class|Storm Mage|Storm Verbs}}',
    storm_nouns = '{{L|Class|Storm Mage|Storm Nouns}}',
    life_verbs = '{{L|Class|Life Mage|Life Verbs}}',
    life_nouns = '{{L|Class|Life Mage|Life Nouns}}',
    necro_verbs = '{{L|Class|Necromancer|Necro Verbs}}',
    necro_nouns = '{{L|Class|Necromancer|Necro Nouns}}',
    nature_verbs = '{{L|Class|Nature Mage|Nature Verbs}}',
    nature_nouns = '{{L|Class|Nature Mage|Nature Nouns}}'
}

-- Helper function to parse the source table
local function parseTable(frame, output)
    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*$'))
                -- output = output .. cell .. '\n'
            end
            if #row > 0 then
                table.insert(data, row)
            end
        end
    end
    
    return data, output
end

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

local function supertrim(s)
    return s:match('^%s*(.-)%s*$'):gsub('%d', '')
end

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

    if args then
        -- 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 k, v in pairs(args) do
            if ((v ~= '') and columns[k]) then
                table.insert(selectedCols, {name = k, index = columns[k]})
            end
        end
    end

    local bg = (args.bg ~= '')

    -- Sort columns by their original index
    table.sort(selectedCols, function(a, b) return a.index < b.index end)

    -- for _, col in ipairs(selectedCols) do
    --     output = output .. ' !! ' .. tostring(col.index)
    -- end

    -- output = output .. '\n\n'
    
    -- Generate output table
    output = output .. '{| class="mw-collapsible wikitable sortable"\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
    local header = true
    for _, row in ipairs(data) do
        if (#row > 0) and (#selectedCols > 0) then
            output = output .. '\n\n|-\n\n'
            local dontskip = false
            row_content = ''
            for _, col in ipairs(selectedCols) do
                if header then
                    d = '!'
                else
                    d = '|'
                end
                content = d
                op = tonumber(row[1]) or 0
                if bg then
                    if op % 2 == 0 then
                        content = content .. ' style="background-color: ' .. backgrounds_even[col.name] .. '" |'
                    else
                        content = content .. ' style="background-color: ' .. backgrounds[col.name] .. '" |'
                    end
                    -- content = content .. ' style="background-color: ' .. backgrounds[col.name] .. '" |'
                end
                word = tostring(row[col.index])
                if links[word] then
                    word = frame:preprocess(links[word])
                end
                if supertrim(word) ~= '' then
                    dontskip = true
                end
                content = content .. ' ' .. word .. ' ' .. d
                row_content = row_content .. content
            end
            if dontskip then
                output = output .. row_content
            end
            output = output:sub(1, -3)
            -- output = output .. '\n'
        end
        if header then
            header = false
        end
    end
    
    output = output .. '\n|}'
    return output
end

return p