Module:ZoteroItem

De alcolois
Aller à la navigation Aller à la recherche

La documentation pour ce module peut être créée à Module:ZoteroItem/doc

local p = {}

function p.getItem(frame)
    local key = frame.args[1]
    if not key or key == "" then
        return "Erreur : itemKey requis"
    end

    local groupId = "4893620"
    local url = string.format(
        "https://api.zotero.org/groups/%s/items/%s?format=json&include=data",
        groupId,
        key
    )

    local data, errors = mw.ext.externalData.getExternalData{
        url = url,
        format = "JSON",
        data = { json = "__json" }
    }

    if errors then
        return "Erreur ExternalData : " .. table.concat(errors, "; ")
    elseif not data or not data.json then
        return "Erreur : aucune donnée retournée"
    end

    local d = data.json.data
    if not d then
        return "Erreur : champ 'data' manquant dans le JSON"
    end

    local escape = mw.text.nowiki  -- alias utile

    local out = {}
    table.insert(out, "<strong>Titre :</strong> " .. escape(d.title or d.caseName or "(sans titre)"))

    if d.creators then
        local authors = {}
        for _, c in ipairs(d.creators) do
            table.insert(authors, escape((c.firstName or "") .. " " .. (c.lastName or "")))
        end
        table.insert(out, "<br><strong>Auteurs :</strong> " .. table.concat(authors, ", "))
    end

    if d.dateDecided then
        table.insert(out, "<br><strong>Date :</strong> " .. escape(d.dateDecided))
    end

    if d.abstractNote then
        table.insert(out, "<br><strong>Résumé :</strong> " .. escape(d.abstractNote))
    end

    if d.url then
        table.insert(out, '<br><strong>Lien :</strong> <a href="' .. escape(d.url) .. '" target="_blank">Voir</a>')
    end

    return table.concat(out)
end

return p