Module:Navbox: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 67: | Line 67: | ||
local paddedItems = {} | local paddedItems = {} | ||
for _,x in ipairs(group.items) do | for _,x in ipairs(group.items) do | ||
local needPadding = not string.find(x, "[[File:", 1, true) | -- we need to pad it if this is not the first element in the row and it isnt preceeded with an image | ||
local needPadding = #paddedItems>0 and not string.find(x, "[[File:", 1, true) | |||
table.insert(paddedItems, needPadding and (" "..x) or x) | table.insert(paddedItems, needPadding and (" "..x) or x) | ||
end | end | ||
Revision as of 11:44, 17 August 2026
Creates a navbox used as the bottom of most pages
local p = {}
local args, groups
local function parseGroups()
local groups = {}
local currentGroup = nil
local i = 1
while args[i] ~= nil do
local arg = args[i]
i = i + 1
if arg:sub(1, 2) == "+ " then
table.insert(currentGroup.items, mw.text.trim(arg:sub(3)))
else
currentGroup = { title = arg, items = {} }
table.insert(groups, currentGroup)
end
end
return groups
end
local function renderTitleRow(tbl)
tbl:tag('tr')
:tag('th')
:attr('class', 'navbox-title')
:attr('scope', 'col')
:attr('colspan', '3')
:tag('div')
:wikitext(args.title)
end
local function renderAboveRow(tbl)
if args.above and args.above ~= "" then
tbl:tag('tr')
:tag('td')
:attr('class', 'navbox-abovebelow')
:attr('colspan', '3')
:tag('div')
:wikitext(args.above)
end
end
local function renderBelowRow(tbl)
if args.below and args.below ~= "" then
tbl:tag('tr')
:tag('td')
:attr('class', 'navbox-abovebelow')
:attr('colspan', '3')
:tag('div')
:wikitext(args.below)
end
end
local function renderListRow(tbl, group, isOdd)
local listClass = "navbox-list1 navbox-list"
listClass = listClass..(isOdd and "navbox-odd" or "navbox-even")
local row = tbl:tag('tr')
row:tag('th')
:attr('scope', 'row')
:addClass('navbox-group')
:wikitext(group.title)
local paddedItems = {}
for _,x in ipairs(group.items) do
-- we need to pad it if this is not the first element in the row and it isnt preceeded with an image
local needPadding = #paddedItems>0 and not string.find(x, "[[File:", 1, true)
table.insert(paddedItems, needPadding and (" "..x) or x)
end
local listContent = table.concat(paddedItems, " ")
mw.log(listContent)
row:tag('td')
:addClass(listClass)
:cssText('width:100%; padding:0 0 0 0.3rem; margin:0px; border-left-width:0;')
:tag('div')
:wikitext(listContent)
end
p.navbox = function(frame)
args = frame:getParent().args
groups = parseGroups()
local navbox = mw.html.create('div')
:addClass('navbox')
local tbl = navbox:tag('table')
:addClass('nowraplinks')
:addClass("navbox-inner")
:addClass(args.bodyclass)
renderTitleRow(tbl)
renderAboveRow(tbl)
for i, group in ipairs(groups) do
renderListRow(tbl, group, i % 2 == 1)
end
renderBelowRow(tbl)
return tostring(navbox)
end
return p