Modulo:Immagine sinottico

Da Semi del Verbo, l'enciclopedia dell'influenza del Vangelo sulla cultura
Versione del 28 ago 2020 alle 15:05 di Johnrdorazio (discussione | contributi) (una versione importata)

La documentazione per questo modulo può essere creata in Modulo:Immagine sinottico/man

--[[
* Modulo che implementa il template Immagine sinottico.
]]--

require('Modulo:No globals')

local getArgs = require('Modulo:Arguments').getArgs
local mWikidata = require('Modulo:Wikidata')
local catCompatibile = '[[Categoria:Sinottici con immagini formattate a mano]]'
local catNonCompatibile = '[[Categoria:Sinottici con immagini con formattazione ridondante]]'
local catDidascalia = '[[Categoria:Voci con template Immagine sinottico con didascalia e immagine letta da Wikidata]]'
local p = {}

local function formatImage(file, args)
	local dim = args.dim_utente and
				math.min(args.dim_utente, args.dim_max or args.dim or 260) or
				(args.dim or 260)
	return string.format('[[File:%s|frameless|center|%sx%spx%s%s]]',
						  file, dim, args.dim_vert_max or 300,
						  args.alt and '|alt=' .. args.alt or '',
						  args.desc and '|' .. args.desc or '')
end

-- Per l'utilizzo da altro modulo
function p._main(args)
	local userval, wdval, ret

	-- evita letture indesiderate da wikidata
	if args[1] == 'no' then return '' end

	-- controllo quadre in eccesso
	if args[1] and args[1]:sub(1, 1) == '[' then
		local ns0 = mw.title.getCurrentTitle().namespace == 0
		-- con args.compatibile restituisce direttamente args[1] e args[2]
		if args.compatibile then
			local cat = ns0 and catCompatibile or ''
			return args[1] .. (args[2] and ('<br />' .. args[2]) or '') .. cat
		elseif ns0 then
			ret = catNonCompatibile
		end
	end

	args.dim = tonumber(args.dim)
	args.dim_max = tonumber(args.dim_max)
	args.dim_utente	= args.dim_utente and tonumber(mw.text.split(args.dim_utente, 'px')[1])
	args.dim_vert_max = tonumber(args.dim_vert_max)

	-- valore utente e wikidata
	userval = args[1]
	if not userval and args['proprietà'] then
		wdval = mWikidata._getProperty({ args['proprietà'], n = 1 })
	end

	-- formatta l'immagine
	if userval or wdval then
		ret = (ret or '') .. formatImage(userval or wdval, args)
	end

	-- didascalia
	if userval then
		ret = ret .. (args[2] or '')
	elseif wdval then
		-- se l'immagine è letta da Wikidata la didascalia può essere ottenuta
		-- solo dal qualificatore P2096, altrimenti restituirà una categoria di errore
		ret = ret .. (mWikidata._getQualifier({ args['proprietà'], 'P2096', includelang = 'it', n = 1 }) or '')
	end

	return (ret or '') .. ((args[2] and wdval and not userval) and catDidascalia or '')
end

-- Funzione per il template {{Immagine sinottico}}
function p.main(frame)
	return p._main(getArgs(frame, { parentOnly = true }))
end

return p