Módulo:Ficha
La documentación para este módulo puede ser creada en Módulo:Ficha/doc
--
-- Este módulo es para implementar en {{Ficha}}
--
local p = {}
local HtmlBuilder = require('Module:HtmlBuilder')
local args
local root
function union(t1, t2)
-- devuelve la unión de los valores de dos tablas, como una secuencia
local vals = {}
for k, v in pairs(t1) do
vals[v] = true
end
for k, v in pairs(t2) do
vals[v] = true
end
local ret = {}
for k, v in pairs(vals) do
table.insert(ret, k)
end
return ret
end
local function getArgNums(prefix)
local nums = {}
for k, v in pairs(args) do
local num = ('' .. k):match('^' .. prefix .. '(%d+)$')
if num then table.insert(nums, tonumber(num)) end
end
table.sort(nums)
return nums
end
local function addRow(rowArgs)
if rowArgs.seccion then
root
.tag('tr')
.tag('th')
.attr('colspan', 2)
.addClass(rowArgs.clase)
.css('text-align', 'center')
.cssText(args.estilosubtitulo)
.wikitext(rowArgs.subtitulo)
elseif rowArgs.datos then
local row = root.tag('tr')
row.addClass(rowArgs.rowclass)
if rowArgs.etiqueta then
row
.tag('th')
.attr('scope', 'row')
.css('text-align', 'left')
.cssText(args.estiloetiqueta)
.wikitext(rowArgs.etiqueta)
.done()
end
local dataCell = row.tag('td')
if not rowArgs.etiqueta then
dataCell
.attr('colspan', 2)
.css('text-align', 'center')
end
dataCell
.addClass(rowArgs.clase)
.cssText(rowArgs.estilodatos)
.wikitext(rowArgs.datos)
end
end
local function renderTitle()
if not args.titulo then return end
root
.tag('caption')
.addClass(args.clasetitulo)
.cssText(args.estilotitulo)
.wikitext(args.titulo)
end
local function renderAboveRow()
if not args.titulo then return end
root
.tag('tr')
.tag('th')
.attr('colspan', 2)
.addClass(args.aboveclass)
.css('text-align', 'center')
.css('font-size', '125%')
.css('font-weight', 'bold')
.cssText(args.abovestyle)
.wikitext(args.above)
end
local function renderBelowRow()
if not args.below then return end
root
.tag('tr')
.tag('td')
.attr('colspan', '2')
.addClass(args.belowclass)
.css('text-align', 'center')
.cssText(args.belowstyle)
.newline()
.wikitext(args.below)
.newline()
end
local function renderSubheaders()
if args.subtitulo then
args.subtitulo1 = args.subtitulo
end
if args.clasesubtitulo then
args.clasesubtitulo1 = args.clasesubtitulo
end
local subtitulonumero = getArgNums('subtitulo')
for k, num in ipairs(subtitulonumero) do
addRow({
datos = args['subtitulo' .. num],
estilodatos = args.estilosubtitulo or args['estilosubtitulo' .. num],
clase = args.clasesubtiltulo,
clasefila = args['clasesubtitulo' .. num]
})
end
end
local function renderImages()
if args.imagen then
args.imagen1 = args.imagen
end
if args.caption then
args.pie1 = args.pie
end
local imagenums = getArgNums('imagen')
for k, num in ipairs(imagenums) do
local pie = args['pie' .. num]
local datos = HtmlBuilder.create().wikitext(args['imagen' .. num])
if pie then
datos
.tag('br', {selfClosing = true})
.done()
.tag('span')
.cssText(args.captionstyle)
.wikitext(caption)
end
addRow({
datos = tostring(data),
estilodatos = args.estiloimagen,
clase = args.claseimagen,
clasefila = args['claseimagen' .. num]
})
end
end
local function renderRows()
local rownums = union(getArgNums('seccion'), getArgNums('datos'))
table.sort(rownums)
for k, num in ipairs(rownums) do
addRow({
seccion = args['seccion' .. num],
etiqueta = args['etiqueta' .. num],
datos = args['datos' .. num],
estilodatos = args['estilodatos' .. num],
clase = args['clase' .. num],
clasefila = args['clasefila' .. num]
})
end
end
local function renderNavBar()
if not args.nombre then return end
root
.tag('tr')
.tag('td')
.attr('colspan', '2')
.css('text-align', 'right')
.wikitext(mw.getCurrentFrame():expandTemplate({
title = 'navbar',
args = { args.name, mini = 1 }
}))
end
local function renderItalicTitle()
local italicTitle = args['italic title'] and mw.ustring.lower(args['italic title'])
if italicTitle == '' or italicTitle == 'force' or italicTitle == 'yes' then
root.wikitext(mw.getCurrentFrame():expandTemplate({title = 'italic title'}))
end
end
local function renderTrackingCategories()
if args.decat ~= 'yes' and #(getArgNums('data')) == 0 and mw.title.getCurrentTitle().namespace == 0 then
root.wikitext('[[Categoría:Wikipedia:Artículos que usan ficha sin datos en filas]]')
end
end
local function _infobox()
if args.child ~= 'yes' then
root = HtmlBuilder.create('table')
root
.addClass('infobox_v2')
.addClass(args.bodyclass)
--.css('width', '22.7em')
--.css('line-height', '1.4em')
--.css('text-align', 'left')
--.css('padding', '23em')
.attr('cellspacing', 3)
.css('border-spacing', '3px')
.css('width', '22em')
.cssText(args.bodystyle)
renderTitle()
renderAboveRow()
else
root = HtmlBuilder.create()
if args.titulo then
root.wikitext("'''" .. args.titulo .. "'''")
end
end
renderSubheaders()
renderImages()
renderRows()
renderBelowRow()
renderNavBar()
renderItalicTitle()
renderTrackingCategories()
return tostring(root)
end
function p.infobox(frame)
local origArgs
-- Si se llama a través de #invoke, utilice los argumentos pasados a la plantilla de invocación.
-- De lo contrario, para fines de prueba, asumen args que se pasan directamente.
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
else
origArgs = frame
end
-- ParserFunctions considera que una cadena vacía es falsa, por lo que para preservar el anterior
-- comportamiento de {{Ficha}}, debe cambiar los argumentos vacíos a cero, por lo que Lua los considerará
-- que son falsos también (excepto parámetros 'title cursiva', que especifica un comportamiento diferente
-- dependiendo de si está ausente o vacío)
args = {}
for k, v in pairs(origArgs) do
if v ~= '' or k == 'italic title' then
args[k] = v
end
end
return _infobox()
end
return p