Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Navpills: Difference between revisions

From AstRO
Created page with "local mArguments --initialize lazily local p = {} -- Returns a table containing the numbers of the arguments that exist -- for the specified prefix. For example, if the prefix was 'data', and -- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}. 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.s..."
 
No edit summary
 
Line 1: Line 1:
local mArguments --initialize lazily
local mArguments
local p = {}
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


-- Returns a table containing the numbers of the arguments that exist
function p.navpills(frame)
-- for the specified prefix. For example, if the prefix was 'data', and
    mArguments = require('Module:Arguments')
-- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
    if not frame then
local function getArgNums(prefix, args)
        return 'Error: No frame passed to navpills'
local nums = {}
    end
for k, v in pairs(args) do
    local args = mArguments.getArgs(frame, {
local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
        parentOnly = true
if num then table.insert(nums, tonumber(num)) end
    })
end
    return p._navpills(args)
table.sort(nums)
return nums
end
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')


--Implements {{Navpills}} from the frame
            local extraClass = args['extraClass' .. i]
function p.navpills( frame )
            if big then
mArguments = require( 'Module:Arguments' )
                navpill = navpill:addClass('navpill-big')
return p._navpills( mArguments.getArgs( frame ) )
            end
end
            if wide then
                navpill = navpill:addClass('navpill-wide')
            end
            if extraClass then
                navpill = navpill:addClass(extraClass)
            end


function p._navpills( args )
            local image = args['image' .. i]
if not args then
            if image then
return 'Missing arguments'
                navpill:tag('div'):addClass('template-navpill-background'):wikitext(string.format(
end
                    '[[File:%s|x48px|link=]]', image))


local html = mw.html.create( 'div' ):addClass( 'template-navpills' ):attr( 'role', 'navigation' )
            end
for i, _ in ipairs( getArgNums( 'page', args ) ) do
if not args[ 'page' .. i ] then return end
local num = tostring( i )
local navpill = mw.html.create('div'):addClass('template-navpill')


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


navpill:wikitext( string.format( '[[%s|%s]]', args[ 'page' .. num ], args[ 'text' .. num ] or args[ 'page' .. num ] ) )
            if url then
html:node(navpill)
                -- External link using wikitext format
end
                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 mw.getCurrentFrame():extensionTag{
    return tostring(html)
name = 'templatestyles', args = { src = 'Module:Navpills/styles.css' }
} .. tostring( html )
end
end


return p
return p

Latest revision as of 13:07, 21 November 2025

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