« Module:ZoteroItem » : différence entre les versions
Aller à la navigation
Aller à la recherche
Aucun résumé des modifications Balise : Révoqué |
Annulation des modifications 17552 de Marc (discussion) Balise : Annulation |
||
| Ligne 142 : | Ligne 142 : | ||
-- Collections | -- Collections | ||
function p.collections(frame) | |||
local itemKey = frame.args[1] | |||
local data = fetchZoteroData(itemKey) | |||
if not data or not data.data or not data.data.collections then return "" end | |||
if #data.data.collections == 0 then return "" end | |||
local out = {} | |||
for _, id in ipairs(data.data.collections) do | |||
table.insert(out, mw.text.nowiki(id)) | |||
end | |||
return table.concat(out, ", ") | |||
end | |||
-- Relations | |||
function p.relations(frame) | function p.relations(frame) | ||
local itemKey = frame.args[1] | local itemKey = frame.args[1] | ||
local groupName = "alcolois" | local groupName = "alcolois" -- adapter si besoin | ||
local groupId = "4893620" | local groupId = "4893620" | ||
| Ligne 151 : | Ligne 164 : | ||
local rel = data.data.relations | local rel = data.data.relations | ||
local | local out = {} | ||
for k, v in pairs(rel) do | for k, v in pairs(rel) do | ||
if k == "dc:relation" | if k == "dc:relation" and type(v) == "table" then | ||
for _, zoteroUrl in ipairs(v) do | |||
local id = | local id = tostring(zoteroUrl):match("/items/(%w+)$") | ||
if id then | if id then | ||
local fullUrl = string.format( | |||
"https://www.zotero.org/groups/%s/%s/items/%s", | "https://www.zotero.org/groups/%s/%s/items/%s", | ||
groupId, groupName, id | groupId, | ||
groupName, | |||
id | |||
) | |||
table.insert(out, '<li><a href="' .. fullUrl .. '" target="_blank">' .. fullUrl .. '</a></li>') | |||
end | end | ||
end | |||
elseif type(v) == "string" then | |||
-- single relation | |||
local id = v:match("/items/(%w+)$") | |||
if id then | |||
local fullUrl = string.format( | |||
"https://www.zotero.org/groups/%s/%s/items/%s", | |||
groupId, | |||
groupName, | |||
id | |||
) | |||
table.insert(out, '<li><a href="' .. fullUrl .. '" target="_blank">' .. fullUrl .. '</a></li>') | |||
end | end | ||
end | end | ||
end | end | ||
if # | if #out > 0 then | ||
return "" | return "dc:relation:<ul>" .. table.concat(out, "") .. "</ul>" | ||
end | end | ||
return | return "" | ||
end | end | ||
return p | return p | ||
Version du 12 juin 2025 à 18:12
La documentation pour ce module peut être créée à Module:ZoteroItem/doc
local p = {}
-- Fonction utilitaire pour récupérer les données JSON Zotero
local function fetchZoteroData(itemKey)
if not itemKey or itemKey == "" then return nil end
local url = string.format(
"https://api.zotero.org/groups/4893620/items/%s?format=json&include=data",
itemKey
)
local data, errors = mw.ext.externalData.getExternalData{
url = url,
format = "JSON",
data = { json = "__json" }
}
if errors or not data or not data.json then
return nil
end
return data.json
end
-- Fonction générique pour retourner un champ simple
local function getField(frame, path)
local itemKey = frame.args[1]
local data = fetchZoteroData(itemKey)
if not data then return "" end
local current = data
for _, key in ipairs(path) do
if type(current) ~= "table" or current[key] == nil then
return ""
end
current = current[key]
end
return mw.text.nowiki(tostring(current))
end
-- Fonctions individuelles
function p.key(frame)
return getField(frame, { "key" })
end
function p.alternateLink(frame)
return getField(frame, { "links", "alternate", "href" })
end
function p.caseName(frame)
return getField(frame, { "data", "caseName" })
end
function p.abstractNote(frame)
return getField(frame, { "data", "abstractNote" })
end
function p.court(frame)
return getField(frame, { "data", "court" })
end
function p.dateDecided(frame)
return getField(frame, { "data", "dateDecided" })
end
function p.docketNumber(frame)
return getField(frame, { "data", "docketNumber" })
end
function p.reporter(frame)
return getField(frame, { "data", "reporter" })
end
function p.reporterVolume(frame)
return getField(frame, { "data", "reporterVolume" })
end
function p.firstPage(frame)
return getField(frame, { "data", "firstPage" })
end
function p.history(frame)
return getField(frame, { "data", "history" })
end
function p.language(frame)
return getField(frame, { "data", "language" })
end
function p.shortTitle(frame)
return getField(frame, { "data", "shortTitle" })
end
function p.url(frame)
return getField(frame, { "data", "url" })
end
function p.accessDate(frame)
return getField(frame, { "data", "accessDate" })
end
function p.rights(frame)
return getField(frame, { "data", "rights" })
end
function p.extra(frame)
return getField(frame, { "data", "extra" })
end
function p.dateAdded(frame)
return getField(frame, { "data", "dateAdded" })
end
function p.dateModified(frame)
return getField(frame, { "data", "dateModified" })
end
-- Créateurs (formaté en liste de noms complets)
function p.creators(frame)
local itemKey = frame.args[1]
local data = fetchZoteroData(itemKey)
if not data or not data.data or not data.data.creators then return "" end
local authors = {}
for _, c in ipairs(data.data.creators) do
local name = c.name or ((c.firstName or "") .. " " .. (c.lastName or ""))
if name ~= "" then
table.insert(authors, mw.text.nowiki(name))
end
end
return table.concat(authors, ", ")
end
-- Tags
function p.tags(frame)
local itemKey = frame.args[1]
local data = fetchZoteroData(itemKey)
if not data or not data.data or not data.data.tags then return "" end
local tags = {}
for _, t in ipairs(data.data.tags) do
if t.tag then
table.insert(tags, mw.text.nowiki(t.tag))
end
end
return table.concat(tags, ", ")
end
-- Collections
function p.collections(frame)
local itemKey = frame.args[1]
local data = fetchZoteroData(itemKey)
if not data or not data.data or not data.data.collections then return "" end
if #data.data.collections == 0 then return "" end
local out = {}
for _, id in ipairs(data.data.collections) do
table.insert(out, mw.text.nowiki(id))
end
return table.concat(out, ", ")
end
-- Relations
function p.relations(frame)
local itemKey = frame.args[1]
local groupName = "alcolois" -- adapter si besoin
local groupId = "4893620"
local data = fetchZoteroData(itemKey)
if not data or not data.data or not data.data.relations then return "" end
local rel = data.data.relations
local out = {}
for k, v in pairs(rel) do
if k == "dc:relation" and type(v) == "table" then
for _, zoteroUrl in ipairs(v) do
local id = tostring(zoteroUrl):match("/items/(%w+)$")
if id then
local fullUrl = string.format(
"https://www.zotero.org/groups/%s/%s/items/%s",
groupId,
groupName,
id
)
table.insert(out, '<li><a href="' .. fullUrl .. '" target="_blank">' .. fullUrl .. '</a></li>')
end
end
elseif type(v) == "string" then
-- single relation
local id = v:match("/items/(%w+)$")
if id then
local fullUrl = string.format(
"https://www.zotero.org/groups/%s/%s/items/%s",
groupId,
groupName,
id
)
table.insert(out, '<li><a href="' .. fullUrl .. '" target="_blank">' .. fullUrl .. '</a></li>')
end
end
end
if #out > 0 then
return "dc:relation:<ul>" .. table.concat(out, "") .. "</ul>"
end
return ""
end
return p