Modulo:Navbox: differenze tra le versioni

Da Semi del Verbo, l'enciclopedia dell'influenza del Vangelo sulla cultura
Contenuto cancellato Contenuto aggiunto
Undid revision 948472523 by [[Special:Contributions/w>MusikAnimal|w>MusikAnimal]] ([[User talk:w>MusikAnimal|talk]])
 
m una versione importata
 
(6 versioni intermedie di 3 utenti non mostrate)
Riga 1: Riga 1:
--
--[[
* Modulo che implementa i template Navbox e Navbox_subgroup.
-- This module implements {{Navbox}}
--
]]--


require('strict')
local p = {}


local navbar = require('Module:Navbar')._navbar
local getArgs = require('Modulo:Arguments').getArgs
-- Numero massimo di liste e gruppi per i template Navbox e Navbox_subgroup
local getArgs -- lazily initialized
local MAX_LIST_NAVBOX = 30
local MAX_LIST_NAVBOX_SUBGROUP = 20


-- =============================================================================
local args
-- Funzioni di utilità
local border
-- =============================================================================
local listnums
local ODD_EVEN_MARKER = '\127_ODDEVEN_\127'
local RESTART_MARKER = '\127_ODDEVEN0_\127'
local REGEX_MARKER = '\127_ODDEVEN(%d?)_\127'


-- Restituisce una sequence Lua ordinata contenente gli ID dei listN presenti.
local function striped(wikitext)
-- Se withGroup è true, controlla anche i groupN.
-- Return wikitext with markers replaced for odd/even striping.
--
-- Child (subgroup) navboxes are flagged with a category that is removed
-- @param {table} args
-- by parent navboxes. The result is that the category shows all pages
-- @param {boolean} withGroup
-- where a child navbox is not contained in a parent navbox.
-- @return {table}
local orphanCat = '[[Category:Navbox orphans]]'
local function getIds(args, withGroup)
if border == 'subgroup' and args.orphan ~= 'yes' then
local ret, ids = {}, {}
-- No change; striping occurs in outermost navbox.
for key, _ in pairs(args) do
return wikitext .. orphanCat
if type(key) == 'string' then
end
local id = key:match('^list(%d+)$') or (withGroup and key:match('^group(%d+)$'))
local first, second = 'odd', 'even'
if id and tonumber(id) <= (withGroup and MAX_LIST_NAVBOX or MAX_LIST_NAVBOX_SUBGROUP) then
if args.evenodd then
ids[tonumber(id)] = true
if args.evenodd == 'swap' then
first, second = second, first
else
first = args.evenodd
second = first
end
end
local changer
if first == second then
changer = first
else
local index = 0
changer = function (code)
if code == '0' then
-- Current occurrence is for a group before a nested table.
-- Set it to first as a valid although pointless class.
-- The next occurrence will be the first row after a title
-- in a subgroup and will also be first.
index = 0
return first
end
end
index = index + 1
return index % 2 == 1 and first or second
end
end
end
end
for key, _ in pairs(ids) do
local regex = orphanCat:gsub('([%[%]])', '%%%1')
table.insert(ret, key)
return (wikitext:gsub(regex, ''):gsub(REGEX_MARKER, changer)) -- () omits gsub count
end

local function processItem(item, nowrapitems)
if item:sub(1, 2) == '{|' then
-- Applying nowrap to lines in a table does not make sense.
-- Add newlines to compensate for trim of x in |parm=x in a template.
return '\n' .. item ..'\n'
end
end
table.sort(ret)
if nowrapitems == 'yes' then
return ret
local lines = {}
for line in (item .. '\n'):gmatch('([^\n]*)\n') do
local prefix, content = line:match('^([*:;#]+)%s*(.*)')
if prefix and not content:match('^<span class="nowrap">') then
line = prefix .. '<span class="nowrap">' .. content .. '</span>'
end
table.insert(lines, line)
end
item = table.concat(lines, '\n')
end
if item:match('^[*:;#]') then
return '\n' .. item ..'\n'
end
return item
end
end


-- Rimuove eventuali spazi/a capo attorno ai {{,}}.
local function renderNavBar(titleCell)
--

