Modulo:Navbox: differenze tra le versioni

Da Semi del Verbo, l'enciclopedia dell'influenza del Vangelo sulla cultura
(Undid revision 948472523 by [[Special:Contributions/w>MusikAnimal|w>MusikAnimal]] ([[User talk:w>MusikAnimal|talk]]))
 
(nowrap gestito dal css del modulo, vedi Speciale:Permalink/111974644#Formattazione)
Riga 1: Riga 1:
--
--[[
-- This module implements {{Navbox}}
* Modulo che implementa i template Navbox e Navbox_subgroup.
--
]]--


local p = {}
require('Modulo:No globals')


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


local args
-- =============================================================================
local border
--                            Funzioni di utilità
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)
-- Restituisce una sequence Lua ordinata contenente gli ID dei listN presenti.
-- Return wikitext with markers replaced for odd/even striping.
-- Se withGroup è true, controlla anche i groupN.
-- Child (subgroup) navboxes are flagged with a category that is removed
--
-- by parent navboxes. The result is that the category shows all pages
-- @param {table} args
-- where a child navbox is not contained in a parent navbox.
-- @param {boolean} withGroup
local orphanCat = '[[Category:Navbox orphans]]'
-- @return {table}
if border == 'subgroup' and args.orphan ~= 'yes' then
local function getIds(args, withGroup)
-- No change; striping occurs in outermost navbox.
local ret, ids = {}, {}
return wikitext .. orphanCat
for key, _ in pairs(args) do
end
if type(key) == 'string' then
local first, second = 'odd', 'even'
local id = key:match('^list(%d+)$') or (withGroup and key:match('^group(%d+)$'))
if args.evenodd then
if id and tonumber(id) <= (withGroup and MAX_LIST_NAVBOX or MAX_LIST_NAVBOX_SUBGROUP) then
if args.evenodd == 'swap' then
ids[tonumber(id)] = true
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
local regex = orphanCat:gsub('([%[%]])', '%%%1')
for key, _ in pairs(ids) do
return (wikitext:gsub(regex, ''):gsub(REGEX_MARKER, changer))  -- () omits gsub count
table.insert(ret, key)
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
if nowrapitems == 'yes' then
table.sort(ret)
local lines = {}
return ret
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


local function renderNavBar(titleCell)
-- Rimuove eventuali spazi/a capo attorno ai {{,}}.
 
--
if args.navbar ~= 'off' and args.navbar ~= 'plain' and not (not args.name and mw.getCurrentFrame():getParent():getTitle():gsub('/sandbox$', '') == 'Template:Navbox') then
-- @param {string} list
titleCell:wikitext(navbar{
-- @return {string}
args.name,
local function trimSep(list)
mini = 1,
local sep = mw.getCurrentFrame():expandTemplate{ title = "," }
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;'
local sepEsc = mw.ustring.gsub(sep, '-', '%-')
})
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.
--
--
--   Title row
-- @param {table} tableNode
--
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
--  Above/Below rows
local function isTemplatePage(name)
--
local title = mw.title.getCurrentTitle().prefixedText
 
name = 'Template:' .. (name or '')
local function getAboveBelowColspan()
return name == title and true or false
local ret = 2
if args.imageleft then ret = ret + 1 end
if args.image then ret = ret + 1 end
return ret
end
end


local function renderAboveRow(tbl)
-- Carica il CSS via TemplateStyles quando opportuno
if not args.above then return end
local function loadCSS(name)
 
local prefix = isTemplatePage(name) and 'mobile-' or ''
tbl:tag('tr')
local styles = 'Modulo:Navbox/' ..  prefix .. 'styles.css'
:tag('td')
return mw.getCurrentFrame():extensionTag{
:addClass('navbox-abovebelow')
name = 'templatestyles',
: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)
-- =============================================================================
if not args.below then return end
--                            Classe Navbox
-- =============================================================================


tbl:tag('tr')
local Navbox = {}
: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.
--
--
--   List rows
-- @param {table} args - gli argomenti passati al modulo
--
-- @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


