Module:Navpills: Difference between revisions
From AstRO
More actions
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 | 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 | |||
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 | 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 | |||
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 | 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