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 13:07, 21 November 2025 by Astro-adm (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local mArguments
local p = {}

local function getArgNums(prefix, args)
    local nums = {}
    for k, v in pairs(args) do
        local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
        if num then
            table.insert(nums, tonumber(num))
        end
    end
    table.sort(nums)
    return nums
end

function p.navpills(frame)
    mArguments = require('Module:Arguments')
    if not frame then
        return 'Error: No frame passed to navpills'
    end
    local args = mArguments.getArgs(frame, {
        parentOnly = true
    })
    return p._navpills(args)
end

function p._navpills(args)
    if not args then
        return 'Missing arguments'
    end

    local html = mw.html.create('div'):addClass('template-navpills'):attr('role', 'navigation')

    if args["header"] then
        html:attr('data-attr', args["header"])
    end

    for _, i in ipairs(getArgNums('page', args)) do
        local page = args['page' .. i]
        if not page then
            break
        end

        if page == "!!placeholder!!" then
            local navpill = mw.html.create('div'):addClass('navpill-placeholder')
            html:node(navpill)
        else
            local big = args['big' .. i]
            local wide = args['wide' .. i]
            local navpill = mw.html.create('div'):addClass('template-navpill')

            local extraClass = args['extraClass' .. i]
            if big then
                navpill = navpill:addClass('navpill-big')
            end
            if wide then
                navpill = navpill:addClass('navpill-wide')
            end
            if extraClass then
                navpill = navpill:addClass(extraClass)
            end

            local image = args['image' .. i]
            if image then
                navpill:tag('div'):addClass('template-navpill-background'):wikitext(string.format(
                    '[[File:%s|x48px|link=]]', image))

            end

            local linkText = args['text' .. i] or page
            local url = args['url' .. i]

            if url then
                -- External link using wikitext format
                navpill:wikitext(string.format('[%s %s]', url, linkText))
            else
                -- Internal wiki link
                navpill:wikitext(string.format('[[%s|%s]]', page, linkText))
            end
            html:node(navpill)
        end
    end

    return tostring(html)
end

return p