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

De alcolois
Aller à la navigation Aller à la recherche
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
local p = {}
local p = {}


function p.getItemRawJson(frame)
-- Fonction utilitaire pour récupérer et décoder les données Zotero
    local itemKey = frame.args[1]
function p._fetchZoteroData(itemKey)
    if not itemKey or itemKey == '' then
if not itemKey or itemKey == '' then
        return '❌ Aucun itemKey fourni.'
return nil
    end
end


    local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'
local url = 'https://api.zotero.org/groups/4893620/items/' .. itemKey .. '?include=data&format=json'


    local result = mw.ext.externalData.getExternalData({
local data = mw.ext.externalData.getExternalData({
        url = url,
url = url,
        format = 'json'
format = 'json'
    })
})


    if not result or type(result) ~= 'table' then
local decoded
        return '❌ Aucune donnée reçue.'
if type(data) == "string" then
    end
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


    -- Représente le tableau Lua en JSON formaté
if decoded.__json and type(decoded.__json) == "table" then
    local ok, json = pcall(mw.text.jsonEncode, result)
return decoded.__json
    if not ok then
end
        return '❌ Erreur lors de l’encodage JSON.'
    end


    return '<pre>' .. json .. '</pre>'
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
end


return p
return p

Version du 11 juin 2025 à 16:02

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