Ir al contenido

Diferencia entre revisiones de «Módulo:Ficha»

Sin resumen de edición
Sin resumen de edición
Línea 37: Línea 37:


local function addRow(rowArgs)
local function addRow(rowArgs)
     if rowArgs.header then
     if rowArgs.seccion then
         root
         root
             .tag('tr')
             .tag('tr')
Línea 46: Línea 46:
                     .cssText(args.headerstyle)
                     .cssText(args.headerstyle)
                     .wikitext(rowArgs.header)
                     .wikitext(rowArgs.header)
     elseif rowArgs.data then
     elseif rowArgs.datos then
         local row = root.tag('tr')
         local row = root.tag('tr')
         row.addClass(rowArgs.rowclass)
         row.addClass(rowArgs.rowclass)
         if rowArgs.label then
         if rowArgs.etiqueta then
             row
             row
                 .tag('th')
                 .tag('th')
                     .attr('scope', 'row')
                     .attr('scope', 'row')
                     .css('text-align', 'left')
                     .css('text-align', 'left')
                     .cssText(args.labelstyle)
                     .cssText(args.estiloetiqueta)
                     .wikitext(rowArgs.label)
                     .wikitext(rowArgs.etiqueta)
                     .done()
                     .done()
         end
         end
          
          
         local dataCell = row.tag('td')
         local dataCell = row.tag('td')
         if not rowArgs.label then  
         if not rowArgs.etiqueta then  
             dataCell
             dataCell
                 .attr('colspan', 2)
                 .attr('colspan', 2)
Línea 66: Línea 66:
         end
         end
         dataCell
         dataCell
             .addClass(rowArgs.class)
             .addClass(rowArgs.clase)
             .cssText(rowArgs.datastyle)
             .cssText(rowArgs.estilodatos)
             .wikitext(rowArgs.data)
             .wikitext(rowArgs.datos)
     end
     end
end
end
Línea 116: Línea 116:
         args.subheader1 = args.subtitulo
         args.subheader1 = args.subtitulo
     end
     end
     if args.subheaderrowclass then
     if args.clasesubtitulo then
         args.clasesubtitulo1 = args.clasesubtitulo
         args.clasesubtitulo1 = args.clasesubtitulo
     end
     end
Línea 139: Línea 139:
     local imagenums = getArgNums('imagen')
     local imagenums = getArgNums('imagen')
     for k, num in ipairs(imagenums) do
     for k, num in ipairs(imagenums) do
         local caption = args['pie' .. num]
         local pie = args['pie' .. num]
         local data = HtmlBuilder.create().wikitext(args['imagen' .. num])
         local datos = HtmlBuilder.create().wikitext(args['imagen' .. num])
         if pie then
         if pie then
             data
             datos
                 .tag('br', {selfClosing = true})
                 .tag('br', {selfClosing = true})
                     .done()
                     .done()
Línea 222: Línea 222:
          
          
         if args.titulo then
         if args.titulo then
             root.wikitext("'''" .. args.title .. "'''")
             root.wikitext("'''" .. args.titulo .. "'''")
         end
         end
     end
     end

Revisión del 20:14 26 may 2013

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.class)
                    .css('text-align', 'center')
                    .cssText(args.headerstyle)
                    .wikitext(rowArgs.header)
    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.above 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.subheader then
        args.subheader1 = 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