-- @param {string} list
if args.navbar ~= 'off' and args.navbar ~= 'plain' and not (not args.name and mw.getCurrentFrame():getParent():getTitle():gsub('/sandbox$', '') == 'Template:Navbox') then
-- @return {string}
titleCell:wikitext(navbar{
local function trimSep(list)
args.name,
local sep = mw.getCurrentFrame():expandTemplate{ title = "," }
mini = 1,
local sepEsc = mw.ustring.gsub(sep, '-', '%-')
fontstyle = (args.basestyle or '') .. ';' .. (args.titlestyle or '') .. ';background:none transparent;border:none;-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none; padding:0;'
return mw.ustring.gsub(list, '%s*' .. sepEsc .. '%s*', sep)
})
end

end
end


-- Con il debug ridefinisce il metodo mw.html:css,
-- permettendo di eseguire i test senza controllare anche il CSS.
--
--
-- @param {table} tableNode
-- Title row
local function disableCSS(tableNode)
--
local function renderTitleRow(tbl)
local mt = getmetatable(tableNode)
if not args.title then return end
mt.__index.css = function(t, name, val) return t end

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.imageleft then titleColspan = titleColspan + 1 end
if args.image then titleColspan = titleColspan + 1 end
if args.titlegroup then titleColspan = titleColspan - 1 end

titleCell
:cssText(args.basestyle)
:cssText(args.titlestyle)
:addClass('navbox-title')
:attr('colspan', titleColspan)

renderNavBar(titleCell)

titleCell
:tag('div')
-- id for aria-labelledby attribute
:attr('id', mw.uri.anchorEncode(args.title))
:addClass(args.titleclass)
:css('font-size', '114%')
:css('margin', '0 4em')
:wikitext(processItem(args.title))
end
end


-- Verifica se il template è elaborato nella sua pagina
--
local function isTemplatePage(name)
-- Above/Below rows
local title = mw.title.getCurrentTitle().prefixedText
--
name = 'Template:' .. (name or '')

return name == title and true or false
local function getAboveBelowColspan()
local ret = 2
if args.imageleft then ret = ret + 1 end
if args.image then ret = ret + 1 end
return ret
end
end


-- Carica il CSS via TemplateStyles quando opportuno
local function renderAboveRow(tbl)
local function loadCSS(name)
if not args.above then return end
local prefix = isTemplatePage(name) and 'mobile-' or ''

local styles = 'Modulo:Navbox/' .. prefix .. 'styles.css'
tbl:tag('tr')
return mw.getCurrentFrame():extensionTag{
:tag('td')
name = 'templatestyles',
:addClass('navbox-abovebelow')
:addClass(args.aboveclass)
args = {src = styles}
}
:cssText(args.basestyle)
:cssText(args.abovestyle)
:attr('colspan', getAboveBelowColspan())
:tag('div')
-- id for aria-labelledby attribute, if no title
:attr('id', args.title and nil or mw.uri.anchorEncode(args.above))
:wikitext(processItem(args.above, args.nowrapitems))
end
end


-- =============================================================================
local function renderBelowRow(tbl)
-- Classe Navbox
if not args.below then return end
-- =============================================================================


local Navbox = {}
tbl:tag('tr')
:tag('td')
:addClass('navbox-abovebelow')
:addClass(args.belowclass)
:cssText(args.basestyle)
:cssText(args.belowstyle)
:attr('colspan', getAboveBelowColspan())
:tag('div')
:wikitext(processItem(args.below, args.nowrapitems))
end


-- Costruttore della classe Navbox.
--
--
-- @param {table} args - gli argomenti passati al modulo
-- List rows
-- @return {table} un nuovo oggetto Navbox
--
local function renderListRow(tbl, index, listnum)
function Navbox:new(args)
local row = tbl:tag('tr')
local self = {}
local thNode


