Module:MedicoInfobox: Difference between revisions

From determinar.ia.br - Determine suas informações
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


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


local function formatarValor(dv, propLabelLower)
local function formatarValor(dv, propertyId)
     if dv.type == 'string' then
     if dv.type == 'string' then
         if NOMES_PROPRIEDADE_FOTO[propLabelLower] then
         if propertyId == PROPRIEDADE_FOTO then
             return nil, dv.value
             return nil, dv.value
         end
         end
Line 47: Line 41:
         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 53: Line 46:
                 local snak = claim.mainsnak
                 local snak = claim.mainsnak
                 if snak and snak.datavalue then
                 if snak and snak.datavalue then
                     local formatado, arquivoImagem = formatarValor(snak.datavalue, propLabelLower)
                     local formatado, arquivoImagem = formatarValor(snak.datavalue, propertyId)
                     if arquivoImagem then
                     if arquivoImagem then
                         imagem = arquivoImagem
                         imagem = arquivoImagem
Line 62: Line 55:
             end
             end


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

Revision as of 11:41, 1 August 2026

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

local p = {}

local PROPRIEDADE_FOTO = 'P1'

local function formatarValor(dv, propertyId)
    if dv.type == 'string' then
        if propertyId == PROPRIEDADE_FOTO 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 valores = {}

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

            if propertyId ~= PROPRIEDADE_FOTO and #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