Jump to content

Module:WLRList

From Meta, a Wikimedia project coordination wiki
Module documentation
local p = {}

-- Function to create a category button with "active" detection
function p.categoryButton(frame)
    local targetLink = frame.args[1]
    local displayName = frame.args[2]
    local currentTitle = mw.title.getCurrentTitle().fullText
    
    -- Strip any trailing page numbers (e.g., /1, /10) to find the base category
    local baseTarget = targetLink:gsub("/%d+$", "")
    local isSelected = false
    
    -- Check if current page contains the base path
    if string.find(currentTitle, baseTarget, 1, true) then
        isSelected = true
    end

    local class = "hov_sec"
    if isSelected then class = "hov_sec selected" end

    return string.format('[[Special:MyLanguage/%s|<div style="padding: 0.7em;" class="%s">%s</div>]]', targetLink, class, displayName)
end

-- Function for the pagination
function p.makeHeader(frame)
    local currentTitle = mw.title.getCurrentTitle().fullText

    -- Normalize base path (remove /number and language codes)
    local categoryPath = currentTitle
        :gsub("/%d+$", "")
        :gsub("/%d+/[a-z][a-z]$", "")
        :gsub("/[a-z][a-z]$", "")

    local maxPages = 20
    local highestPage = 0

    -- Detect highest existing page
    for i = 1, maxPages do
        local subpage = mw.title.new(categoryPath .. '/' .. i)
        if subpage and subpage.exists then
            highestPage = i
        end
    end

    if highestPage <= 1 then
        return ""
    end

    -- Extract current page number safely
    local currentPage = tonumber(currentTitle:match("/(%d+)$"))

    local container = mw.html.create('div')
        :css({
            ['display'] = 'flex',
            ['justify-content'] = 'center',
            ['gap'] = '8px',
            ['margin'] = '15px 0',
            ['flex-wrap'] = 'wrap'
        })

    for i = 1, highestPage do
        local isCurrent = (currentPage == i)
        local subpagePath = categoryPath .. '/' .. i

        container:tag('div')
            :css({
                ['padding'] = '4px 14px',
                ['border-radius'] = '20px',
                ['border'] = '1px solid #d6cdc4',
                ['background-color'] = isCurrent and '#A68D6A' or '#f2f0eb',
                ['font-weight'] = isCurrent and 'bold' or 'normal'
            })
            :wikitext(string.format(
                "[[Special:MyLanguage/%s|<span style='color:%s;'>%d</span>]]",
                subpagePath,
                isCurrent and 'white' or '#202122',
                i
            ))
    end

    return tostring(container)
end
return p