setmetatable(self, { __index = Navbox })
if index == 1 and args.imageleft then
self.args = args
row
-- costruzione tabella HTML
:tag('td')
self.tableNode = mw.html.create('table')
:addClass('navbox-image')
if self.args.debug then
:addClass(args.imageclass)
disableCSS(self.tableNode)
:css('width', '1px') -- Minimize width
:css('padding', '0px 2px 0px 0px')
:cssText(args.imageleftstyle)
:attr('rowspan', #listnums)
:tag('div')
:wikitext(processItem(args.imageleft))
end
end
self:_setupTableNode()

-- prima riga: contiene la navbar e il titolo
if args['group' .. listnum] then
local groupCell = row:tag('th')
thNode = self.tableNode:tag('tr')
:tag('th')

:attr('colspan', self.args.image and '3' or '2')
-- id for aria-labelledby attribute, if lone group with no title or above
:cssText(self.args.titlestyle)
if listnum == 1 and not (args.title or args.above or args.group2) then
if self.args.navbar ~= 'plain' then
groupCell
self:_addTnavbar(thNode)
:attr('id', mw.uri.anchorEncode(args.group1))
end

groupCell
:attr('scope', 'row')
:addClass('navbox-group')
:addClass(args.groupclass)
:cssText(args.basestyle)
:css('width', args.groupwidth or '1%') -- If groupwidth not specified, minimize width

groupCell
:cssText(args.groupstyle)
:cssText(args['group' .. listnum .. 'style'])
:wikitext(args['group' .. listnum])
end
end
if self.args.title then

self:_addTitle(thNode)
local listCell = row:tag('td')

if args['group' .. listnum] then
listCell
:css('text-align', 'left')
:css('border-left-width', '2px')
:css('border-left-style', 'solid')
else
listCell:attr('colspan', 2)
end
end
-- eventuale riga per l'above

if not args.groupwidth then
if self.args.above then
self:_addAboveOrBelow(self.args.above, self.args.abovestyle)
listCell:css('width', '100%')
end
end
-- altre righe

self:_addLists()
local rowstyle -- usually nil so cssText(rowstyle) usually adds nothing
-- eventuale riga finale per il below
if index % 2 == 1 then
if self.args.below then
rowstyle = args.oddstyle
self:_addAboveOrBelow(self.args.below, self.args.belowstyle)
else
rowstyle = args.evenstyle
end
end


return self
local listText = args['list' .. listnum]
end
local oddEven = ODD_EVEN_MARKER
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('<th[^>]*"navbox%-title"') and RESTART_MARKER or 'odd'
end
listCell
:css('padding', '0px')
:cssText(args.liststyle)
:cssText(rowstyle)
:cssText(args['list' .. listnum .. 'style'])
:addClass('navbox-list')
:addClass('navbox-' .. oddEven)
:addClass(args.listclass)
:addClass(args['list' .. listnum .. 'class'])
:tag('div')
:css('padding', (index == 1 and args.list1padding) or args.listpadding or '0em 0.25em')
:wikitext(processItem(listText, args.nowrapitems))


-- Restituisce la tabella HTML.
if index == 1 and args.image then
--
row
-- @return {string}
:tag('td')
function Navbox:getHTML()
:addClass('navbox-image')
return tostring(self.tableNode)
:addClass(args.imageclass)
:css('width', '1px') -- Minimize width
:css('padding', '0px 0px 0px 2px')
:cssText(args.imagestyle)
:attr('rowspan', #listnums)
:tag('div')
:wikitext(processItem(args.image))
end
end
end


-- Configura gli stili CSS della tabella
function Navbox:_setupTableNode()
self.tableNode
:addClass(isTemplatePage(self.args.name) and 'navbox_mobile' or 'navbox')
:addClass('mw-collapsible')
:addClass(isTemplatePage(self.args.name) and 'autocollapse' or
self.args.state == 'collapsed' and 'mw-collapsed' or
self.args.state == 'autocollapse' and 'autocollapse' or
not self.args.state and 'autocollapse' or nil)
:addClass('noprint metadata')
:attr('id', 'navbox-' .. (self.args.name or ''))
:cssText(self.args.style)
:cssText(self.args.bodystyle)
end


-- Aggiunge il Tnavbar (collegamenti alla pagina del template, di discussione e modifica).
--
--
-- @param {table} node
-- Tracking categories
function Navbox:_addTnavbar(node)
--
local tnavbar = mw.getCurrentFrame():expandTemplate {

title = 'Tnavbar',
local function needsHorizontalLists()
args = {
if border == 'subgroup' or args.tracking == 'no' then
[1] = self.args.name,
return false
['mini'] = 1
end
}
local listClasses = {
['plainlist'] = true, ['hlist'] = true, ['hlist hnum'] = true,
['hlist hwrap'] = true, ['hlist vcard'] = true, ['vcard hlist'] = true,
['hlist vevent'] = true,
}
}
node:tag('div'):addClass('navbox_navbar'):wikitext(tnavbar)
return not (listClasses[args.listclass] or listClasses[args.bodyclass])
end
end


-- Imposta il titolo del navbox dal parametro "title".
local function hasBackgroundColors()
--
for _, key in ipairs({'titlestyle', 'groupstyle', 'basestyle', 'abovestyle', 'belowstyle'}) do
-- @param {table} node
if tostring(args[key]):find('background', 1, true) then
function Navbox:_addTitle(node)
return true
node:tag('span'):addClass('navbox_title'):wikitext(self.args.title)
end
end
end
end


-- Aggiunge la riga per i parametri "above" e "below".
local function hasBorders()
--
for _, key in ipairs({'groupstyle', 'basestyle', 'abovestyle', 'belowstyle'}) do
-- @param {string} arg
if tostring(args[key]):find('border', 1, true) then
-- @param {string} argStyle
return true
function Navbox:_addAboveOrBelow(arg, argStyle)
end
self.tableNode
end
:tag('tr')
:tag('th')
:attr('colspan', self.args.image and '3' or '2')
:addClass('navbox_abovebelow')
:cssText(argStyle)
:wikitext(arg)
end
end


-- Aggiunge una colonna per l'immagine.
local function isIllegible()
--
local styleratio = require('Module:Color contrast')._styleratio
-- @param {table} trNode
-- @param {number} rowspan
function Navbox:_addImage(trNode, rowspan)
trNode
:tag('td')
:attr('rowspan', rowspan)
:addClass('navbox_image')
:cssText(self.args.imagestyle)
:wikitext(self.args.image)
end


-- Aggiunge una nuova riga per ogni groupN/listN
for key, style in pairs(args) do
function Navbox:_addLists()
if tostring(key):match("style$") then
local rowIds, altStyle, altBackground
if styleratio{mw.text.unstripNoWiki(style)} < 4.5 then
-- crea una riga per ogni groupN/listN
return true
rowIds = getIds(self.args, true)
for i, id in ipairs(rowIds) do
local trNode = self.tableNode:tag('tr')
-- groupN
if self.args['group' .. id] then
trNode:tag('th')
:attr('colspan', self.args['list' .. id] and '1' or '2')
:addClass('navbox_group')
:cssText(self.args.groupstyle)
:cssText(self.args['group' .. id .. 'style'])
:wikitext(self.args['group' .. id])
end
-- listN
if self.args['list' .. id] then
local list = trimSep(self.args['list' .. id])
if (i % 2) == 0 then
altStyle = self.args.evenstyle
altBackground = 'navbox_even'
else
altStyle = self.args.oddstyle
altBackground = 'navbox_odd'
end
end
trNode:tag('td')
:attr('colspan', self.args['group' .. id] and '1' or '2')
:addClass('navbox_list')
:addClass(not self.args['group' .. id] and 'navbox_center' or nil)
:addClass(altBackground)
:cssText(self.args.liststyle)
:cssText(altStyle)
:cssText(self.args['list' .. id .. 'style'])
:wikitext(list)
end
if id == 1 and self.args.image then
self:_addImage(trNode, #rowIds)
end
end
end
end
return false
end
end


-- =============================================================================
local function getTrackingCategories()
-- Classe NavboxSubgroup
local cats = {}
-- =============================================================================
if needsHorizontalLists() then table.insert(cats, 'Navigational boxes without horizontal lists') end
if hasBackgroundColors() then table.insert(cats, 'Navboxes using background colours') end
if isIllegible() then table.insert(cats, 'Potentially illegible navboxes') end
if hasBorders() then table.insert(cats, 'Navboxes using borders') end
return cats
end


local NavboxSubgroup = {}
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 == 'doc' or subpage == 'sandbox' or subpage == 'testcases' then return end


-- Costruttore della classe NavboxSubgroup.
for _, cat in ipairs(getTrackingCategories()) do
builder:wikitext('[[Category:' .. cat .. ']]')
end
end

--
-- Main navbox tables
--
--
-- @param {table} args - gli argomenti passati al modulo
local function renderMainTable()
-- @return {table} un nuovo oggetto NavboxSubgroup
local tbl = mw.html.create('table')
function NavboxSubgroup:new(args)
:addClass('nowraplinks')
local self = {}
:addClass(args.bodyclass)


setmetatable(self, { __index = NavboxSubgroup })
if args.title and (args.state ~= 'plain' and args.state ~= 'off') then
self.args = args
if args.state == 'collapsed' then args.state = 'mw-collapsed' end
-- costruzione tabella HTML
tbl
self.tableNode = mw.html.create('table')
:addClass('mw-collapsible')
if self.args.debug then
:addClass(args.state or 'autocollapse')
disableCSS(self.tableNode)
end
end
self:_setupTableNode()
self:_addLists()


return self
tbl:css('border-spacing', 0)
end
if border == 'subgroup' or border == 'none' then
tbl
:addClass('navbox-subgroup')
:cssText(args.bodystyle)
:cssText(args.style)
else -- regular navbox - bodystyle and style will be applied to the wrapper table
tbl
:addClass('navbox-inner')
:css('background', 'transparent')
:css('color', 'inherit')
end
tbl:cssText(args.innerstyle)


-- Restituisce la tabella HTML.
renderTitleRow(tbl)
--
renderAboveRow(tbl)
-- @return {string}
for i, listnum in ipairs(listnums) do
function NavboxSubgroup:getHTML()
renderListRow(tbl, i, listnum)
return tostring(self.tableNode)
end
end
renderBelowRow(tbl)


-- Configura gli stili CSS della tabella.
return tbl
function NavboxSubgroup:_setupTableNode()
self.tableNode
:addClass('subnavbox')
:cssText(self.args.bodystyle)
end
end


-- Aggiunge una nuova riga per ogni groupN/listN.
function p._navbox(navboxArgs)
function NavboxSubgroup:_addLists()
args = navboxArgs
local listIds, altStyle
listnums = {}
-- crea una row per ogni listN

for k, _ in pairs(args) do
listIds = getIds(self.args)
for _, id in ipairs(listIds) do
if type(k) == 'string' then
local listnum = k:match('^list(%d+)$')
local trNode = self.tableNode:tag('tr')
local list = trimSep(self.args['list' .. id])
if listnum then table.insert(listnums, tonumber(listnum)) end
-- i groupN sono visibili solo se c'è la corrispettiva listN
if self.args['group' .. id] then
trNode:tag('th')
:addClass('subnavbox_group')
:cssText(self.args.groupstyle)
:wikitext(self.args['group' .. id])
end
if (id % 2) == 0 then
altStyle = self.args.evenstyle
else
altStyle = self.args.oddstyle
end
end
trNode:tag('td')
:attr('colspan', self.args['group' .. id] and '1' or '2')
:addClass(not self.args['group' .. id] and 'navbox_center' or nil)
:cssText(self.args.liststyle)
:cssText(altStyle)
:wikitext(list)
end
end
end
table.sort(listnums)


-- =============================================================================
border = mw.text.trim(args.border or args[1] or '')
-- Funzioni esportate
if border == 'child' then
-- =============================================================================
border = 'subgroup'
end


local p = {}
-- render the main body of the navbox
local tbl = renderMainTable()


-- Funzione per l'utilizzo da un altro modulo.
-- render the appropriate wrapper around the navbox, depending on the border param
function p._navbox(args)
local res = mw.html.create()
return loadCSS(args.name) .. Navbox:new(args):getHTML()
if border == 'none' then
end
local nav = res:tag('div')
:attr('role', 'navigation')
:node(tbl)
-- aria-labelledby title, otherwise above, otherwise lone group
if args.title or args.above or (args.group1 and not args.group2) then
nav:attr('aria-labelledby', mw.uri.anchorEncode(args.title or args.above or args.group1))
else
nav:attr('aria-label', 'Navbox')
end
elseif border == 'subgroup' then
-- We assume that this navbox is being rendered in a list cell of a parent navbox, and is
-- therefore inside a div with padding:0em 0.25em. We start with a </div> to avoid the
-- padding being applied, and at the end add a <div> to balance out the parent's </div>
res
:wikitext('</div>')
:node(tbl)
:wikitext('<div>')
else
local nav = res:tag('div')
:attr('role', 'navigation')
:addClass('navbox')
:addClass(args.navboxclass)
:cssText(args.bodystyle)
:cssText(args.style)
:css('padding', '3px')
:node(tbl)
-- aria-labelledby title, otherwise above, otherwise lone group
if args.title or args.above or (args.group1 and not args.group2) then
nav:attr('aria-labelledby', mw.uri.anchorEncode(args.title or args.above or args.group1))
else
nav:attr('aria-label', 'Navbox')
end
end


-- Funzione per l'utilizzo da un altro modulo.
if (args.nocat or 'false'):lower() == 'false' then
function p._navbox_subgroup(args)
renderTrackingCategories(res)
return NavboxSubgroup:new(args):getHTML()
end
return striped(tostring(res))
end
end


-- Funzione per il template {{Navbox}}.
function p.navbox(frame)
function p.navbox(frame)
return p._navbox(getArgs(frame, { parentOnly = true }))
if not getArgs then
end
getArgs = require('Module:Arguments').getArgs
end
args = getArgs(frame, {wrappers = {'Template:Navbox', 'Template:Navbox subgroup'}})
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.title
_ = args.above
for i = 1, 20 do
_ = args["group" .. tostring(i)]
_ = args["list" .. tostring(i)]
end
_ = args.below


-- Funzione per il template {{Navbox subgroup}}.
return p._navbox(args)
function p.navbox_subgroup(frame)
return p._navbox_subgroup(getArgs(frame, { parentOnly = true }))
end
end



Versione attuale delle 15:19, 26 ott 2022

Il modulo Navbox implementa le funzionalità dei template {{Navbox}} e {{Navbox subgroup}}.

Ha due sottopagine CSS: Modulo:Navbox/styles.css e Modulo:Navbox/mobile-styles.css.


--[[
* Modulo che implementa i template Navbox e Navbox_subgroup.
]]--

require('strict')

local getArgs = require('Modulo:Arguments').getArgs
-- Numero massimo di liste e gruppi per i template Navbox e Navbox_subgroup
local MAX_LIST_NAVBOX = 30
local MAX_LIST_NAVBOX_SUBGROUP = 20

-- =============================================================================
--                            Funzioni di utilità
-- =============================================================================

-- Restituisce una sequence Lua ordinata contenente gli ID dei listN presenti.
-- Se withGroup è true, controlla anche i groupN.
--
-- @param {table} args
-- @param {boolean} withGroup
-- @return {table}
local function getIds(args, withGroup)
	local ret, ids = {}, {}
	for key, _ in pairs(args) do
		if type(key) == 'string' then
			local id = key:match('^list(%d+)$') or (withGroup and key:match('^group(%d+)$'))
			if id and tonumber(id) <= (withGroup and MAX_LIST_NAVBOX or MAX_LIST_NAVBOX_SUBGROUP) then
				ids[tonumber(id)] = true
			end
		end
	end
	for key, _ in pairs(ids) do
		table.insert(ret, key)
	end
	table.sort(ret)
	return ret
end

-- Rimuove eventuali spazi/a capo attorno ai {{,}}.
--
-- @param {string} list
-- @return {string}
local function trimSep(list)
	local sep = mw.getCurrentFrame():expandTemplate{ title = "," }
	local sepEsc = mw.ustring.gsub(sep, '-', '%-')
	return mw.ustring.gsub(list, '%s*' .. sepEsc .. '%s*', sep)
end

-- Con il debug ridefinisce il metodo mw.html:css,
-- permettendo di eseguire i test senza controllare anche il CSS.
--
-- @param {table} tableNode
local function disableCSS(tableNode)
	local mt = getmetatable(tableNode)
	mt.__index.css = function(t, name, val) return t end
end

-- Verifica se il template è elaborato nella sua pagina
local function isTemplatePage(name)
	local title = mw.title.getCurrentTitle().prefixedText
	name = 'Template:' .. (name or '')
	return name == title and true or false
end

-- Carica il CSS via TemplateStyles quando opportuno
local function loadCSS(name)
	local prefix = isTemplatePage(name) and 'mobile-' or ''
	local styles = 'Modulo:Navbox/' ..  prefix .. 'styles.css'
	return mw.getCurrentFrame():extensionTag{
			name = 'templatestyles',
			args = {src = styles}
		}
end

-- =============================================================================
--                            Classe Navbox
-- =============================================================================

local Navbox = {}

-- Costruttore della classe Navbox.
--
-- @param {table} args - gli argomenti passati al modulo
-- @return {table} un nuovo oggetto Navbox
function Navbox:new(args)
	local self = {}
	local thNode

	setmetatable(self, { __index = Navbox })
	self.args = args
	-- costruzione tabella HTML
	self.tableNode = mw.html.create('table')
	if self.args.debug then
		disableCSS(self.tableNode)
	end
	self:_setupTableNode()
	-- prima riga: contiene la navbar e il titolo
	thNode = self.tableNode:tag('tr')
		:tag('th')
			:attr('colspan', self.args.image and '3' or '2')
			:cssText(self.args.titlestyle)
	if self.args.navbar ~= 'plain' then
		self:_addTnavbar(thNode)
	end
	if self.args.title then
		self:_addTitle(thNode)
	end
	-- eventuale riga per l'above
	if self.args.above then
		self:_addAboveOrBelow(self.args.above, self.args.abovestyle)
	end
	-- altre righe
	self:_addLists()
	-- eventuale riga finale per il below
	if self.args.below then
		self:_addAboveOrBelow(self.args.below, self.args.belowstyle)
	end

	return self
end

-- Restituisce la tabella HTML.
--
-- @return {string}
function Navbox:getHTML()
	return tostring(self.tableNode)
end

-- Configura gli stili CSS della tabella
function Navbox:_setupTableNode()
	self.tableNode
		:addClass(isTemplatePage(self.args.name) and 'navbox_mobile' or 'navbox')
		:addClass('mw-collapsible')
		:addClass(isTemplatePage(self.args.name) and 'autocollapse' or
				  self.args.state == 'collapsed' and 'mw-collapsed' or
				  self.args.state == 'autocollapse' and 'autocollapse' or
				  not self.args.state and 'autocollapse' or nil)
		:addClass('noprint metadata')
		:attr('id', 'navbox-' .. (self.args.name or ''))
		:cssText(self.args.style)
		:cssText(self.args.bodystyle)
end

-- Aggiunge il Tnavbar (collegamenti alla pagina del template, di discussione e modifica).
--
-- @param {table} node
function Navbox:_addTnavbar(node)
	local tnavbar = mw.getCurrentFrame():expandTemplate {
		title = 'Tnavbar',
		args = {
			[1] = self.args.name,
			['mini'] = 1
		}
	}
	node:tag('div'):addClass('navbox_navbar'):wikitext(tnavbar)
end

-- Imposta il titolo del navbox dal parametro "title".
--
-- @param {table} node
function Navbox:_addTitle(node)
	node:tag('span'):addClass('navbox_title'):wikitext(self.args.title)
end

-- Aggiunge la riga per i parametri "above" e "below".
--
-- @param {string} arg
-- @param {string} argStyle
function Navbox:_addAboveOrBelow(arg, argStyle)
	self.tableNode
		:tag('tr')
			:tag('th')
				:attr('colspan', self.args.image and '3' or '2')
				:addClass('navbox_abovebelow')
				:cssText(argStyle)
				:wikitext(arg)
end

-- Aggiunge una colonna per l'immagine.
--
-- @param {table} trNode
-- @param {number} rowspan
function Navbox:_addImage(trNode, rowspan)
	trNode
		:tag('td')
			:attr('rowspan', rowspan)
			:addClass('navbox_image')
			:cssText(self.args.imagestyle)
			:wikitext(self.args.image)
end

-- Aggiunge una nuova riga per ogni groupN/listN
function Navbox:_addLists()
	local rowIds, altStyle, altBackground
	-- crea una riga per ogni groupN/listN
	rowIds = getIds(self.args, true)
	for i, id in ipairs(rowIds) do
		local trNode = self.tableNode:tag('tr')
		-- groupN
		if self.args['group' .. id] then
			trNode:tag('th')
				:attr('colspan', self.args['list' .. id] and '1' or '2')
				:addClass('navbox_group')
				:cssText(self.args.groupstyle)
				:cssText(self.args['group' .. id .. 'style'])
				:wikitext(self.args['group' .. id])
		end
		-- listN
		if self.args['list' .. id] then
			local list = trimSep(self.args['list' .. id])
			if (i % 2) == 0 then
				altStyle = self.args.evenstyle
				altBackground = 'navbox_even'
			else
				altStyle = self.args.oddstyle
				altBackground = 'navbox_odd'
			end
			trNode:tag('td')
				:attr('colspan', self.args['group' .. id] and '1' or '2')
				:addClass('navbox_list')
				:addClass(not self.args['group' .. id] and 'navbox_center' or nil)
				:addClass(altBackground)
				:cssText(self.args.liststyle)
				:cssText(altStyle)
				:cssText(self.args['list' .. id .. 'style'])
				:wikitext(list)
		end
		if id == 1 and self.args.image then
			self:_addImage(trNode, #rowIds)
		end
	end
end

-- =============================================================================
--                            Classe NavboxSubgroup
-- =============================================================================

local NavboxSubgroup = {}

-- Costruttore della classe NavboxSubgroup.
--
-- @param {table} args - gli argomenti passati al modulo
-- @return {table} un nuovo oggetto NavboxSubgroup
function NavboxSubgroup:new(args)
	local self = {}

	setmetatable(self, { __index = NavboxSubgroup })
	self.args = args
	-- costruzione tabella HTML
	self.tableNode = mw.html.create('table')
	if self.args.debug then
		disableCSS(self.tableNode)
	end
	self:_setupTableNode()
	self:_addLists()

	return self
end

-- Restituisce la tabella HTML.
--
-- @return {string}
function NavboxSubgroup:getHTML()
	return tostring(self.tableNode)
end

-- Configura gli stili CSS della tabella.
function NavboxSubgroup:_setupTableNode()
	self.tableNode
		:addClass('subnavbox')
		:cssText(self.args.bodystyle)
end

-- Aggiunge una nuova riga per ogni groupN/listN.
function NavboxSubgroup:_addLists()
	local listIds, altStyle
	-- crea una row per ogni listN
	listIds = getIds(self.args)
	for _, id in ipairs(listIds) do
		local trNode = self.tableNode:tag('tr')
		local list = trimSep(self.args['list' .. id])
		-- i groupN sono visibili solo se c'è la corrispettiva listN
		if self.args['group' .. id] then
			trNode:tag('th')
				:addClass('subnavbox_group')
				:cssText(self.args.groupstyle)
				:wikitext(self.args['group' .. id])
		end
		if (id % 2) == 0 then
			altStyle = self.args.evenstyle
		else
			altStyle = self.args.oddstyle
		end
		trNode:tag('td')
			:attr('colspan', self.args['group' .. id] and '1' or '2')
			:addClass(not self.args['group' .. id] and 'navbox_center' or nil)
			:cssText(self.args.liststyle)
			:cssText(altStyle)
			:wikitext(list)
	end
end

-- =============================================================================
--                            Funzioni esportate
-- =============================================================================

local p = {}

-- Funzione per l'utilizzo da un altro modulo.
function p._navbox(args)
	return loadCSS(args.name) .. Navbox:new(args):getHTML()
end

-- Funzione per l'utilizzo da un altro modulo.
function p._navbox_subgroup(args)
	return NavboxSubgroup:new(args):getHTML()
end

-- Funzione per il template {{Navbox}}.
function p.navbox(frame)
	return p._navbox(getArgs(frame, { parentOnly = true }))
end

-- Funzione per il template {{Navbox subgroup}}.
function p.navbox_subgroup(frame)
	return p._navbox_subgroup(getArgs(frame, { parentOnly = true }))
end

return p