Diferencia entre revisiones de «Módulo:Navbox»

De Touhou Wiki
Ir a la navegación Ir a la búsqueda
Sin resumen de edición
Sin resumen de edición
(No se muestran 4 ediciones intermedias del mismo usuario)
Línea 1: Línea 1:
-- lua port of [[Plantilla:Navbox]]
-- lua port of [[Template:Navbox]]
-- by DennouNeko
-- by DennouNeko
 
-- == Helper functions ==
-- == Helper functions ==
--[[ libraries ]]
--[[ libraries ]]
local common = require("Módulo:Common")
local common = require("Module:Common")
local style = require("Módulo:Styles")
local style = require("Module:Styles")


local colors = style.main_colors
local colors = style.main_colors
 
if mw.text == nil then mw.text = require("Módulo:MW.text") end
if mw.text == nil then mw.text = require("Module:MW.text") end
 
--[[ Retrieve a full list of 'list#', 'list#style', 'group#' and 'group#style' arguments
--[[ Retrieve a full list of 'list#', 'list#style', 'group#' and 'group#style' arguments
     as a table of objects with 'index', 'list', 'liststyle', 'group' and 'groupstyle' fields,
     as a table of objects with 'index', 'list', 'liststyle', 'group' and 'groupstyle' fields,
Línea 19: Línea 19:
local function scan_lists(frame)
local function scan_lists(frame)
   local idx = {}
   local idx = {}
   local lista = {}
   local list = {}
   local grupo = {}
   local group = {}
   local liststyle = {}
   local liststyle = {}
   local groupstyle = {}
   local groupstyle = {}
 
   local ret = {}
   local ret = {}
 
   local s,e,t
   local s,e,t
 
   for k,v in frame:argumentPairs() do
   for k,v in frame:argumentPairs() do
     -- accept only parameters without leading zeros
     -- accept only parameters without leading zeros
     s,e,t = string.find(k, '^group([1-9]%d*)style$')
     s,e,t = string.find(k, '^estilogrupo([1-9]%d*)$')
     if s ~= nil then groupstyle[tonumber(t)] = v end
     if s ~= nil then groupstyle[tonumber(t)] = v end
 
     s,e,t = string.find(k, '^list([1-9]%d*)style$')
     s,e,t = string.find(k, '^estilolista([1-9]%d*)$')
     if s ~= nil then liststyle[tonumber(t)] = v end
     if s ~= nil then liststyle[tonumber(t)] = v end
 
     s,e,t = string.find(k, '^grupo([1-9]%d*)$')
     s,e,t = string.find(k, '^grupo([1-9]%d*)$')
     if s ~= nil then grupo[tonumber(t)] = v end
     if s ~= nil then group[tonumber(t)] = v end
 
     s,e,t = string.find(k, '^lista([1-9]%d*)$')
     s,e,t = string.find(k, '^lista([1-9]%d*)$')
     if s ~= nil then
     if s ~= nil then
       lista[tonumber(t)] = v
       list[tonumber(t)] = v
       idx[#idx+1] = tonumber(t)
       idx[#idx+1] = tonumber(t)
     end
     end
   end
   end
 
   table.sort(idx)
   table.sort(idx)
 
   for k,v in pairs(idx) do
   for k,v in pairs(idx) do
     local tmp = {}
     local tmp = {}
     tmp['index'] = v
     tmp['index'] = v
     tmp['lista'] = lista[v]
     tmp['lista'] = list[v]
     if common.isset(grupo[v]) then tmp['grupo'] = grupo[v] end
     if common.isset(group[v]) then tmp['grupo'] = group[v] end
     if common.isset(liststyle[v]) then tmp['liststyle'] = liststyle[v] end
     if common.isset(liststyle[v]) then tmp['estilo lista'] = liststyle[v] end
     if common.isset(groupstyle[v]) then tmp['groupstyle'] = groupstyle[v] end
     if common.isset(groupstyle[v]) then tmp['estilo grupo'] = groupstyle[v] end
     ret[#ret+1] = tmp
     ret[#ret+1] = tmp
   end
   end
 
   return ret
   return ret
end
end
 
 
-- == Functions generating the document elements ==
-- == Functions generating the document elements ==
 
--[[ Generates the beginning of box, based on specified 'ttype' and 'border'. ]]
--[[ Generates the beginning of box, based on specified 'ttype' and 'border'. ]]
local function start_box(frame, border, ttype)
local function start_box(frame, border, ttype)
Línea 75: Línea 75:
     -- new, independent navbox - create a frame around it
     -- new, independent navbox - create a frame around it
     ret[#ret+1] = '<table class="navbox'
     ret[#ret+1] = '<table class="navbox'
     if common.isset(frame.args['bodyclass']) then ret[#ret+1] = " " .. frame.args['bodyclass'] end
     if common.isset(frame.args['clase cuerpo']) then ret[#ret+1] = " " .. frame.args['clase cuerpo'] end
     ret[#ret+1] = '" cellspacing="0" style="'
     ret[#ret+1] = '" cellspacing="0" style="'
     if common.isset(colors[ttype]) then ret[#ret+1] = 'background:' .. colors[ttype]['background'] .. ';' end
     if common.isset(colors[ttype]) then ret[#ret+1] = 'background:' .. colors[ttype]['background'] .. ';' end
     -- ret[#ret+1] = 'border:1px solid #aaa; padding:1px;width:100%;vertical-align:middle;margin:auto;clear:both;font-size:88%;text-align:center;'
     -- ret[#ret+1] = 'border:1px solid #aaa; padding:1px;width:100%;vertical-align:middle;margin:auto;clear:both;font-size:88%;text-align:center;'
     style.add(ret, frame.args['bodystyle'])
     style.add(ret, frame.args['estilo cuerpo'])
     style.add(ret, frame.args['style'])
     style.add(ret, frame.args['estilo'])
     ret[#ret+1] = '"><tr><td style="padding: 2px;">'
     ret[#ret+1] = '"><tr><td style="padding: 2px;">'
   end
   end
 
   -- The actual navbox table starts here
   -- The actual navbox table starts here
   ret[#ret+1] = '<table cellspacing="0" class="nowraplinks'
   ret[#ret+1] = '<table cellspacing="0" class="nowraplinks'
   if common.isset(frame.args['título']) and frame.args['state'] ~= "plain" and frame.args['state'] ~= "off" then
   if common.isset(frame.args['título']) and frame.args['estado'] ~= "plain" and frame.args['estado'] ~= "off" then
     --TODO: If there is support for collapsible and autocollapse, swap comments here
     --TODO: If there is support for collapsible and autocollapse, swap comments here
     --ret[#ret+1] = ' collapsible '
     --ret[#ret+1] = ' collapsible '
     ret[#ret+1] = ' mw-collapsible '
     ret[#ret+1] = ' mw-collapsible '
     if common.isset(frame.args['state']) then
     if common.isset(frame.args['estado']) then
       ret[#ret+1] = frame.args['state']
       ret[#ret+1] = frame.args['estado']
     else
     else
       --ret[#ret+1] = 'autocollapse'
       --ret[#ret+1] = 'autocollapse'
Línea 102: Línea 102:
     -- ret[#ret+1] = 'margin:auto;clear:both;font-size:88%;text-align:center;width:100%;vertical-align:middle;'
     -- ret[#ret+1] = 'margin:auto;clear:both;font-size:88%;text-align:center;width:100%;vertical-align:middle;'
     ret[#ret+1] = 'width: 100%;'
     ret[#ret+1] = 'width: 100%;'
     style.add(ret, frame.args['bodystyle'])
     style.add(ret, frame.args['estilo cuerpo'])
     style.add(ret, frame.args['style'])
     style.add(ret, frame.args['estilo'])
     ret[#ret+1] = '">'
     ret[#ret+1] = '">'
   else
   else
     ret[#ret+1] = '" style="width:100%;background:transparent;color:inherit;">'
     ret[#ret+1] = '" style="width:100%;background:transparent;color:inherit;">'
   end
   end
 
   return table.concat(ret)
   return table.concat(ret)
end
end
 
--[[ Finalizes the box, based on specified 'ttype' and 'border' ]]
--[[ Finalizes the box, based on specified 'ttype' and 'border' ]]
local function end_box(frame, border, ttype)
local function end_box(frame, border, ttype)
Línea 127: Línea 127:
   return table.concat(ret)
   return table.concat(ret)
end
end
 
--[[ Generates the title row: 'v(iew) • d(iscuss) • e(dit)' + title + '[Expand]'/'[Collapse]' buttons. ]]
--[[ Generates the title row: 'v(iew) • d(iscuss) • e(dit)' + title + '[Expand]'/'[Collapse]' buttons. ]]
local function build_title(frame, border, ttype)
local function build_title(frame, border, ttype)
   local ret = {""}
   local ret = {""}
   if not common.isset(frame.args['título']) then return '' end
   if not common.isset(frame.args['título']) then return '' end
 
   table.insert(ret, '<tr>')
   table.insert(ret, '<tr>')
   if common.isset(frame.args['titlegroup']) then
   if common.isset(frame.args['título grupo']) then
     -- in case that there's a group for title
     -- in case that there's a group for title
     ret[#ret+1] = '<td class="navbox-group" style="'
     ret[#ret+1] = '<td class="navbox-group" style="'
     if common.isset(colors[ttype]) then ret[#ret+1] = 'background:' .. colors[ttype]['grupo'] .. ';' end
     if common.isset(colors[ttype]) then ret[#ret+1] = 'background:' .. colors[ttype]['grupo'] .. ';' end
     ret[#ret+1] = 'padding-left:1em;padding-right:1em;white-space:nowrap;text-align:right;'
     ret[#ret+1] = 'padding-left:1em;padding-right:1em;white-space:nowrap;text-align:right;'
     style.add(ret, frame.args['basestyle'])
     style.add(ret, frame.args['estilo base'])
     style.add(ret, frame.args['groupstyle'])
     style.add(ret, frame.args['estilo grupo'])
     style.add(ret, frame.args['titlegroupstyle'])
     style.add(ret, frame.args['estilo título grupo'])
     ret[#ret+1] = '">\n'
     ret[#ret+1] = '">\n'
     ret[#ret+1] = frame.args['titlegroup']
     ret[#ret+1] = frame.args['título grupo']
     ret[#ret+1] = '</td>'
     ret[#ret+1] = '</td>'
 
     ret[#ret+1] = '<th style="border-left:2px solid '
     ret[#ret+1] = '<th style="border-left:2px solid '
     ret[#ret+1] = common.cv(common.isset(colors[ttype]), colors[ttype]['background'], '#fdfdfd')
     ret[#ret+1] = common.cv(common.isset(colors[ttype]), colors[ttype]['background'], '#fdfdfd')
Línea 155: Línea 155:
   if common.isset(colors[ttype]) then ret[#ret+1] = 'background:' .. colors[ttype]['título'] .. ';' end
   if common.isset(colors[ttype]) then ret[#ret+1] = 'background:' .. colors[ttype]['título'] .. ';' end
   ret[#ret+1] = 'text-align:center;'
   ret[#ret+1] = 'text-align:center;'
   style.add(ret, frame.args['basestyle'])
   style.add(ret, frame.args['estilo base'])
   style.add(ret, frame.args['titlestyle'])
   style.add(ret, frame.args['estilo título'])
   ret[#ret+1] = '"'
   ret[#ret+1] = '"'
 
   -- calculate the colspan for title
   -- calculate the colspan for title
   local cs = 2 -- 2 because each list element with group occupies 2 columns
   local cs = 2 -- 2 because each list element with group occupies 2 columns
   if common.isset(frame.args['imageleft']) then cs = cs + 1 end
   if common.isset(frame.args['imagen izquierda']) then cs = cs + 1 end
   if common.isset(frame.args['imagen']) then cs = cs + 1 end
   if common.isset(frame.args['imagen']) then cs = cs + 1 end
   if common.isset(frame.args['titlegroup']) then cs = cs - 1 end -- if there was a titlegroup, it occupies one cell already
   if common.isset(frame.args['título grupo']) then cs = cs - 1 end -- if there was a titlegroup, it occupies one cell already
   ret[#ret+1] = ' colspan = "' .. cs .. '"'
   ret[#ret+1] = ' colspan = "' .. cs .. '"'
   ret[#ret+1] = ' class="navbox-title">'
   ret[#ret+1] = ' class="navbox-title">'
 
   -- add navbars and/or padding when needed
   -- add navbars and/or padding when needed
   if frame.args['navbar'] == "plain" or frame.args['navbar'] == "off" or border == "subgroup" or border == "child" or border == "none" then
   if frame.args['navbar'] == "plain" or frame.args['navbar'] == "off" or border == "subgroup" or border == "child" or border == "none" then
     if frame.args['navbar'] == "off" then
     if frame.args['navbar'] == "off" then
       if frame.args['state'] == "plain" then ret[#ret+1] = '<div style="float:right;width:6em;">&nbsp;</div>' end
       if frame.args['estado'] == "plain" then ret[#ret+1] = '<div style="float:right;width:6em;">&nbsp;</div>' end
     else
     else
       if frame.args['state'] ~= "plain" then ret[#ret+1] = '<div style="float:left; width:6em;text-align:left;">&nbsp;</div>' end
       if frame.args['estado'] ~= "plain" then ret[#ret+1] = '<div style="float:left; width:6em;text-align:left;">&nbsp;</div>' end
     end
     end
   else
   else
     if frame.args['state'] == "plain" then ret[#ret+1] = '<div style="float:right;width:6em;">&nbsp;</div>' end
     if frame.args['estado'] == "plain" then ret[#ret+1] = '<div style="float:right;width:6em;">&nbsp;</div>' end
 
     ret[#ret+1] = '<div style="float:left; width:6em;text-align:left;">'
     ret[#ret+1] = '<div style="float:left; width:6em;text-align:left;">'
     if frame.args['nombre'] ~= nil then
     if frame.args['nombre'] ~= nil then
Línea 182: Línea 182:
       args[#args+1] = frame.args['nombre']
       args[#args+1] = frame.args['nombre']
       args['mini'] = '1'
       args['mini'] = '1'
 
       -- TODO: Build the navbar without using external template?
       -- TODO: Build the navbar without using external template?
       local q = {""}
       local q = {""}
       q[#q+1] = 'text-align:left;'
       q[#q+1] = 'text-align:left;'
       style.add(q, frame.args['basestyle'])
       style.add(q, frame.args['estilo base'])
       style.add(q, frame.args['titlestyle'])
       style.add(q, frame.args['estilo título'])
       q[#q+1] = 'border:none;'
       q[#q+1] = 'border:none;'
       args['fontstyle'] = table.concat(q)
       args['fontstyle'] = table.concat(q)
 
       ret[#ret+1] = frame:expandTemplate{title = 'Navbar', args = args}
       ret[#ret+1] = frame:expandTemplate{title = 'Navbar', args = args}
     else
     else
       ret[#ret+1] = '&nbsp;'
       ret[#ret+1] = '&nbsp;'
       ret[#ret+1] = '[[Categoría:Navboxes sin nombre]]'
       ret[#ret+1] = '[[Category:Navboxes without name]]'
     end
     end
     ret[#ret+1] = '</div>'
     ret[#ret+1] = '</div>'
   end
   end
 
   -- the title starts here
   -- the title starts here
   ret[#ret+1] = '<span'
   ret[#ret+1] = '<span'
   if common.isset(frame.args['titleclass']) then ret[#ret+1] = ' class="' .. frame.args['titleclass'] .. '"' end
   if common.isset(frame.args['clase título']) then ret[#ret+1] = ' class="' .. frame.args['clase título'] .. '"' end
   ret[#ret+1] = ' style="font-size:' .. common.cv((border == "subgroup" or border == "child" or border == "none"), '100%', '110%') .. ';">\n'
   ret[#ret+1] = ' style="font-size:' .. common.cv((border == "subgroup" or border == "child" or border == "none"), '100%', '110%') .. ';">\n'
 
   ret[#ret+1] = frame.args['título']
   ret[#ret+1] = frame.args['título']
 
   ret[#ret+1] = '</span>'
   ret[#ret+1] = '</span>'
   ret[#ret+1] = '</th></tr>'
   ret[#ret+1] = '</th></tr>'
   return table.concat(ret)
   return table.concat(ret)
end
end
 
--[[ Generates the 'above' and 'below' rows.
--[[ Generates the 'above' and 'below' rows.
     Returns the generated row and updated 'sep' value (if separation is going to be needed). ]]
     Returns the generated row and updated 'sep' value (if separation is going to be needed). ]]
Línea 219: Línea 219:
       table.insert(ret, '<tr style="height: 2px;"><td></td></tr>')
       table.insert(ret, '<tr style="height: 2px;"><td></td></tr>')
     end
     end
 
     -- start the 'above' or 'below' row
     -- start the 'above' or 'below' row
     ret[#ret+1] = '<tr>'
     ret[#ret+1] = '<tr>'
Línea 225: Línea 225:
     if common.isset(colors[ttype]) then
     if common.isset(colors[ttype]) then
       -- both above and below share same default color from 'colors' table
       -- both above and below share same default color from 'colors' table
       ret[#ret+1] = 'background:' .. common.cv((border ~= "subgroup" and border ~= "child"), colors[ttype]['above'], colors[ttype]['grupo']) .. ';'
       ret[#ret+1] = 'background:' .. common.cv((border ~= "subgroup" and border ~= "child"), colors[ttype]['arriba'], colors[ttype]['grupo']) .. ';'
     end
     end
     ret[#ret+1] = 'padding-left:1em;padding-right:1em;text-align:center;'
     ret[#ret+1] = 'padding-left:1em;padding-right:1em;text-align:center;'
     style.add(ret, frame.args['basestyle'])
     style.add(ret, frame.args['estilo base'])
     style.add(ret, rstyle)
     style.add(ret, rstyle)
     ret[#ret+1] = '"'
     ret[#ret+1] = '"'
 
     -- calculate and add the colspan (always full table width)
     -- calculate and add the colspan (always full table width)
     local cs = 2
     local cs = 2
     if common.isset(frame.args['imageleft']) then cs = cs + 1 end
     if common.isset(frame.args['imagen izquierda']) then cs = cs + 1 end
     if common.isset(frame.args['imagen']) then cs = cs + 1 end
     if common.isset(frame.args['imagen']) then cs = cs + 1 end
     ret[#ret+1] = ' colspan="' .. cs .. '">\n'
     ret[#ret+1] = ' colspan="' .. cs .. '">\n'
 
     -- row content
     -- row content
     ret[#ret+1] = text
     ret[#ret+1] = text
 
     ret[#ret+1] = '</td></tr>'
     ret[#ret+1] = '</td></tr>'
     sep = true -- now we're going to need separation
     sep = true -- now we're going to need separation
Línea 246: Línea 246:
   return table.concat(ret),sep
   return table.concat(ret),sep
end
end
 
--[[ Generates a single row of the list. ]]
--[[ Generates a single row of the list. ]]
local function build_row(frame, border, ttype, k, group, list, groupstyle, liststyle)
local function build_row(frame, border, ttype, k, group, list, groupstyle, liststyle)
Línea 257: Línea 257:
     end
     end
     ret[#ret+1] = 'padding-left:1em; padding-right:1em; white-space:nowrap; text-align:right;'
     ret[#ret+1] = 'padding-left:1em; padding-right:1em; white-space:nowrap; text-align:right;'
     style.add(ret, frame.args['basestyle'])
     style.add(ret, frame.args['estilo base'])
     if common.isset(frame.args['groupwidth']) then ret[#ret+1] = 'width:' .. frame.args['groupwidth'] .. ';' end
     if common.isset(frame.args['ancho grupo']) then ret[#ret+1] = 'width:' .. frame.args['ancho grupo'] .. ';' end
     style.add(ret, frame.args['groupstyle'])
     style.add(ret, frame.args['estilo grupo'])
     style.add(ret, groupstyle)
     style.add(ret, groupstyle)
     ret[#ret+1] = '">'
     ret[#ret+1] = '">'
Línea 265: Línea 265:
     ret[#ret+1] = group
     ret[#ret+1] = group
     ret[#ret+1] = '</div></td>'
     ret[#ret+1] = '</div></td>'
 
     -- start the content cell
     -- start the content cell
     ret[#ret+1] = '<td style="text-align:left;border-left-width:2px;border-left-style:solid;'
     ret[#ret+1] = '<td style="text-align:left;border-left-width:2px;border-left-style:solid;'
Línea 273: Línea 273:
   end
   end
   -- a bit tricky, but this works as XOR
   -- a bit tricky, but this works as XOR
   if (frame.args['evenodd'] == "swap") ~= ((k % 2) == 0) then
   if (frame.args['par impar'] == "swap") ~= ((k % 2) == 0) then
     if common.isset(colors[ttype]) then ret[#ret+1] = 'background:' .. colors[ttype]['dark'] .. ';' end
     if common.isset(colors[ttype]) then ret[#ret+1] = 'background:' .. colors[ttype]['dark'] .. ';' end
   else
   else
     ret[#ret+1] = 'background:transparent;'
     ret[#ret+1] = 'background:transparent;'
   end
   end
   if not common.isset(frame.args['groupwidth']) then ret[#ret+1] = 'width:100%;' end
   if not common.isset(frame.args['ancho grupo']) then ret[#ret+1] = 'width:100%;' end
   ret[#ret+1] = 'padding: 0;'
   ret[#ret+1] = 'padding: 0;'
   style.add(ret, frame.args['liststyle'])
   style.add(ret, frame.args['estilo lista'])
   style.add(ret, common.cv((frame.args['evenodd'] == "swap") ~= ((k % 2) == 0), frame.args['evenstyle'], frame.args['oddstyle']))
   style.add(ret, common.cv((frame.args['par impar'] == "swap") ~= ((k % 2) == 0), frame.args['estilo par'], frame.args['estilo impar']))
   style.add(ret, liststyle)
   style.add(ret, liststyle)
   ret[#ret+1] = '" class="navbox-list ' .. common.cv((frame.args['evenodd'] == "swap") ~= ((k % 2) == 0), 'navbox-even', 'navbox-odd') .. '">'
   ret[#ret+1] = '" class="navbox-list ' .. common.cv((frame.args['par impar'] == "swap") ~= ((k % 2) == 0), 'navbox-even', 'navbox-odd') .. '">'
 
   -- add the <div> with content (same <div> that child navbox has to close when starting table)
   -- add the <div> with content (same <div> that child navbox has to close when starting table)
   ret[#ret+1] = '<div style="padding:'
   ret[#ret+1] = '<div style="padding:'
   if common.isset(frame.args['lista' .. k .. 'padding']) then
   if common.isset(frame.args['list' .. k .. 'padding']) then
     ret[#ret+1] = frame.args['lista' .. k .. 'padding']
     ret[#ret+1] = frame.args['list' .. k .. 'padding']
   elseif common.isset(frame.args['listpadding']) then
   elseif common.isset(frame.args['listpadding']) then
     ret[#ret+1] = frame.args['listpadding']
     ret[#ret+1] = frame.args['listpadding']
Línea 299: Línea 299:
   return table.concat(ret)
   return table.concat(ret)
end
end
 
--[[ Generates the "body" of table (everything below title) ]]
--[[ Generates the "body" of table (everything below title) ]]
local function build_body(frame, border, ttype, lists)
local function build_body(frame, border, ttype, lists)
Línea 306: Línea 306:
   local imgs = true -- add images if present
   local imgs = true -- add images if present
   local irows = common.cv( (#lists > 0), (2 * #lists - 1), 1 )
   local irows = common.cv( (#lists > 0), (2 * #lists - 1), 1 )
 
   -- aff the 'above' row
   -- aff the 'above' row
   ret[#ret+1],sep = build_above_below(frame, border, ttype, frame.args['above'], frame.args['abovestyle'], sep)
   ret[#ret+1],sep = build_above_below(frame, border, ttype, frame.args['arriba'], frame.args['estilo arriba'], sep)
 
   for k,v in pairs(lists) do
   for k,v in pairs(lists) do
     if sep then -- add separation if needed
     if sep then -- add separation if needed
       ret[#ret+1] = '<tr style="height: 2px;"><td></td></tr>'
       ret[#ret+1] = '<tr style="height: 2px;"><td></td></tr>'
     end
     end
 
     table.insert(ret, '<tr>')
     table.insert(ret, '<tr>')
 
     -- add left image (if wasn't added yet and is present)
     -- add left image (if wasn't added yet and is present)
     if imgs then
     if imgs then
       if common.isset(frame.args['imageleft']) then
       if common.isset(frame.args['imagen izquierda']) then
         ret[#ret+1] = '<td style="width:0%;padding:0px 2px 0px 0px;'
         ret[#ret+1] = '<td style="width:0%;padding:0px 2px 0px 0px;'
         style.add(ret, frame.args['imageleftstyle'])
         style.add(ret, frame.args['estilo imagen izquierda'])
         ret[#ret+1] = '" rowspan="' .. irows .. '">' .. frame.args['imageleft'] .. '</td>'
         ret[#ret+1] = '" rowspan="' .. irows .. '">' .. frame.args['imagen izquierda'] .. '</td>'
       end
       end
     end
     end
 
     -- add the list content
     -- add the list content
     ret[#ret+1] = build_row(frame, border, ttype, v['index'], v['grupo'], v['lista'], v['groupstyle'], v['liststyle'])
     ret[#ret+1] = build_row(frame, border, ttype, v['index'], v['grupo'], v['lista'], v['estilo grupo'], v['estilo lista'])
 
     -- add right image (if wasn't added yet and is present)
     -- add right image (if wasn't added yet and is present)
     if imgs then
     if imgs then
       if common.isset(frame.args['imagen']) then
       if common.isset(frame.args['imagen']) then
         ret[#ret+1] = '<td style="width:0%;padding:0px 0px 0px 2px;'
         ret[#ret+1] = '<td style="width:0%;padding:0px 0px 0px 2px;'
         style.add(ret, frame.args['imagestyle'])
         style.add(ret, frame.args['estilo imagen'])
         ret[#ret+1] = '" rowspan="' .. irows .. '">' .. frame.args['imagen'] .. '</td>'
         ret[#ret+1] = '" rowspan="' .. irows .. '">' .. frame.args['imagen'] .. '</td>'
       end
       end
 
       -- mark that images were added already
       -- mark that images were added already
       imgs = false
       imgs = false
     end
     end
 
     ret[#ret+1] = '</tr>'
     ret[#ret+1] = '</tr>'
     sep = true -- now the separation is going to be needed for sure
     sep = true -- now the separation is going to be needed for sure
   end
   end
 
   ret[#ret+1],sep = build_above_below(frame, border, ttype, frame.args['below'], frame.args['belowstyle'], sep)
   ret[#ret+1],sep = build_above_below(frame, border, ttype, frame.args['abajo'], frame.args['estilo abajo'], sep)
 
   return table.concat(ret)
   return table.concat(ret)
end
end
 
-- == Exported functions ==
-- == Exported functions ==
--[[ Generates and returns the whole navbox table. ]]
--[[ Generates and returns the whole navbox table. ]]
Línea 356: Línea 356:
   local border = ""
   local border = ""
   local ttype = ""
   local ttype = ""
 
   if common.isset(frame.args['border']) then
   if common.isset(frame.args['borde']) then
     border = frame.args['border']
     border = frame.args['borde']
   elseif common.isset(frame.args[1]) then
   elseif common.isset(frame.args[1]) then
     border = mw.text.trim(frame.args[1])
     border = mw.text.trim(frame.args[1])
   end
   end
 
   if common.isset(frame.args['tipo']) then
   if common.isset(frame.args['type']) then
     ttype = frame.args['tipo']
     ttype = frame.args['type']
   end
   end
 
   -- TODO: filtering of border and type values?
   -- TODO: filtering of border and type values?
 
   -- prepare the data first
   -- prepare the data first
   local lists = scan_lists(frame)
   local lists = scan_lists(frame)
 
   -- build the content   
   -- build the content   
   template[#template+1] = start_box(frame, border, ttype)
   template[#template+1] = start_box(frame, border, ttype)
Línea 377: Línea 377:
   template[#template+1] = build_body(frame, border, ttype, lists)
   template[#template+1] = build_body(frame, border, ttype, lists)
   template[#template+1] = end_box(frame, border, ttype)
   template[#template+1] = end_box(frame, border, ttype)
 
   return table.concat(template)
   return table.concat(template)
end
end
 
-- [[ Workaround for the "Navbox subgroup" template ]]
-- [[ Workaround for the "Navbox subgroup" template ]]
local function buildNavboxSubgroup(frame)
local function buildNavboxSubgroup(frame)
   if not common.isset(frame.args['border']) then frame.args['border'] = 'child' end
   if not common.isset(frame.args['borde']) then frame.args['borde'] = 'child' end
 
   -- TODO: add/modify/remove other params?
   -- TODO: add/modify/remove other params?
 
   -- call the main function
   -- call the main function
   return buildNavbox(frame)
   return buildNavbox(frame)
end
end
 
--[[ Mostly for debugging, generates and returns a table of sets of colors
--[[ Mostly for debugging, generates and returns a table of sets of colors
     and the values in cells with colors used as backgrounds. ]]
     and the values in cells with colors used as backgrounds. ]]
Línea 402: Línea 402:
     table.insert(ret, '<td>' .. k .. '</td>')
     table.insert(ret, '<td>' .. k .. '</td>')
     table.insert(ret, '<td style="background:' .. v['título'] .. '">' .. v['título'] .. '</td>')
     table.insert(ret, '<td style="background:' .. v['título'] .. '">' .. v['título'] .. '</td>')
     table.insert(ret, '<td style="background:' .. v['above'] .. '">' .. v['above'] .. '</td>')
     table.insert(ret, '<td style="background:' .. v['arriba'] .. '">' .. v['arriba'] .. '</td>')
     table.insert(ret, '<td style="background:' .. v['grupo'] .. '">' .. v['grupo'] .. '</td>')
     table.insert(ret, '<td style="background:' .. v['grupo'] .. '">' .. v['grupo'] .. '</td>')
     table.insert(ret, '<td style="background:' .. v['subgroup'] .. '">' .. v['subgroup'] .. '</td>')
     table.insert(ret, '<td style="background:' .. v['subgroup'] .. '">' .. v['subgroup'] .. '</td>')
Línea 412: Línea 412:
   return table.concat(ret)
   return table.concat(ret)
end
end
 
-- export local functions
-- export local functions
return {
return {
Línea 421: Línea 421:
   ['buildColorTable'] = buildColorTable
   ['buildColorTable'] = buildColorTable
}
}
 
-- [[Categoría:Lua Scripts|{{PAGENAME}}]]
-- [[Category:Lua Scripts|{{PAGENAME}}]]

Revisión del 22:21 22 jun 2018

La documentación para este módulo puede ser creada en Módulo:Navbox/doc

-- lua port of [[Template:Navbox]]
-- by DennouNeko

-- == Helper functions ==
--[[ libraries ]]
local common = require("Module:Common")
local style = require("Module:Styles")

local colors = style.main_colors

if mw.text == nil then mw.text = require("Module:MW.text") end

--[[ Retrieve a full list of 'list#', 'list#style', 'group#' and 'group#style' arguments
     as a table of objects with 'index', 'list', 'liststyle', 'group' and 'groupstyle' fields,
     where 'index' is the number in 'list#' argument name.
     Only 'list' and 'index' are always defined. Elements that weren't specified will be empty.
     If 'list#' wasn't defined, rest of elements for that given index will be ignored.
     Key of argument matches the number in arguments name. ]]
local function scan_lists(frame)
  local idx = {}
  local list = {}
  local group = {}
  local liststyle = {}
  local groupstyle = {}

  local ret = {}

  local s,e,t

  for k,v in frame:argumentPairs() do
    -- accept only parameters without leading zeros
    s,e,t = string.find(k, '^estilogrupo([1-9]%d*)$')
    if s ~= nil then groupstyle[tonumber(t)] = v end

    s,e,t = string.find(k, '^estilolista([1-9]%d*)$')
    if s ~= nil then liststyle[tonumber(t)] = v end

    s,e,t = string.find(k, '^grupo([1-9]%d*)$')
    if s ~= nil then group[tonumber(t)] = v end

    s,e,t = string.find(k, '^lista([1-9]%d*)$')
    if s ~= nil then
      list[tonumber(t)] = v
      idx[#idx+1] = tonumber(t)
    end
  end

  table.sort(idx)

  for k,v in pairs(idx) do
    local tmp = {}
    tmp['index'] = v
    tmp['lista'] = list[v]
    if common.isset(group[v]) then tmp['grupo'] = group[v] end
    if common.isset(liststyle[v]) then tmp['estilo lista'] = liststyle[v] end
    if common.isset(groupstyle[v]) then tmp['estilo grupo'] = groupstyle[v] end
    ret[#ret+1] = tmp
  end

  return ret
end


-- == Functions generating the document elements ==

--[[ Generates the beginning of box, based on specified 'ttype' and 'border'. ]]
local function start_box(frame, border, ttype)
  local ret = {}
  if border == "subgroup" or border == "child" then
    -- since we're in a cell of parent navbox, we have to close its <div> element
    ret[#ret+1] = '</div>'
  elseif border == "none" then
    -- nothing to do
  else
    -- new, independent navbox - create a frame around it
    ret[#ret+1] = '<table class="navbox'
    if common.isset(frame.args['clase cuerpo']) then ret[#ret+1] = " " .. frame.args['clase cuerpo'] end
    ret[#ret+1] = '" cellspacing="0" style="'
    if common.isset(colors[ttype]) then ret[#ret+1] = 'background:' .. colors[ttype]['background'] .. ';' end
    -- ret[#ret+1] = 'border:1px solid #aaa; padding:1px;width:100%;vertical-align:middle;margin:auto;clear:both;font-size:88%;text-align:center;'
    style.add(ret, frame.args['estilo cuerpo'])
    style.add(ret, frame.args['estilo'])
    ret[#ret+1] = '"><tr><td style="padding: 2px;">'
  end

  -- The actual navbox table starts here
  ret[#ret+1] = '<table cellspacing="0" class="nowraplinks'
  if common.isset(frame.args['título']) and frame.args['estado'] ~= "plain" and frame.args['estado'] ~= "off" then
    --TODO: If there is support for collapsible and autocollapse, swap comments here
    --ret[#ret+1] = ' collapsible '
    ret[#ret+1] = ' mw-collapsible '
    if common.isset(frame.args['estado']) then
      ret[#ret+1] = frame.args['estado']
    else
      --ret[#ret+1] = 'autocollapse'
      ret[#ret+1] = 'mw-collapsed'
    end
  end
  if border == "subgroup" or border == "child" or border == "none" then
    ret[#ret+1] = ' navbox-subgroup" style="'
    if common.isset(colors[ttype]) then table.insert(ret, 'background:' .. colors[ttype]['background'] .. ';') end
    -- ret[#ret+1] = 'margin:auto;clear:both;font-size:88%;text-align:center;width:100%;vertical-align:middle;'
    ret[#ret+1] = 'width: 100%;'
    style.add(ret, frame.args['estilo cuerpo'])
    style.add(ret, frame.args['estilo'])
    ret[#ret+1] = '">'
  else
    ret[#ret+1] = '" style="width:100%;background:transparent;color:inherit;">'
  end

  return table.concat(ret)
end

--[[ Finalizes the box, based on specified 'ttype' and 'border' ]]
local function end_box(frame, border, ttype)
  local ret = {""}
  ret[#ret+1] = '</table>'
  if border == "subgroup" or border == "child" then
    -- open a <div>, since we closed one when starting this child navbox
    ret[#ret+1] = '<div>'
  elseif border == "none" then
    -- nothing to do
  else
    -- close the table we started as a frame
    ret[#ret+1] = '</td></tr></table>'
  end
  return table.concat(ret)
end

--[[ Generates the title row: 'v(iew) • d(iscuss) • e(dit)' + title + '[Expand]'/'[Collapse]' buttons. ]]
local function build_title(frame, border, ttype)
  local ret = {""}
  if not common.isset(frame.args['título']) then return '' end

  table.insert(ret, '<tr>')
  if common.isset(frame.args['título grupo']) then
    -- in case that there's a group for title
    ret[#ret+1] = '<td class="navbox-group" style="'
    if common.isset(colors[ttype]) then ret[#ret+1] = 'background:' .. colors[ttype]['grupo'] .. ';' end
    ret[#ret+1] = 'padding-left:1em;padding-right:1em;white-space:nowrap;text-align:right;'
    style.add(ret, frame.args['estilo base'])
    style.add(ret, frame.args['estilo grupo'])
    style.add(ret, frame.args['estilo título grupo'])
    ret[#ret+1] = '">\n'
    ret[#ret+1] = frame.args['título grupo']
    ret[#ret+1] = '</td>'

    ret[#ret+1] = '<th style="border-left:2px solid '
    ret[#ret+1] = common.cv(common.isset(colors[ttype]), colors[ttype]['background'], '#fdfdfd')
    ret[#ret+1] = ';width:100%;'
  else
    -- no group for title
    table.insert(ret, '<th style="')
  end
  if common.isset(colors[ttype]) then ret[#ret+1] = 'background:' .. colors[ttype]['título'] .. ';' end
  ret[#ret+1] = 'text-align:center;'
  style.add(ret, frame.args['estilo base'])
  style.add(ret, frame.args['estilo título'])
  ret[#ret+1] = '"'

  -- calculate the colspan for title
  local cs = 2 -- 2 because each list element with group occupies 2 columns
  if common.isset(frame.args['imagen izquierda']) then cs = cs + 1 end
  if common.isset(frame.args['imagen']) then cs = cs + 1 end
  if common.isset(frame.args['título grupo']) then cs = cs - 1 end -- if there was a titlegroup, it occupies one cell already
  ret[#ret+1] = ' colspan = "' .. cs .. '"'
  ret[#ret+1] = ' class="navbox-title">'

  -- add navbars and/or padding when needed
  if frame.args['navbar'] == "plain" or frame.args['navbar'] == "off" or border == "subgroup" or border == "child" or border == "none" then
    if frame.args['navbar'] == "off" then
      if frame.args['estado'] == "plain" then ret[#ret+1] = '<div style="float:right;width:6em;">&nbsp;</div>' end
    else
      if frame.args['estado'] ~= "plain" then ret[#ret+1] = '<div style="float:left; width:6em;text-align:left;">&nbsp;</div>' end
    end
  else
    if frame.args['estado'] == "plain" then ret[#ret+1] = '<div style="float:right;width:6em;">&nbsp;</div>' end

    ret[#ret+1] = '<div style="float:left; width:6em;text-align:left;">'
    if frame.args['nombre'] ~= nil then
      local args = {}
      args[#args+1] = frame.args['nombre']
      args['mini'] = '1'

      -- TODO: Build the navbar without using external template?
      local q = {""}
      q[#q+1] = 'text-align:left;'
      style.add(q, frame.args['estilo base'])
      style.add(q, frame.args['estilo título'])
      q[#q+1] = 'border:none;'
      args['fontstyle'] = table.concat(q)

      ret[#ret+1] = frame:expandTemplate{title = 'Navbar', args = args}
    else
      ret[#ret+1] = '&nbsp;'
      ret[#ret+1] = '[[Category:Navboxes without name]]'
    end
    ret[#ret+1] = '</div>'
  end

  -- the title starts here
  ret[#ret+1] = '<span'
  if common.isset(frame.args['clase título']) then ret[#ret+1] = ' class="' .. frame.args['clase título'] .. '"' end
  ret[#ret+1] = ' style="font-size:' .. common.cv((border == "subgroup" or border == "child" or border == "none"), '100%', '110%') .. ';">\n'

  ret[#ret+1] = frame.args['título']
  
  ret[#ret+1] = '</span>'
  ret[#ret+1] = '</th></tr>'
  return table.concat(ret)
end

--[[ Generates the 'above' and 'below' rows.
     Returns the generated row and updated 'sep' value (if separation is going to be needed). ]]
local function build_above_below(frame, border, ttype, text, rstyle, sep)
  local ret = {''}
  if common.isset(text) then
    if sep then -- if separation is needed
      table.insert(ret, '<tr style="height: 2px;"><td></td></tr>')
    end

    -- start the 'above' or 'below' row
    ret[#ret+1] = '<tr>'
    ret[#ret+1] = '<td class="navbox-abovebelow" style="'
    if common.isset(colors[ttype]) then
      -- both above and below share same default color from 'colors' table
      ret[#ret+1] = 'background:' .. common.cv((border ~= "subgroup" and border ~= "child"), colors[ttype]['arriba'], colors[ttype]['grupo']) .. ';'
    end
    ret[#ret+1] = 'padding-left:1em;padding-right:1em;text-align:center;'
    style.add(ret, frame.args['estilo base'])
    style.add(ret, rstyle)
    ret[#ret+1] = '"'

    -- calculate and add the colspan (always full table width)
    local cs = 2
    if common.isset(frame.args['imagen izquierda']) then cs = cs + 1 end
    if common.isset(frame.args['imagen']) then cs = cs + 1 end
    ret[#ret+1] = ' colspan="' .. cs .. '">\n'

    -- row content
    ret[#ret+1] = text

    ret[#ret+1] = '</td></tr>'
    sep = true -- now we're going to need separation
  end
  return table.concat(ret),sep
end

--[[ Generates a single row of the list. ]]
local function build_row(frame, border, ttype, k, group, list, groupstyle, liststyle)
  local ret = {""}
  if common.isset(group) then
    -- there's a group name for current list, so add a cell for it
    ret[#ret+1] = '<td class="navbox-group" style="'
    if common.isset(colors[ttype]) then 
      ret[#ret+1] = 'background:' .. common.cv((border ~= "subgroup" and border ~= "child"), colors[ttype]['grupo'], colors[ttype]['subgroup']) .. ';'
    end
    ret[#ret+1] = 'padding-left:1em; padding-right:1em; white-space:nowrap; text-align:right;'
    style.add(ret, frame.args['estilo base'])
    if common.isset(frame.args['ancho grupo']) then ret[#ret+1] = 'width:' .. frame.args['ancho grupo'] .. ';' end
    style.add(ret, frame.args['estilo grupo'])
    style.add(ret, groupstyle)
    ret[#ret+1] = '">'
    ret[#ret+1] = '<div style="padding:0;">\n'
    ret[#ret+1] = group
    ret[#ret+1] = '</div></td>'

    -- start the content cell
    ret[#ret+1] = '<td style="text-align:left;border-left-width:2px;border-left-style:solid;'
  else
    -- no group name, start the content already
    ret[#ret+1] = '<td colspan=2 style="'
  end
  -- a bit tricky, but this works as XOR
  if (frame.args['par impar'] == "swap") ~= ((k % 2) == 0) then
    if common.isset(colors[ttype]) then ret[#ret+1] = 'background:' .. colors[ttype]['dark'] .. ';' end
  else
    ret[#ret+1] = 'background:transparent;'
  end
  if not common.isset(frame.args['ancho grupo']) then ret[#ret+1] = 'width:100%;' end
  ret[#ret+1] = 'padding: 0;'
  style.add(ret, frame.args['estilo lista'])
  style.add(ret, common.cv((frame.args['par impar'] == "swap") ~= ((k % 2) == 0), frame.args['estilo par'], frame.args['estilo impar']))
  style.add(ret, liststyle)
  ret[#ret+1] = '" class="navbox-list ' .. common.cv((frame.args['par impar'] == "swap") ~= ((k % 2) == 0), 'navbox-even', 'navbox-odd') .. '">'

  -- add the <div> with content (same <div> that child navbox has to close when starting table)
  ret[#ret+1] = '<div style="padding:'
  if common.isset(frame.args['list' .. k .. 'padding']) then
    ret[#ret+1] = frame.args['list' .. k .. 'padding']
  elseif common.isset(frame.args['listpadding']) then
    ret[#ret+1] = frame.args['listpadding']
  else
    ret[#ret+1] = '0em 0.25em'
  end
  ret[#ret+1] = ';">\n'
  ret[#ret+1] = list
  ret[#ret+1] = '</div></td>'
  return table.concat(ret)
end

--[[ Generates the "body" of table (everything below title) ]]
local function build_body(frame, border, ttype, lists)
  local ret = {}
  local sep = common.isset(frame.args['título']) -- if there was a title, we have to add separation for first row
  local imgs = true -- add images if present
  local irows = common.cv( (#lists > 0), (2 * #lists - 1), 1 )

  -- aff the 'above' row
  ret[#ret+1],sep = build_above_below(frame, border, ttype, frame.args['arriba'], frame.args['estilo arriba'], sep)
  
  for k,v in pairs(lists) do
    if sep then -- add separation if needed
      ret[#ret+1] = '<tr style="height: 2px;"><td></td></tr>'
    end

    table.insert(ret, '<tr>')

    -- add left image (if wasn't added yet and is present)
    if imgs then
      if common.isset(frame.args['imagen izquierda']) then
        ret[#ret+1] = '<td style="width:0%;padding:0px 2px 0px 0px;'
        style.add(ret, frame.args['estilo imagen izquierda'])
        ret[#ret+1] = '" rowspan="' .. irows .. '">' .. frame.args['imagen izquierda'] .. '</td>'
      end
    end

    -- add the list content
    ret[#ret+1] = build_row(frame, border, ttype, v['index'], v['grupo'], v['lista'], v['estilo grupo'], v['estilo lista'])

    -- add right image (if wasn't added yet and is present)
    if imgs then
      if common.isset(frame.args['imagen']) then
        ret[#ret+1] = '<td style="width:0%;padding:0px 0px 0px 2px;'
        style.add(ret, frame.args['estilo imagen'])
        ret[#ret+1] = '" rowspan="' .. irows .. '">' .. frame.args['imagen'] .. '</td>'
      end

      -- mark that images were added already
      imgs = false
    end

    ret[#ret+1] = '</tr>'
    sep = true -- now the separation is going to be needed for sure
  end

  ret[#ret+1],sep = build_above_below(frame, border, ttype, frame.args['abajo'], frame.args['estilo abajo'], sep)

  return table.concat(ret)
end

-- == Exported functions ==
--[[ Generates and returns the whole navbox table. ]]
local function buildNavbox(frame)
  local template = {}
  local border = ""
  local ttype = ""

  if common.isset(frame.args['borde']) then
    border = frame.args['borde']
  elseif common.isset(frame.args[1]) then
    border = mw.text.trim(frame.args[1])
  end

  if common.isset(frame.args['type']) then
    ttype = frame.args['type']
  end

  -- TODO: filtering of border and type values?

  -- prepare the data first
  local lists = scan_lists(frame)

  -- build the content  
  template[#template+1] = start_box(frame, border, ttype)
  template[#template+1] = build_title(frame, border, ttype)
  template[#template+1] = build_body(frame, border, ttype, lists)
  template[#template+1] = end_box(frame, border, ttype)

  return table.concat(template)
end

-- [[ Workaround for the "Navbox subgroup" template ]]
local function buildNavboxSubgroup(frame)
  if not common.isset(frame.args['borde']) then frame.args['borde'] = 'child' end

  -- TODO: add/modify/remove other params?

  -- call the main function
  return buildNavbox(frame)
end

--[[ Mostly for debugging, generates and returns a table of sets of colors
     and the values in cells with colors used as backgrounds. ]]
local function buildColorTable(frame)
  local ret = {""}
  table.insert(ret, '<table class="wikitable" style="text-align: center;">')
  table.insert(ret, '<caption>List of colors in order from darkest to brigthest one</caption>')
  table.insert(ret, '<tr><th>"type"</th><th>title</th><th>above, below</th><th>group,<br/>sub-above/below</th><th>sub-group</th><th>dark background</th><th>background</th></tr>')
  for k,v in pairs(colors) do
    table.insert(ret, '<tr>')
    table.insert(ret, '<td>' .. k .. '</td>')
    table.insert(ret, '<td style="background:' .. v['título'] .. '">' .. v['título'] .. '</td>')
    table.insert(ret, '<td style="background:' .. v['arriba'] .. '">' .. v['arriba'] .. '</td>')
    table.insert(ret, '<td style="background:' .. v['grupo'] .. '">' .. v['grupo'] .. '</td>')
    table.insert(ret, '<td style="background:' .. v['subgroup'] .. '">' .. v['subgroup'] .. '</td>')
    table.insert(ret, '<td style="background:' .. v['dark'] .. '">' .. v['dark'] .. '</td>')
    table.insert(ret, '<td style="background:' .. v['background'] .. '">' .. v['background'] .. '</td>')
    table.insert(ret, '</tr>')
  end
  table.insert(ret, '</table>')
  return table.concat(ret)
end

-- export local functions
return {
  ['buildNavbox'] = buildNavbox,
  ['buildNavboxTemplate'] = function(frame) return buildNavbox(frame:getParent()) end, -- for wrappers
  ['buildNavboxSubgroup'] = buildNavboxSubgroup,
  ['buildNavboxSubgroupTemplate'] = function(frame) return buildNavboxSubgroup(frame:getParent()) end, -- for wrappers
  ['buildColorTable'] = buildColorTable
}

-- [[Category:Lua Scripts|{{PAGENAME}}]]