239
edits
(remove titlegroup per templatestyles section on talk page) |
m (1 revision imported from templatewiki:Module:Navbox) Tag: Reverted |
||
Line 1:
--
-- This module implements {{Navbox}}
--
local p = {}
local navbar = require('Module:Navbar')._navbar
local getArgs -- lazily initialized
local args
local
local listnums
local ODD_EVEN_MARKER = '\127_ODDEVEN_\127'
local RESTART_MARKER = '\127_ODDEVEN0_\127'
local REGEX_MARKER = '\127_ODDEVEN(%d?)_\127'
local function striped(wikitext
-- Return wikitext with markers replaced for odd/even striping.
-- Child (subgroup) navboxes are flagged with a category that is removed
-- by parent navboxes. The result is that the category shows all pages
-- where a child navbox is not contained in a parent navbox.
local orphanCat =
if border ==
-- No change; striping occurs in outermost navbox.
return wikitext .. orphanCat
end
local first, second =
if args
if args
first, second = second, first
else
first = args
second = first
end
Line 44 ⟶ 53:
end
local regex = orphanCat:gsub('([%[%]])', '%%%1')
return (wikitext:gsub(regex, ''):gsub(
end
Line 53 ⟶ 62:
return '\n' .. item ..'\n'
end
if nowrapitems ==
local lines = {}
for line in (item .. '\n'):gmatch('([^\n]*)\n') do
local prefix, content = line:match('^([*:;#]+)%s*(.*)')
if prefix and not content:match(
line =
end
table.insert(lines, line)
Line 70 ⟶ 79:
end
local function renderNavBar(titleCell)
if args.navbar ~= 'off' and args.navbar ~= 'plain' and not (not args.name and mw.getCurrentFrame():getParent():getTitle():gsub('/sandbox$', '') == 'Template:Navbox') then
titleCell:wikitext(navbar{
})
end
Line 94 ⟶ 91:
end
--
-- Title row
--
local function renderTitleRow(tbl)
if not args
local titleRow = tbl:tag('tr')
if args.titlegroup then
titleRow
:tag('th')
:attr('scope', 'row')
:addClass('navbox-group')
:addClass(args.titlegroupclass)
:cssText(args.basestyle)
:cssText(args.groupstyle)
:cssText(args.titlegroupstyle)
:wikitext(args.titlegroup)
end
local titleCell = titleRow:tag('th'):attr('scope', 'col')
if args.titlegroup then
titleCell
:css('border-left', '2px solid #fdfdfd')
:css('width', '100%')
end
local titleColspan = 2
if args
if args
if args.titlegroup then titleColspan = titleColspan - 1 end
titleCell
:cssText(args
:cssText(args
:addClass(
:attr('colspan', titleColspan)
Line 116 ⟶ 135:
:tag('div')
-- id for aria-labelledby attribute
:attr('id', mw.uri.anchorEncode(args
:addClass(args
:css('font-size', '114%')
:css('margin', '0 4em')
:wikitext(processItem(args
end
--
-- Above/Below rows
--
local function getAboveBelowColspan()
local ret = 2
if args
if args
return ret
end
local function renderAboveRow(tbl)
if not args
tbl:tag('tr')
:tag('td')
:addClass(
:addClass(args
:cssText(args
:cssText(args
:attr('colspan', getAboveBelowColspan())
:tag('div')
-- id for aria-labelledby attribute, if no title
:attr('id', args
:wikitext(processItem(args
end
local function renderBelowRow(tbl)
if not args
tbl:tag('tr')
:tag('td')
:addClass(
:addClass(args
:cssText(args
:cssText(args
:attr('colspan', getAboveBelowColspan())
:tag('div')
:wikitext(processItem(args
end
--
-- List rows
--
local function renderListRow(tbl, index, listnum)
local row = tbl:tag('tr')
if index == 1 and args
row
:tag('td')
:addClass(
:addClass(
:css('width', '1px') -- Minimize width
:css('padding', '
:cssText(args
:attr('rowspan',
:tag('div')
:wikitext(processItem(args
end
if args['group' .. listnum] then
local groupCell = row:tag('th')
-- id for aria-labelledby attribute, if lone group with no title or above
if listnum == 1 and not (args
groupCell
:attr('id', mw.uri.anchorEncode(args
end
groupCell
:attr('scope', 'row')
:addClass(
:addClass(args
:cssText(args
:css('width', args.groupwidth or '1%') -- If groupwidth not specified, minimize width
groupCell
:cssText(args
:cssText(args[
:wikitext(args[
end
local listCell = row:tag('td')
if args[
listCell
:css('text-align', 'left')
:css('border-left-width', '2px')
:css('border-left-style', 'solid')
else
listCell:attr('colspan', 2)
end
if not args
listCell:css('width', '100%')
end
Line 217 ⟶ 241:
local rowstyle -- usually nil so cssText(rowstyle) usually adds nothing
if index % 2 == 1 then
rowstyle = args
else
rowstyle = args
end
local
local
if listText:sub(1, 12) == '</div><table' then
-- Assume list text is for a subgroup navbox so no automatic striping for this row.
oddEven = listText:find(
end
listCell
:css('padding', '
:cssText(args
:cssText(rowstyle)
:cssText(args[
:addClass(
:addClass(
:addClass(args
:addClass(args[
:tag('div')
:css('padding', (index == 1 and args.list1padding) or args.listpadding or '0em 0.25em')
:wikitext(processItem(listText, args.nowrapitems))
if index == 1 and args
row
:tag('td')
:addClass(
:addClass(
:css('width', '1px') -- Minimize width
:css('padding', '
:cssText(args
:attr('rowspan',
:tag('div')
:wikitext(processItem(args
end
end
--
-- Tracking categories
--
local function needsHorizontalLists()
if border == 'subgroup' or args.tracking == 'no' then
return false
end
local listClasses = {
['plainlist'] = true, ['hlist'] = true, ['hlist hnum'] = true,
['hlist hwrap'] = true, ['hlist vcard'] = true, ['vcard hlist'] = true,
['hlist vevent'] = true,
}
return not (listClasses[args.listclass] or listClasses[args.bodyclass])
end
local function hasBackgroundColors()
for _, key in ipairs({
if tostring(args[key]):find('background', 1, true) then
return true
end
end
end
local function hasBorders()
for _, key in ipairs({
if tostring(args[key]):find('border', 1, true) then
return true
end
end
end
local function isIllegible()
local styleratio = require('Module:Color contrast')._styleratio
for key, style in pairs(args) do
if tostring(key):match(
if styleratio{mw.text.unstripNoWiki(style)} < 4.5 then
return true
Line 326 ⟶ 325:
end
local function getTrackingCategories(
local cats = {}
if needsHorizontalLists(
if hasBackgroundColors() then table.insert(cats,
if isIllegible() then table.insert(cats,
if hasBorders() then table.insert(cats,
return cats
end
local function renderTrackingCategories(builder
local title = mw.title.getCurrentTitle()
if title.namespace ~= 10 then return end -- not in template space
local subpage = title.subpageText
if subpage ==
for _, cat in ipairs(getTrackingCategories(
builder:wikitext('[[Category:' .. cat .. ']]')
end
end
--
-- Main navbox tables
--
local function renderMainTable()
local tbl = mw.html.create('table')
:addClass(
:addClass(args
tbl
:addClass(
:addClass(args.state or
end
tbl:css('border-spacing', 0)
if border ==
tbl
:addClass(
:cssText(args
:cssText(args
else -- regular navbox - bodystyle and style will be applied to the wrapper table
tbl
:addClass(
:css('background', 'transparent')
:css('color', 'inherit')
end
tbl:cssText(args
renderTitleRow(tbl)
renderAboveRow(tbl)
for i, listnum in ipairs(listnums) do
renderListRow(tbl, i, listnum
end
renderBelowRow(tbl)
return tbl
end
function p._navbox(navboxArgs)
args = navboxArgs
for k, _ in pairs(args) do
if type(k) == 'string' then
local listnum = k:match(
if listnum then table.insert(listnums, tonumber(listnum)) end
end
Line 429 ⟶ 396:
table.sort(listnums)
if border ==
border =
end
-- render the main body of the navbox
local tbl = renderMainTable(
-- render the appropriate wrapper around the navbox, depending on the border param
local res = mw.html.create()
if border == 'none' then
local nav = res:tag('div')
:attr('role', 'navigation')
:node(tbl)
-- aria-labelledby title, otherwise above, otherwise lone group
if args
nav:attr('aria-labelledby', mw.uri.anchorEncode(args.title or args.above or args.group1))
else
nav:attr('aria-label',
end
elseif border ==
-- We assume that this navbox is being rendered in a list cell of a parent navbox, and is
--
--
res
:wikitext('</div>')
Line 467 ⟶ 425:
:wikitext('<div>')
else
local nav = res:tag('div')
:attr('role', 'navigation')
:addClass(
:addClass(args
:cssText(args
:cssText(args
:css('padding', '3px')
:node(tbl)
-- aria-labelledby title, otherwise above, otherwise lone group
if args
nav:attr('aria-labelledby', mw.uri.anchorEncode(args.title or args.above or args.group1))
else
nav:attr('aria-label',
end
end
if (args
renderTrackingCategories(res
end
return striped(tostring(res)
end
Line 498 ⟶ 451:
getArgs = require('Module:Arguments').getArgs
end
args = getArgs(frame, {wrappers = {
if frame.args.border then
-- This allows Template:Navbox_subgroup to use {{#invoke:Navbox|navbox|border=...}}.
args.border = frame.args.border
end
-- Read the arguments in the order they'll be output in, to make references number in the right order.
local _
_ = args
_ = args
for i = 1, 20 do
_ = args[
_ = args[
end
_ = args
return p._navbox(args)
|