Module:MedicoInfobox: Difference between revisions

From determinar.ia.br - Determine suas informações
Created page with "local p = {} function p.main(frame) local id = frame.args.ID or frame.args[1] local entity = mw.wikibase.getEntity(id) if not entity then return '<strong>Item não encontrado: ' .. tostring(id) .. '</strong>' end local label = entity:getLabel('pt-br') or entity:getLabel('pt') or entity:getLabel('en') or id local description = entity:getDescription('pt-br') or entity:getDescription('pt') or '' local html = mw.html.create('table')..."
 
No edit summary
Line 1: Line 1:
local p = {}
local p = {}
local NOMES_PROPRIEDADE_FOTO = {
    ['foto'] = true,
    ['imagem'] = true,
    ['photo'] = true,
    ['image'] = true,
    ['picture'] = true,
}
local function formatarValor(dv, propLabelLower)
    if dv.type == 'string' then
        if NOMES_PROPRIEDADE_FOTO[propLabelLower] then
            return nil, dv.value
        end
        return dv.value
    elseif dv.type == 'monolingualtext' then
        return dv.value.text
    elseif dv.type == 'wikibase-entityid' then
        local id = dv.value.id
        local label = mw.wikibase.label(id) or id
        return '[[Item:' .. id .. '|' .. label .. ']]'
    elseif dv.type == 'quantity' then
        return dv.value.amount
    elseif dv.type == 'time' then
        return dv.value.time
    else
        return tostring(dv.value)
    end
end


function p.main(frame)
function p.main(frame)
Line 12: Line 41:
     local description = entity:getDescription('pt-br') or entity:getDescription('pt') or ''
     local description = entity:getDescription('pt-br') or entity:getDescription('pt') or ''


     local html = mw.html.create('table')
     local imagem = nil
        :addClass('infobox-medico')
     local linhas = {}
 
    html:tag('tr')
        :tag('th'):attr('colspan', '2'):css('text-align', 'center'):wikitext(label)
 
     if description ~= '' then
        html:tag('tr')
            :tag('td'):attr('colspan', '2'):css('font-style', 'italic'):wikitext(description)
    end


     if entity.claims then
     if entity.claims then
         for propertyId, claims in pairs(entity.claims) do
         for propertyId, claims in pairs(entity.claims) do
             local propLabel = mw.wikibase.label(propertyId) or propertyId
             local propLabel = mw.wikibase.label(propertyId) or propertyId
            local propLabelLower = mw.ustring.lower(propLabel)
             local valores = {}
             local valores = {}


Line 31: Line 53:
                 local snak = claim.mainsnak
                 local snak = claim.mainsnak
                 if snak and snak.datavalue then
                 if snak and snak.datavalue then
                     local dv = snak.datavalue
                     local formatado, arquivoImagem = formatarValor(snak.datavalue, propLabelLower)
                    local val
                     if arquivoImagem then
 
                         imagem = arquivoImagem
                     if dv.type == 'string' then
                     elseif formatado then
                        val = dv.value
                         table.insert(valores, formatado)
                    elseif dv.type == 'monolingualtext' then
                        val = dv.value.text
                    elseif dv.type == 'wikibase-entityid' then
                         val = mw.wikibase.label(dv.value.id) or dv.value.id
                     elseif dv.type == 'quantity' then
                         val = dv.value.amount
                    elseif dv.type == 'time' then
                        val = dv.value.time
                    else
                        val = tostring(dv.value)
                     end
                     end
                    table.insert(valores, val)
                 end
                 end
             end
             end


             if #valores > 0 then
             if #valores > 0 then
                 local row = html:tag('tr')
                 table.insert(linhas, { label = propLabel, valor = table.concat(valores, ', ') })
                row:tag('th'):wikitext(propLabel)
                row:tag('td'):wikitext(table.concat(valores, ', '))
             end
             end
         end
         end
    end
    local html = mw.html.create('div'):addClass('infobox-medico')
    if imagem then
        html:tag('div')
            :addClass('infobox-medico-imagem')
            :wikitext('[[File:' .. imagem .. '|280px|center]]')
    end
    html:tag('div'):addClass('infobox-medico-titulo'):wikitext(label)
    if description ~= '' then
        html:tag('div'):addClass('infobox-medico-descricao'):wikitext(description)
    end
    local tabela = html:tag('table'):addClass('infobox-medico-tabela')
    for _, linha in ipairs(linhas) do
        local tr = tabela:tag('tr')
        tr:tag('th'):wikitext(linha.label)
        tr:tag('td'):wikitext(linha.valor)
     end
     end



Revision as of 11:38, 1 August 2026

Documentation for this module may be created at Module:MedicoInfobox/doc

local p = {}

local NOMES_PROPRIEDADE_FOTO = {
    ['foto'] = true,
    ['imagem'] = true,
    ['photo'] = true,
    ['image'] = true,
    ['picture'] = true,
}

local function formatarValor(dv, propLabelLower)
    if dv.type == 'string' then
        if NOMES_PROPRIEDADE_FOTO[propLabelLower] then
            return nil, dv.value
        end
        return dv.value
    elseif dv.type == 'monolingualtext' then
        return dv.value.text
    elseif dv.type == 'wikibase-entityid' then
        local id = dv.value.id
        local label = mw.wikibase.label(id) or id
        return '[[Item:' .. id .. '|' .. label .. ']]'
    elseif dv.type == 'quantity' then
        return dv.value.amount
    elseif dv.type == 'time' then
        return dv.value.time
    else
        return tostring(dv.value)
    end
end

function p.main(frame)
    local id = frame.args.ID or frame.args[1]
    local entity = mw.wikibase.getEntity(id)

    if not entity then
        return '<strong>Item não encontrado: ' .. tostring(id) .. '</strong>'
    end

    local label = entity:getLabel('pt-br') or entity:getLabel('pt') or entity:getLabel('en') or id
    local description = entity:getDescription('pt-br') or entity:getDescription('pt') or ''

    local imagem = nil
    local linhas = {}

    if entity.claims then
        for propertyId, claims in pairs(entity.claims) do
            local propLabel = mw.wikibase.label(propertyId) or propertyId
            local propLabelLower = mw.ustring.lower(propLabel)
            local valores = {}

            for _, claim in ipairs(claims) do
                local snak = claim.mainsnak
                if snak and snak.datavalue then
                    local formatado, arquivoImagem = formatarValor(snak.datavalue, propLabelLower)
                    if arquivoImagem then
                        imagem = arquivoImagem
                    elseif formatado then
                        table.insert(valores, formatado)
                    end
                end
            end

            if #valores > 0 then
                table.insert(linhas, { label = propLabel, valor = table.concat(valores, ', ') })
            end
        end
    end

    local html = mw.html.create('div'):addClass('infobox-medico')

    if imagem then
        html:tag('div')
            :addClass('infobox-medico-imagem')
            :wikitext('[[File:' .. imagem .. '|280px|center]]')
    end

    html:tag('div'):addClass('infobox-medico-titulo'):wikitext(label)

    if description ~= '' then
        html:tag('div'):addClass('infobox-medico-descricao'):wikitext(description)
    end

    local tabela = html:tag('table'):addClass('infobox-medico-tabela')
    for _, linha in ipairs(linhas) do
        local tr = tabela:tag('tr')
        tr:tag('th'):wikitext(linha.label)
        tr:tag('td'):wikitext(linha.valor)
    end

    return tostring(html)
end

return p