Module:Navpills: Difference between revisions
From Aeronautica Official Wiki
More actions
m changed thumbnail size for better resolution |
m Just Adds colon instead of the whole ':Category:' |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local mArguments = require('Module:Arguments') | |||
local mArguments | |||
local p = {} | local p = {} | ||
-- Returns a table containing the numbers of the arguments that exist | -- Returns a table containing the numbers of the arguments that exist | ||
-- for the specified prefix | -- for the specified prefix. | ||
local function getArgNums(prefix, args) | 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 | end | ||
-- Function to determine if a page is a category | |||
local function isCategoryPage(pageName) | |||
local title = mw.title.new(pageName) | |||
return title and title.namespace == 14 -- Namespace 14 is "Category" | |||
end | |||
--Implements {{Navpills}} from the frame | --Implements {{Navpills}} from the frame | ||
function p.navpills( frame ) | function p.navpills(frame) | ||
mArguments = require('Module:Arguments') | |||
return p._navpills(mArguments.getArgs(frame, { trim = true, removeBlanks = false })) | |||
end | end | ||
function p._navpills( args ) | 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['cols'] then | |||
local num = tostring(args['cols']) | |||
html:addClass(string.format('template-navpills--col%s', num)) | |||
end | |||
if args['rows'] then | |||
local num = tostring(args['rows']) | |||
html:addClass(string.format('template-navpills--row%s', num)) | |||
end | |||
for i, _ in ipairs(getArgNums('page', args)) do | |||
if not args['page' .. i] then | |||
return | |||
end | |||
local num = tostring(i) | |||
local pageName = args['page' .. num] | |||
local text = args['text' .. num] or pageName | |||
local navpill = mw.html.create('div'):addClass('template-navpill') | |||
if args['image' .. i] then | |||
navpill:tag('div'):addClass('template-navpill-background') | |||
:wikitext(string.format('[[File:%s|x128px|link=]]', args['image' .. num])) | |||
end | |||
local linkTarget = pageName | |||
if isCategoryPage(pageName) then | |||
linkTarget = ':' .. pageName -- Ensure "Category:" prefix | |||
end | |||
navpill:wikitext(string.format('[[%s|%s]]', linkTarget, text)) | |||
html:node(navpill) | |||
end | |||
return mw.getCurrentFrame():extensionTag{ | |||
name = 'templatestyles', | |||
args = { src = 'Module:Navpills/styles.css' } | |||
} .. tostring(html) | |||
end | end | ||
return p | return p |
Latest revision as of 04:22, 2 May 2025
Documentation for this module may be created at Module:Navpills/doc
local mArguments = require('Module:Arguments')
local p = {}
-- Returns a table containing the numbers of the arguments that exist
-- for the specified prefix.
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 to determine if a page is a category
local function isCategoryPage(pageName)
local title = mw.title.new(pageName)
return title and title.namespace == 14 -- Namespace 14 is "Category"
end
--Implements {{Navpills}} from the frame
function p.navpills(frame)
mArguments = require('Module:Arguments')
return p._navpills(mArguments.getArgs(frame, { trim = true, removeBlanks = false }))
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['cols'] then
local num = tostring(args['cols'])
html:addClass(string.format('template-navpills--col%s', num))
end
if args['rows'] then
local num = tostring(args['rows'])
html:addClass(string.format('template-navpills--row%s', num))
end
for i, _ in ipairs(getArgNums('page', args)) do
if not args['page' .. i] then
return
end
local num = tostring(i)
local pageName = args['page' .. num]
local text = args['text' .. num] or pageName
local navpill = mw.html.create('div'):addClass('template-navpill')
if args['image' .. i] then
navpill:tag('div'):addClass('template-navpill-background')
:wikitext(string.format('[[File:%s|x128px|link=]]', args['image' .. num]))
end
local linkTarget = pageName
if isCategoryPage(pageName) then
linkTarget = ':' .. pageName -- Ensure "Category:" prefix
end
navpill:wikitext(string.format('[[%s|%s]]', linkTarget, text))
html:node(navpill)
end
return mw.getCurrentFrame():extensionTag{
name = 'templatestyles',
args = { src = 'Module:Navpills/styles.css' }
} .. tostring(html)
end
return p