Modulo:Inno nazionale

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca
local getArgs = require('Modulo:Arguments').getArgs


local function getComponimento(args)
	
	local params       = { 'Lingua(%d+)', 'SpecificazioneTesto(%d+)', 'TitoloTesto(%d+)', 'AutoreTesto(%d+)', 'EpocaTesto(%d+)', 'Strofe(%d+)', 'SchemaMetrico(%d+)' }
	local componimento = { }
	
	for k, v in pairs(args) do
		if type(k) == 'string' then
			for _, param in ipairs(params) do
				local num = k:match('^' .. param .. '$')
				if num then
					num = tonumber(num)
					componimento[num - 1] = componimento[num - 1] or { }
					componimento[num - 1][param:gsub('%(%%d%+%)', '')] = v
					break
				end
			end
		end
	end
	
	return componimento or { }
	
end


local function getLingue(args)

	local lingue = { }
	local param = 'Lingua(%d+)'

	for k, v in pairs(args) do
		if type(k) == 'string' then
			local num = k:match('^' .. param .. '$')
			if num then
				num = tonumber(num)
				lingue[num - 1] = lingue[num - 1] or { }
				lingue[num - 1][param:gsub('%(%%d%+%)', '')] = v
			end
		end
	end

	return lingue or { }
	
end


local function getSuccessione(args)

	local successione = { }
	local params = { 'InnoStorico(%d+)', 'InnoStorico(%d+)Inizio', 'InnoStorico(%d+)Fine' }
	
	for k, v in pairs(args) do
		if type(k) == 'string' then
			for _, param in ipairs(params) do
				local num = k:match('^' .. param .. '$')
				if num then
					num = tonumber(num)
					successione[num] = successione[num] or { }
					successione[num][param:gsub('%(%%d%+%)', '')] = v
				end
			end
		end
	end
	
	return successione or { }
	
end


local function formatLingue(lingue, lingua, sort_key)
	
	local fl = ''

	for k, args in pairs(lingue) do
		if k > 0 then
			if args.Lingua then
				if not (args.Lingua == lingua) and not fl:find(args.Lingua) then
					v = require('Modulo:Lingue').get_voce(args.Lingua)
					if v == '' then
						fl = fl .. '<br />[[' .. args.Lingua .. ']]'
					else
						fl = fl .. '<br />[[' .. v .. '|' .. args.Lingua .. ']]'
						if mw.title.getCurrentTitle().namespace == 0 then
							cat = mw.title.new('Inni nazionali in ' .. v, 'Categoria')
							if cat.exists then 
								fl = fl .. '[[Categoria:Inni nazionali in ' .. v
								if sort_key then
									fl = fl .. '|' .. sort_key
								end
								fl = fl ..']]' 
							end
						end
					end
				end
			end
		end
	end

	return fl or ''
	
end


local function formatSuccessione(successione)

	local i = false
	local ini, ult
	local div = mw.html.create()
	
	for k, args in pairs(successione) do
		ini = args.InnoStoricoInizio
		if i and not ini then
			ini = ult
		end
		i = true
		ult = args.InnoStoricoFine
		div
			:tag('tr')
				:css('font-size', '90%')
				:tag('td')
					:css('padding-left', '5px')
					:css('text-align', 'right')
					:wikitext(ini)
					:done()
				:tag('td')
					:wikitext('&#8211;')
					:done()
				:tag('td')
					:css('padding-right', '5px')
					:css('text-align', 'left')
					:wikitext(args.InnoStoricoFine)
					:done()
				:tag('td')
					:css('padding-left', '3px')
					:css('padding-right', '5px')
					:css('text-align', 'left')
					:css('width', '100%')
					:wikitext(args.InnoStorico)
					:done()
	end
	
	return div or { }
	
end


local function formatComponimento(componimento)

	local fc = ''
	local div = mw.html.create()
	
	for k, args in pairs(componimento) do
		if args.Lingua then
			if args.TitoloTesto or args.AutoreTesto or args.EpocaTesto then
				fc = 'Testo in ' .. args.Lingua:gsub("^%l", string.lower)
				if args.SpecificazioneTesto then fc = fc .. ' ' .. args.SpecificazioneTesto end
				div
					:tag('tr')
						:css('background', '#F3F3F3')
						:tag('th')
							:css('font-weight', 'normal')
							:css('border-top', '2px solid #C0C0FF')
							:css('line-height', '1.6em')
							:attr('colspan', 2)
							:wikitext(fc)
							:done()
				if args.TitoloTesto then
					local c = ''
					if not (args.Lingua == 'italiano') then c = require('Modulo:Lingue').lingue({ args.Lingua }) .. ' ' end
					div
						:tag('tr')
							:tag('th')
								:wikitext('Titolo')
								:done()
							:tag('td')
								:wikitext(c .. '<span style="font-style:italic;">' .. args.TitoloTesto .. '</span>')
								:done()
				end
				if args.AutoreTesto then
					div
						:tag('tr')
							:tag('th')
								:wikitext('Autore')
								:done()
							:tag('td')
								:wikitext(args.AutoreTesto)
								:done()
				end
				if args.EpocaTesto then
					div
						:tag('tr')
							:tag('th')
								:wikitext('Epoca')
								:done()
							:tag('td')
								:wikitext(args.EpocaTesto)
								:done()
				end
				if args.Strofe or args.SchemaMetrico then
					div
						:tag('tr')
							:css('background', '#F3F3F3')
							:tag('th')
								:css('font-weight', 'normal')
								:css('line-height', '1.6em')
								:attr('colspan', 2)
								:wikitext('Struttura')
								:done()
					if args.Strofe then
						div
							:tag('tr')
								:tag('th')
									:wikitext('Strofe')
									:done()
								:tag('td')
									:wikitext(args.Strofe)
									:done()
					end
					if args.SchemaMetrico then
						div
							:tag('tr')
								:tag('th')
									:wikitext('Schema metrico')
									:done()
								:tag('td')
									:wikitext(args.SchemaMetrico)
									:done()
					end
				end
			end
		else
			if k == 1 then end
			break
		end
	end

	if not (fc == '') then return div or { } else return fc or '' end
	
end

-- =============================================================================
--                            Funzioni esportate
-- =============================================================================

local p = { }


function p.componimento(frame, arg)
	
	local args         = getArgs(frame, { parentOnly = true })
	local componimento = getComponimento(args) or { }
	local arg          = frame[arg]
	
	componimento = #componimento >= 0 and componimento or { }
	
	if arg == nil then
		return #componimento >= 0 and formatComponimento(componimento) or ''
	else
		return componimento or { }
	end
	
end


function p.lingue(frame)
	
	local args   = getArgs(frame, { parentOnly = true })
	local lingue = args.Lingua and getLingue(args) or { }
	
	lingue = #lingue >= 0 and lingue or { }
	
	return #lingue >= 0 and formatLingue(lingue, args.Lingua, args.ForzaOrdinamento) or ''
	
end


function p.successione(frame)
	
	local args = getArgs(frame, { parentOnly = true })
	local successione = getSuccessione(args) or { }
	
	successione = #successione >= 0 and successione or { }
	
	return #successione >= 0 and formatSuccessione(successione) or ''
	
end


return p