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

Aucun résumé des modifications
Aucun résumé des modifications
 
(5 versions intermédiaires par le même utilisateur non affichées)
Ligne 17 : Ligne 17 :
end
end
return data.json
return data.json
end
-- Fonction utilitaire de formatage
local function htmlDecode(str)
if not str then return nil end
return str
:gsub(":", ":")
:gsub("[", "[")
:gsub("]", "]")
:gsub(""", '"')
:gsub("&", "&")
end
end


Ligne 42 : Ligne 53 :
function p.alternateLink(frame)
function p.alternateLink(frame)
local data = fetchZoteroData(frame.args[1])
local data = fetchZoteroData(frame.args[1])
if not data or not data.links or not data.links.alternate then return "" end
if data and data.links and data.links.alternate and data.links.alternate.href then
return mw.text.nowiki(data.links.alternate.href or "")
return htmlDecode(data.links.alternate.href)
end
return ""
end
end


Ligne 91 : Ligne 104 :


function p.url(frame)
function p.url(frame)
return getField(frame, { "data", "url" })
local data = fetchZoteroData(frame.args[1])
end
if data and data.data and data.data.url then
 
return htmlDecode(data.data.url)
function p.accessDate(frame)
end
return getField(frame, { "data", "accessDate" })
return ""
end
end


Ligne 104 : Ligne 117 :
function p.extra(frame)
function p.extra(frame)
return getField(frame, { "data", "extra" })
return getField(frame, { "data", "extra" })
end
local function trimDate(dateStr)
if type(dateStr) == "string" then
return mw.text.nowiki(dateStr:sub(1, 10))
end
return ""
end
end


function p.dateAdded(frame)
function p.dateAdded(frame)
return getField(frame, { "data", "dateAdded" })
local data = fetchZoteroData(frame.args[1])
if not data or not data.data or not data.data.dateAdded then return "" end
return trimDate(data.data.dateAdded)
end
end


function p.dateModified(frame)
function p.dateModified(frame)
return getField(frame, { "data", "dateModified" })
local data = fetchZoteroData(frame.args[1])
if not data or not data.data or not data.data.dateModified then return "" end
return trimDate(data.data.dateModified)
end
 
function p.accessDate(frame)
local data = fetchZoteroData(frame.args[1])
if not data or not data.data or not data.data.accessDate then return "" end
return trimDate(data.data.accessDate)
end
end


Ligne 192 : Ligne 222 :
end
end


if #urls == 0 then
return table.concat(urls, ", ")
end
function p.notes(frame)
local itemKey = frame.args[1]
if not itemKey or itemKey == "" then return "" end
 
local url = string.format(
"https://api.zotero.org/groups/4893620/items/%s/children?format=json&include=data",
mw.uri.encode(itemKey)
)
 
local data, errors = mw.ext.externalData.getExternalData{
url = url,
format = "JSON",
data = { notes = "__json" }
}
 
if errors or not data or not data.notes then
return ""
return ""
end
end


return table.concat(urls, ", ")
local output = {}
 
for _, item in ipairs(data.notes) do
if item.data and item.data.itemType == "note" then
local rawHtml = item.data.note or ""
local url = (item.links and item.links.alternate and item.links.alternate.href) or ""
 
-- Étape 1 : supprimer tout sauf les balises autorisées
-- Autorisé : <p>, </p>, <h1>, </h1>, <a href="...">, </a>
local cleaned = rawHtml:gsub('<(.-)>', function(tag)
-- Cas <a href="...">
if tag:match('^a%s+href=') then
return "<" .. tag .. ">"
end
-- Cas </a>, <p>, </p>, <h1>, </h1>
if tag == "/a" or tag == "p" or tag == "/p" or tag == "h1" or tag == "/h1" then
return "<" .. tag .. ">"
end
-- Sinon : supprimer
return ""
end)
 
if cleaned ~= "" then
table.insert(output, cleaned)
end
if url ~= "" then
table.insert(output, url)
end
end
end
 
return table.concat(output, "\n\n")
end
end
return p
return p