La documentation pour ce module peut être créée à Module:Gallery/Documentation

--[[  
Le module Gallery sert à ajouter un livre à l’intérieur 
du pseudo-élément gallery utilisé dans le modèle Validation.

EXEMPLE :
Appel à partir d’une page Wikisource ou d’un modèle :
{{#invoke:Gallery|item|Lesueur - À force d'aimer, 1895.djvu}}
Réponse obtenue :
Fichier:Lesueur - À force d'aimer, 1895 (cover).jpg|link=À force d’aimer|
etc
]]

p = {}
info = {}

local indexToWikidata = {
	-- type, titre, image et BNF_ARK sont gérés spécialement
    ['auteur'] = 'P50',
    ['annee'] = 'P577',
}

function p.item(frame)
	local args = frame.args
	local sCode = ''
	if args[1] == nil then
		args = frame:getParent().args
	end
	for i, v in ipairs(args) do
		v = mw.ustring.gsub(v,"[\r\n]", "") --élimination des sauts de ligne
		--v = mw.ustring.gsub(v,"\\", "")
		sCode = sCode .. p.definition(v)
		--szCode = v
    end	
	return frame:preprocess(sCode)
end

--=p.definition("Lesueur - À force d'aimer, 1895.djvu")
function p.definition(livre)
	local szCode = ''
	if p.infoFromIndex(livre) then
		if tonumber(info['image']) ~= nil then
			info['image'] = livre .. '{{!}}page=' .. info['image']
		end
		szCode = 'Fichier:' .. info['image']
		szCode = szCode .. '{{!}}link=' .. info['titrePage']
		szCode = szCode .. "{{!}}'''" .. info['auteur']
		szCode = szCode .. "''',<br />''{{L2S|" .. info['titrePage']
		szCode = szCode .. "|" .. livre
		szCode = szCode .. "|" .. info['titreText']
		szCode = szCode .. "}}'',<br />" .. info['annee']
		szCode = szCode .. '<br/>{{export|' .. info['titrePage']
		
		if info['avancement'] == 'T' then
			szCode = szCode .. '}}{{validé}}'
		elseif info['avancement'] == 'V' then
			szCode = szCode .. '}}{{4/4}}'
		else szCode = szCode .. '}}{{2/4}}'
		end
		local gal = {}
		gal["heights"] = '150px'
		gal["mode"] = 'packed'
		return '\n' .. szCode
		--return '{{#tag:gallery|' ..szCode .. '|heights=200px|mode="packed" }}'
	else
		return '' 
	end
end

function p.infoFromIndex(titre)
	local titre = mw.title.new( titre, 'Livre' )
	local contenu = ''
	local fragment = {}
	local item 
	if titre ~= nil then 
		contenu = titre:getContent()
		local wdata = mw.ustring.match(contenu, 'wikidata_item=(.-)|')
		if wdata and wdata ~= '' then wdata = mw.text.trim(wdata) end
 		if wdata and wdata ~= '' then
			item = mw.wikibase.getEntity(wdata)
		end 
		info['auteur'] = mw.ustring.match(contenu, 'Auteur=[%C]*(%[%[.-%]%])') or ''
 			if info['auteur'] == '' then
				info['auteur'] = getWikidataItemValue(item, 'auteur') 
			end
		info['titrePage'] = mw.ustring.match(contenu, 'Titre=%[%[(.-)%]%]') or ''
		if info['titrePage'] ~= '' then 
			fragment = mw.text.split(info['titrePage'],'|')
			if fragment[2] ~= nil then
				info['titrePage'] = fragment[1]
				info['titreText'] = fragment[2]
			else info['titreText'] = info['titrePage']
			end
 		else
			if item then
				local value = item:formatStatements('P1476')['value']
				if value == '' then
					value = item:getLabel() or ''
				end
				if value ~= '' then
					local siteLink =  item:getSitelink()
					if siteLink then
						info['titrePage'] = siteLink
						info['titreText'] = value
					else 
						info['titrePage'] = value
					end
				end
			end
		end
		info['annee'] = mw.text.trim(mw.ustring.match(contenu, 'Annee=(.-)|')) or ''
 			if info['annee'] == '' then
				info['annee'] = getWikidataItemValue(item, 'annee') 
			end
		info['image'] = mw.text.trim(mw.ustring.match(contenu, 'Image=(.-)|'))
		info['image'] = mw.ustring.match(info['image'], ':-(.-)$')
		info['avancement'] = mw.text.trim(mw.ustring.match(contenu, 'Avancement=(.-)|'))
	else return false
	end
	if info['image'] == nil or info['titreText'] == nil or info['auteur'] == nil then
		return false
	else return true
	end
end

function getWikidataItemValue(item, element)
	if item then
		-- élément depuis Wikidata
		return item:formatStatements(indexToWikidata[element])["value"] or ''
	else
	return ''
	end
end

return p