Modulo:Periodo attività musicale/sandbox: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Nuova sandbox
 
Omega Bot (discussione | contributi)
m Bot: rimuovo no globals obsoleto
 
(13 versioni intermedie di un altro utente non mostrate)
Riga 5:
]]--
 
require('Modulo:No globalsstrict')
 
local getArgs = require('Modulo:Arguments').getArgs
local cfg = mw.loadData('Modulo:Periodo attività musicale/Configurazione')
local errorCategory = '[[Categoria:Voci con template Artista musicale con periodo errato]]'
local trackingCategory = 'Voci con template Artista musicale con periodo attività con singolo anno'
local maxIntervals = 10
local p = {}
 
-- Verifica se il decennio è nel formato accettato "anni nnnn" e con un valore consentito.
local function errhandler(msg)
--
local cat = mw.title.getCurrentTitle().namespace == 0 and errorCategory or ''
-- @param {string} value
return string.format('<span class="error">%s</span>%s', msg, cat)
-- @return {string} la stringa non modificata o nil se non è un decennio valido
local function isDecade(value)
return value and (value:match('^anni 19[0-9]0$') or value:match('^anni 20[0-2]0$'))
end
 
-- Parsifica i parametri relativi al periodo di attività musicale:
-- "anno inizio attività N", "anno fine attività N" e "note periodo attività N".
--
-- Restituisce una sequence Lua con elementi con chiavi
-- @param {table} args
-- "start", "ending" ("end" è una parola riservata) e "note".
-- @return {table} sequence Lua con elementi con chiavi "start", "ending"
-- ("end" è una parola riservata) e "note"
local function parseArgs(args)
local ret = {}
Line 33 ⟶ 38:
}
if interval.start or interval.ending then
if interval.start then
if not tonumber(interval.start) then
error('anno inizio attività' .. n .. ' non valido', 3)
end
end
if interval.ending then
if not tonumber(interval.ending) and interval.ending ~= 'in attività' then
error('anno fine attività' .. n .. ' non valido', 3)
end
end
table.insert(ret, interval)
else
Line 52 ⟶ 47:
end
 
-- Formatta il valore del parametro '"anno inizio attività'" del primo intervallo.
--
-- @param {string} value
-- @param {table} confCat
-- @return {string} il valore formattato
-- @return {string} l'eventuale categoria per l'anno di costituzione dell'artista
local function formatStartYear(value, confCat)
local ret, cat
if isDecade(value) then
ret = string.format('[[%s]]', value)
elseif tonumber(value) then
cat = confCat.inizio and (confCat.inizio .. ' ' .. value) or nil
ret = cat and string.format('[[:Categoria:%s|%s]]', cat, value) or string.format('[[%s]]', value)
else
cat = confCat.inizio_assente
ret = value or '?'
end
return ret, cat
end
 
-- Formatta il valore del parametro '"anno fine attività'" dell'ultimo intervallo.
--
-- @param {string} value
-- @param {table} confCat
-- @return {string} il valore formattato
-- @return {string} l'eventuale categoria per l'anno di scioglimento dell'artista
local function formatEndYear(value, confCat)
local ret, cat
if isDecade(value) then
ret = string.format('[[%s]]', value)
if value == 'in attività' then
elseif value == 'in attività' then
cat = confCat.in_attivita
cat = confCat.in_attivita
ret = cat and string.format('[[:%s|%s]]', cat, value) or value
ret = cat and string.format('[[:Categoria:%s|%s]]', cat, value) or value
else
elseif tonumber(value) then
cat = confCat.fine and (confCat.fine .. ' ' .. value) or nil
ret = cat and string.format('[[:Categoria:%s|%s]]', cat, value) or string.format('[[%s]]', value)
end
else
cat = confCat.fine_assente
ret = value or '?'
end
return ret, cat
end
 
-- =============================================================================
-- Genera il contenuto della riga "Periodo di attività musicale" e la relativa categorizzazione,
-- Funzioni esportate
-- in base ai parametri "tipo artista", "anno inizio attività N", "anno fine attività N", "note periodo attività N"
-- =============================================================================
function p._main(args)
 
local p = {}
 
-- Funzione per {{#invoke:Periodo attività musicale|main}}
function p.main(frame)
local args = getArgs(frame, { parentOnly = false })
local formattedIntervals, categories = {}, {}
local ns0 = mw.title.getCurrentTitle().namespace == 0
local sepCat = args.debug and '<br />' or ''
local singleYear = false
 
-- categorie configurate per il 'tipo artista' richiesto
local-- tipo_artistacategorie =configurate mw.ustring.lower(args['per il "tipo artista'])" richiesto
local tipo_artista = args['tipo artista'] and mw.ustring.lower(args['tipo artista']) or nil
local confCat = cfg.categorie[cfg.alias[tipo_artista] or tipo_artista] or {}
local confCat = cfg.categorie[cfg.alias[tipo_artista] or tipo_artista] or cfg.categorie.default
 
-- parsifica gli intervalli richiesti
Line 103 ⟶ 117:
start, cat = formatStartYear(interval.start, confCat)
table.insert(categories, cat)
elseif tonumber(interval.start) or isDecade(interval.start) then
start = string.format('[[%s]]', interval.start)
elseif interval.start then
start = string.format('<span class="error">anno inizio attività %s non valido</span>', idx)
table.insert(categories, errorCategory)
else
start = '?'
start = interval.start and string.format('[[%s]]', interval.start) or '?'
end
 
if idx == #intervals then
ending, cat = formatEndYear(interval.ending, confCat)
table.insert(categories, cat)
elseif tonumber(interval.ending) or isDecade(interval.ending) then
ending = string.format('[[%s]]', interval.ending)
elseif interval.ending then
ending = string.format('<span class="error">anno fine attività %s non valido</span>', idx)
table.insert(categories, errorCategory)
else
ending = '?'
ending = interval.ending and string.format('[[%s]]', interval.ending) or nil
end
 
formattedInterval = ending and string.format('%s&nbsp;–&nbsp;%s', start, ending) or start
-- se sono più di uno e inizio=fine genera un solo link
if #intervals > 1 and interval.start == interval.ending then
formattedInterval = idx == 1 and start or ending
singleYear = true
else
formattedInterval = string.format('%s&nbsp;–&nbsp;%s', start, ending)
end
 
if interval.note then
local fmt = interval.note:sub(1, 1) == string.char(127) and
Line 121 ⟶ 154:
end
 
if #intervals > 1 andthen
if confCat.intermedi then
table.insert(categories, confCat.intermedi)
end
if singleYear then
table.insert(categories, trackingCategory)
end
end
 
for i = 1, #categories do
categories[i] = string.format('[[%ssCategoria:%s]]', args.debug and ':' or '', categories[i])
end
 
return table.concat(formattedIntervals, '<br />') .. sepCat ..
((ns0 or args.debug) and table.concat(categories, sepCat) or '')
end
 
-- Funzione per {{#invoke:Periodo attività musicale|main}}
function p.main(frame)
return select(2, xpcall(function()
return p._main(getArgs(frame, { parentOnly = false }))
end, errhandler))
end
 
-- Funzione temporanea per la transizione graduale al modulo
function p.valid_args(frame)
local args = getArgs(frame, { parentOnly = false })
if args['tipo artista'] then
if args['anno inizio attività 2'] or
(not args['periodi attività intermedi'] and
(not args['anno inizio attività'] or tonumber(args['anno inizio attività'])) and
(not args['anno fine attività'] or tonumber(args['anno fine attività']) or args['anno fine attività'] == 'in attività')) then
return 1
end
end
end