if index == 1 and args.imageleft then
setmetatable(self, { __index = Navbox })
row
self.args = args
:tag('td')
-- costruzione tabella HTML
:addClass('navbox-image')
self.tableNode = mw.html.create('table')
:addClass(args.imageclass)
if self.args.debug then
:css('width', '1px')              -- Minimize width
disableCSS(self.tableNode)
:css('padding', '0px 2px 0px 0px')
:cssText(args.imageleftstyle)
:attr('rowspan', #listnums)
:tag('div')
:wikitext(processItem(args.imageleft))
end
end
 
self:_setupTableNode()
if args['group' .. listnum] then
-- prima riga: contiene la navbar e il titolo
local groupCell = row:tag('th')
thNode = self.tableNode:tag('tr')
 
:tag('th')
-- id for aria-labelledby attribute, if lone group with no title or above
:attr('colspan', self.args.image and '3' or '2')
if listnum == 1 and not (args.title or args.above or args.group2) then
:cssText(self.args.titlestyle)
groupCell
if self.args.navbar ~= 'plain' then
:attr('id', mw.uri.anchorEncode(args.group1))
self:_addTnavbar(thNode)
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
local listCell = row:tag('td')
self:_addTitle(thNode)
 
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
listCell:css('width', '100%')
self:_addAboveOrBelow(self.args.above, self.args.abovestyle)
end
end
 
-- altre righe
local rowstyle  -- usually nil so cssText(rowstyle) usually adds nothing
self:_addLists()
if index % 2 == 1 then
-- eventuale riga finale per il below
rowstyle = args.oddstyle
if self.args.below then
else
self:_addAboveOrBelow(self.args.below, self.args.belowstyle)
rowstyle = args.evenstyle
end
end


local listText = args['list' .. listnum]
return self
local oddEven = ODD_EVEN_MARKER
end
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))


if index == 1 and args.image then
-- Restituisce la tabella HTML.
row
--
:tag('td')
-- @return {string}
:addClass('navbox-image')
function Navbox:getHTML()
:addClass(args.imageclass)
return tostring(self.tableNode)
: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).
--
--
--   Tracking categories
-- @param {table} node
--
function Navbox:_addTnavbar(node)
 
local tnavbar = mw.getCurrentFrame():expandTemplate {
local function needsHorizontalLists()
title = 'Tnavbar',
if border == 'subgroup' or args.tracking == 'no' then
args = {
return false
[1] = self.args.name,
end
['mini'] = 1
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])
node:tag('div'):addClass('navbox_navbar'):wikitext(tnavbar)
end
end


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


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


local function isIllegible()
-- Aggiunge una colonna per l'immagine.
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


for key, style in pairs(args) do
-- Aggiunge una nuova riga per ogni groupN/listN
if tostring(key):match("style$") then
function Navbox:_addLists()
if styleratio{mw.text.unstripNoWiki(style)} < 4.5 then
local rowIds, altStyle, altBackground
return true
-- 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
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
local function getTrackingCategories()
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
end


local function renderTrackingCategories(builder)
-- =============================================================================
local title = mw.title.getCurrentTitle()
--                           Classe NavboxSubgroup
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


for _, cat in ipairs(getTrackingCategories()) do
local NavboxSubgroup = {}
builder:wikitext('[[Category:' .. cat .. ']]')
end
end


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


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


tbl:css('border-spacing', 0)
return self
if border == 'subgroup' or border == 'none' then
end
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)


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


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


function p._navbox(navboxArgs)
-- Aggiunge una nuova riga per ogni groupN/listN.
args = navboxArgs
function NavboxSubgroup:_addLists()
listnums = {}
local listIds, altStyle
 
-- crea una row per ogni listN
for k, _ in pairs(args) do
listIds = getIds(self.args)
if type(k) == 'string' then
for _, id in ipairs(listIds) do
local listnum = k:match('^list(%d+)$')
local trNode = self.tableNode:tag('tr')
if listnum then table.insert(listnums, tonumber(listnum)) end
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
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
table.sort(listnums)
end


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


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


-- render the appropriate wrapper around the navbox, depending on the border param
-- Funzione per l'utilizzo da un altro modulo.
local res = mw.html.create()
function p._navbox(args)
if border == 'none' then
return loadCSS(args.name) .. Navbox:new(args):getHTML()
local nav = res:tag('div')
end
: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


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


-- Funzione per il template {{Navbox}}.
function p.navbox(frame)
function p.navbox(frame)
if not getArgs then
return p._navbox(getArgs(frame, { parentOnly = true }))
getArgs = require('Module:Arguments').getArgs
end
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


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


return p
return p

Versione delle 14:35, 6 apr 2020

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('Modulo:No globals')

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