Module:Navbox: Difference between revisions
Appearance
Created page with "local p = {} p.navbox = function(frame, args) local args = args or frame:getParent().args local title = args.title or "Title" local above = args.above or "Above" local below = args.below or "Below" args[1] = "Group 1" args[2] = "+ Item 1" args[3] = "+ Item 2" local html = ([[<nowiki> <div class="navbox"> <table class="navbox-inner"> <tr> <th class="navbox-title" scope="col" colspan="3"> <div>%s</div> </th> </tr..." |
No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local args, groups | |||
local function parseGroups() | |||
local groups = {} | |||
local currentGroup = nil | |||
local | |||
local i = 1 | local i = 1 | ||
while args[i] ~= nil do | while args[i] ~= nil do | ||
local arg = args[i] | local arg = args[i] | ||
i = i + 1 | i = i + 1 | ||
if arg:sub(1, 2) == "+ " then | if arg:sub(1, 2) == "+ " then | ||
table.insert(currentGroup.items, arg:sub(3)) | |||
else | 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 | ||
end | |||
if | 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 | ||
end | |||
local function renderListRow(tbl, group, isOdd) | |||
local listClass = isOdd and "navbox-list1 navbox-list navbox-odd" or "navbox-list1 navbox-list navbox-even" | |||
local row = tbl:tag('tr') | |||
row:tag('th') | |||
:attr('scope', 'row') | |||
:attr('class', 'navbox-group') | |||
:wikitext(group.title) | |||
local listContent = table.concat(group.items, " ") | |||
row:tag('td') | |||
:attr('class', listClass) | |||
:attr('style', 'width:100%; padding:0px; margin:0px') | |||
: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 | end | ||
renderBelowRow(tbl) | |||
return | return tostring(navbox) | ||
end | end | ||
return p | return p |
Revision as of 00:50, 8 September 2025
Documentation for this module may be created at Module:Navbox/doc
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, 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 = isOdd and "navbox-list1 navbox-list navbox-odd" or "navbox-list1 navbox-list navbox-even"
local row = tbl:tag('tr')
row:tag('th')
:attr('scope', 'row')
:attr('class', 'navbox-group')
:wikitext(group.title)
local listContent = table.concat(group.items, " ")
row:tag('td')
:attr('class', listClass)
:attr('style', 'width:100%; padding:0px; margin:0px')
: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