Module:Tabs
From eSportsAmaze
More actions
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
function p.main(frame)
local args = frame:getParent().args
-- 1. Collect Tab Names
local tabNames = {}
local i = 1
while args[i] do
local t = text.trim(args[i])
if t ~= "" then table.insert(tabNames, t) end
i = i + 1
end
-- 2. INTELLIGENT ROOT DETECTION
local basePage = args.base
if not basePage then
basePage = title.prefixedText
local parts = text.split(title.prefixedText, '/')
local foundRoot = false -- Flag to track if we found the root
for idx, part in ipairs(parts) do
local cleanPart = part:gsub('_', ' ')
for _, tab in ipairs(tabNames) do
-- If we find a matching tab name in the URL (e.g. "The Grind")
-- and it's not "Overview"
if cleanPart == tab and tab:lower() ~= 'overview' then
-- The Root is the path up to this point
basePage = table.concat(parts, '/', 1, idx - 1)
foundRoot = true
break -- Break inner loop
end
end
if foundRoot then
break -- Break outer loop
end
end
end
-- 3. Build HTML
local isSub = (args.type == 'sub')
local containerClass = isSub and 'pastel-bar' or 'chrome-tabs-container'
local tabClass = isSub and 'pastel-pill' or 'chrome-tab'
local root = html.create('div'):addClass(containerClass)
for _, name in ipairs(tabNames) do
local target = ""
local cleanName = name:gsub(' ', '_')
if name:lower() == 'overview' then
target = basePage
else
target = basePage .. '/' .. cleanName
end
-- 4. ACTIVE STATE LOGIC
local isActive = false
if title.prefixedText == target then
-- Exact match? Always active.
isActive = true
elseif title.prefixedText:find(target .. '/', 1, true) == 1 then
-- Are we on a sub-sub-page? (e.g. .../The_Grind/Group_A)
-- YES, highlight this tab... UNLESS it is Overview.
if name:lower() ~= 'overview' then
isActive = true
end
end
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