« Module:Matériau » : différence entre les versions
Apparence
Modèle:Infobox>Zolo mAucun résumé des modifications |
Modèle:Infobox>Zolo un peu plus simple |
||
| Ligne 9 : | Ligne 9 : | ||
} | } | ||
function p.formatFromItem(item, | function p.formatFromItem(item, params) -- affiche le matériau en fonction de Property:P186, et de son qualificatif P518 ('s'applique à') | ||
params = params or {} | |||
params.speciallabels = speciallabels | |||
local | |||
if not | |||
local claims = wikidata.getClaims{entity = item, property = 'P186'} | |||
if not claims then | |||
return nil | return nil | ||
end | end | ||
-- | -- crée trois tables : main pour celle sans P518, support pour celle avec qualif "s'applique à "support de peinture" et "parts" pour les autres | ||
local | local main, support, parts = {}, {}, {} | ||
local function assignClaim(claim) -- met une claims dans la bonne table | |||
local function | local str = wikidata.formatStatement(claim, params) | ||
local | if (not claim.qualifiers) or (not claim.qualifiers.P518) then | ||
table.insert(main, str) | |||
for | else | ||
for _, val in pairs(claim.qualifiers.P518) do | |||
local key = wikidata.getid(val) | |||
if key == 'Q861259' then | |||
table.insert(support, str) | |||
else | |||
parts[key] = parts[key] or {} | |||
table.insert(parts[key], str) | |||
end | |||
end | |||
end | end | ||
end | end | ||
for i, j in pairs(claims) do | |||
assignClaim(j) | |||
end | end | ||
if mainstr | local str -- la chaîne à retourner | ||
-- transforme en chaîne la table des valeurs sans qualificatif | |||
local mainstr = linguistic.conj(main) | |||
-- ajoute le support de peinture (sur toile) | |||
local supportstr = linguistic.conj(support) | |||
if (mainstr and supportstr) then | |||
str = mainstr .. ' sur ' .. supportstr | |||
end | end | ||
-- | -- chaîne des autres valeurs avec qualifs: [piédestal] = {marbre, bronze} => 'piédestal en marbre et bronze' | ||
local | local formattedparts = {} | ||
for part, materials in pairs(parts) do | |||
for part, materials in pairs( | local str = wikidata.formatEntity(part, {link = '-'}) .. ' en ' .. linguistic.conj(materials) | ||
local str = wikidata.formatEntity(part, {link = '-'}) | table.insert(formattedparts, str) | ||
table.insert( | |||
end | end | ||
local partsStr = linguistic.conj(formattedparts, 'comma') | |||
str = linguistic.conj({str, partsStr}, 'comma') | |||
if ( | if (params.linkback ~= '-') then | ||
str = wikidata.addLinkback(str, item, 'P186') | |||
end | end | ||
return | return str .. wikidata.addtrackingcat('P186') | ||
end | end | ||
return p | return p | ||
Version du 27 août 2015 à 14:11
La documentation pour ce module peut être créée à Module:Matériau/doc
local p = {}
local wikidata = require 'Module:Interface Wikidata'.fromLua
local linguistic = require 'Module:Linguistique'
local speciallabels = { -- libellés définis localement, qui priment sur ceux de Wikidata
Q296955 = '[[Peinture à l\'huile|huile]]',
Q22731 = '[[Pierre naturelle|pierre]]',
Q4259259 = '[[Toile (peinture)|toile]]',
}
function p.formatFromItem(item, params) -- affiche le matériau en fonction de Property:P186, et de son qualificatif P518 ('s'applique à')
params = params or {}
params.speciallabels = speciallabels
local claims = wikidata.getClaims{entity = item, property = 'P186'}
if not claims then
return nil
end
-- crée trois tables : main pour celle sans P518, support pour celle avec qualif "s'applique à "support de peinture" et "parts" pour les autres
local main, support, parts = {}, {}, {}
local function assignClaim(claim) -- met une claims dans la bonne table
local str = wikidata.formatStatement(claim, params)
if (not claim.qualifiers) or (not claim.qualifiers.P518) then
table.insert(main, str)
else
for _, val in pairs(claim.qualifiers.P518) do
local key = wikidata.getid(val)
if key == 'Q861259' then
table.insert(support, str)
else
parts[key] = parts[key] or {}
table.insert(parts[key], str)
end
end
end
end
for i, j in pairs(claims) do
assignClaim(j)
end
local str -- la chaîne à retourner
-- transforme en chaîne la table des valeurs sans qualificatif
local mainstr = linguistic.conj(main)
-- ajoute le support de peinture (sur toile)
local supportstr = linguistic.conj(support)
if (mainstr and supportstr) then
str = mainstr .. ' sur ' .. supportstr
end
-- chaîne des autres valeurs avec qualifs: [piédestal] = {marbre, bronze} => 'piédestal en marbre et bronze'
local formattedparts = {}
for part, materials in pairs(parts) do
local str = wikidata.formatEntity(part, {link = '-'}) .. ' en ' .. linguistic.conj(materials)
table.insert(formattedparts, str)
end
local partsStr = linguistic.conj(formattedparts, 'comma')
str = linguistic.conj({str, partsStr}, 'comma')
if (params.linkback ~= '-') then
str = wikidata.addLinkback(str, item, 'P186')
end
return str .. wikidata.addtrackingcat('P186')
end
return p