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

Aucun résumé des modifications
Aucun résumé des modifications
 
(9 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 41 : Ligne 52 :


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


Ligne 89 : 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 102 : 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 157 : Ligne 189 :
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 groupName = "alcolois"
local groupId = "4893620"
local groupId = "4893620"


Ligne 164 : Ligne 196 :


local rel = data.data.relations
local rel = data.data.relations
local out = {}
local urls = {}


for k, v in pairs(rel) do
for k, v in pairs(rel) do
if k == "dc:relation" and type(v) == "table" then
if k == "dc:relation" then
for _, zoteroUrl in ipairs(v) do
if type(v) == "string" then
local id = tostring(zoteroUrl):match("/items/(%w+)$")
local id = v:match("/items/(%w+)$")
if id then
if id then
local fullUrl = string.format(
table.insert(urls, string.format(
"https://www.zotero.org/groups/%s/%s/items/%s",
"https://www.zotero.org/groups/%s/%s/items/%s",
groupId,
groupId, groupName, id
groupName,
))
id
end
)
elseif type(v) == "table" then
table.insert(out, '<li><a href="' .. fullUrl .. '" target="_blank">' .. fullUrl .. '</a></li>')
for _, url in ipairs(v) do
local id = tostring(url):match("/items/(%w+)$")
if id then
table.insert(urls, string.format(
"https://www.zotero.org/groups/%s/%s/items/%s",
groupId, groupName, id
))
end
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 #out > 0 then
return table.concat(urls, ", ")
return "dc:relation:<ul>" .. table.concat(out, "") .. "</ul>"
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 ""
end
 
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
end


return ""
return table.concat(output, "\n\n")
end
end
return p
return p