« Module:ZoteroItem » : différence entre les versions

Aucun résumé des modifications
Aucun résumé des modifications
Ligne 157 : Ligne 157 :
function p.relations(frame)
function p.relations(frame)
local itemKey = frame.args[1]
local itemKey = frame.args[1]
local groupName = "alcolois" -- adapter si besoin
local groupId = "4893620"
local data = fetchZoteroData(itemKey)
local data = fetchZoteroData(itemKey)
if not data or not data.data or not data.data.relations then return "" end
if not data or not data.data or not data.data.relations then return "" end
local rel = data.data.relations
local rel = data.data.relations
local out = {}
local out = {}
for k, v in pairs(rel) do
for k, v in pairs(rel) do
local valueStr = ""
if k == "dc:relation" and type(v) == "table" then
if type(v) == "string" then
for _, zoteroUrl in ipairs(v) do
valueStr = v
local id = tostring(zoteroUrl):match("/items/(%w+)$")
elseif type(v) == "table" then
if id then
valueStr = mw.text.nowiki(mw.text.jsonEncode(v))
local fullUrl = string.format(
else
"https://www.zotero.org/groups/%s/%s/items/%s",
valueStr = tostring(v)
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
table.insert(out, mw.text.nowiki(k .. ": " .. valueStr))
end
end
return table.concat(out, ", ")
 
if #out > 0 then
return "dc:relation:<ul>" .. table.concat(out, "") .. "</ul>"
end
 
return ""
end
end


return p
return p