Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 02:46, 25 January 2026 by Esportsamaze (talk | contribs)

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

local p = {}
local html = mw.html
local title = mw.title.getCurrentTitle()
local text = mw.text -- Load text module for trimming

function p.main(frame)
    local args = frame:getParent().args
    
    -- 1. Collect Tab Names (and TRIM whitespace)
    local tabNames = {}
    local i = 1
    while args[i] do
        -- mw.text.trim removes invisible spaces like " Overview " -> "Overview"
        local cleanName = text.trim(args[i])
        if cleanName ~= "" then
            table.insert(tabNames, cleanName)
        end
        i = i + 1
    end
    
    -- 2. Auto-Detect the Base (Root) Page
    local basePage = args.base
    if not basePage then
        local currentSub = text.trim(title.subpageText)
        local isTabSubpage = false
        
        for _, name in ipairs(tabNames) do
            if name == currentSub then
                isTabSubpage = true
                break
            end
        end
        
        if isTabSubpage then
            basePage = title.baseText -- Go up one level
        else
            basePage = title.prefixedText -- We are on the root
        end
    end

    -- 3. Setup Styles
    local isSub = (args.type == 'sub')
    local containerClass = isSub and 'pastel-bar' or 'chrome-tabs-container'
    local tabClass = isSub and 'pastel-pill' or 'chrome-tab'
    
    -- 4. Build the Tabs
    local root = html.create('div'):addClass(containerClass)
    
    for _, name in ipairs(tabNames) do
        local target = ""
        
        -- Fix: Check matches "Overview" exactly, even if case differs slightly
        if name:lower() == 'overview' then
            target = basePage
        else
            target = basePage .. '/' .. name
        end
        
        -- Determine Active State
        -- We compare the targets to the current page title
        local isActive = (title.prefixedText == target)
        
        local tabDiv = root:tag('div'):addClass(tabClass)
        if isActive then tabDiv:addClass('active') end
        
        tabDiv:wikitext('[[' .. target .. '|' .. name .. ']]')
    end
    
    return tostring(root)
end

return p