Module:ZoteroAPI
La documentation pour ce module peut être créée à Module:ZoteroAPI/doc
local p = {}
-- Fonction utilitaire pour récupérer et décoder les données Zotero
function p._fetchZoteroData(itemKey)
if not itemKey or itemKey == '' then
return nil
end
local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'
local data = mw.ext.externalData.getExternalData({
url = url,
format = 'json'
})
local decoded
if type(data) == "string" then
local ok, result = pcall(mw.text.jsonDecode, data)
if not ok or type(result) ~= "table" then
return nil
end
decoded = result
elseif type(data) == "table" then
decoded = data
else
return nil
end
if decoded.__json and type(decoded.__json) == "table" then
return decoded.__json
end
return decoded
end
-- Fonction pour afficher le JSON formaté
function p.debugRawJson(frame)
local itemKey = frame.args[1]
if not itemKey or itemKey == '' then
return "❌ Aucun itemKey fourni."
end
local d = p._fetchZoteroData(itemKey)
if not d then
return "❌ Aucune donnée reçue ou erreur de décodage."
end
-- Fonction d’indentation
local function indentJson(json)
local indent = 0
local formatted = {}
local inString = false
for i = 1, #json do
local c = json:sub(i, i)
if c == '"' and json:sub(i - 1, i - 1) ~= '\\' then
inString = not inString
end
if not inString then
if c == '{' or c == '[' then
table.insert(formatted, c .. '\n' .. string.rep(' ', indent + 1))
indent = indent + 1
elseif c == '}' or c == ']' then
indent = indent - 1
table.insert(formatted, '\n' .. string.rep(' ', indent) .. c)
elseif c == ',' then
table.insert(formatted, ',\n' .. string.rep(' ', indent))
else
table.insert(formatted, c)
end
else
table.insert(formatted, c)
end
end
return table.concat(formatted)
end
local raw = mw.text.jsonEncode(d)
return '<pre>' .. indentJson(raw) .. '</pre>'
end
return p