Modulo:Fumetto e animazione/sandbox: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m -
riscrivo gestione generi
Riga 3:
le funzioni di categorizzazione automatica
]]
 
local cfg = mw.loadData("Modulo:Fumetto e animazione/Configurazione/sandbox")
local getArgs = require('Module:Arguments').getArgs
local error_category = 'Errori di compilazione del template Fumetto e animazione'
 
Riga 33:
end
 
-- Restituisce listeun numero variabile di valori uno alla volta
local function extract_values(args, base_name)
local index = 0
return function()
whileindex true= doindex + 1
local value = args[base_name .. ' ' .. tostring(index)]
index = index + 1
localif not value =and args[base_nameindex ..== '1 ' .. tostring(index)]then
if not value and index == 1 thenargs[base_name]
end
value = args[base_name]
return value
end
end
if not value then break end
end
return delink(value)
 
-- Restituisce un numero variabile di generi uno alla volta
local function extract_genres(value, table_name)
local index = 0
local substrings = mw.text.split(value, ' *, *')
return function()
index = index + 1
if not substrings[index] then return end
local genre
local link, tail = substrings[index]:match('^(%[%[.-%]%]%a*)(.*)$')
if link then
genre = link:gsub('^%[%[', ''):gsub('^[^%[%]%|]+%|', ''):gsub('%]%]', '')
else
genre, tail = substrings[index]:match('^([%w%(%) ]+[%w%(%)])( *.*)$')
end
if genre then
genre = mw.text.trim(genre:lower())
genre = cfg[table_name][cfg.alias_genere[genre]] or cfg[table_name][genre]
end
if genre then
return genre, tail
else
return substrings[index], ''
end
end
Riga 146 ⟶ 169:
self.media = Media:new(self.args)
self.year = self:_getYear()
self.genre_table_name = self:_getGenreTableName()
self.error_category = false
self.categories = {}
self.tables_matched = {}
 
self:_addCategories(self.args.categorie == 'no')
 
return self
Riga 160 ⟶ 185:
 
-- Avvia la categorizzazione automatica dell'opera
function CategoryManager:_addCategories(error_only)
if self.genre_table_name then
if (self.args['data inizio'] or self.args['data fine']) and not self.year then
self:_addCategory_addCategoriesBy(error_category'genere')
end
 
if error_only or self.media.name == nil then return end
 
if self.media.name == "serie televisive d'animazione" then
Riga 228 ⟶ 251:
self:_addCategory(self.media.name)
-- categorizzazione delle opere suddivise per anno
elseif self.media.name and self.year then
local media = self.media.sottotipo ~= 'serie TV' and
self.media.alias or self.media.name
self:_addCategory(string.format('%s del %s', media, self.year))
end
 
if self.error_category == true then
self:_addCategory(error_category)
end
end
Riga 240 ⟶ 267:
local base_name = table_name:match('[^_]+')
for value in extract_values(self.args, base_name) do
if table_name == 'paesegenere' then
self:_getCategoryByCountry_addCategoryByGenre(value, gender, callback)
else
value = delink(value)
if callback then
if table_name == 'paese' then
value = callback(self, value)
self:_addCategoryByCountry(value, gender, callback)
end
else
local category = self:_getCategory(value, table_name)
if categorycallback then
value = callback(self, value)
self.tables_matched[table_name] = true
end
self:_addCategory(category)
local category = self:_getCategory(value, table_name)
if category then
self.tables_matched[table_name] = true
self:_addCategory(category)
end
end
end
Riga 259 ⟶ 291:
-- Formatta e aggiunge una categoria
function CategoryManager:_addCategory(category)
table.insert(if self.args.categories, string.format(~= '[[Categoria:%s]]no', or category)) == error_category then
table.insert(self.categories, string.format('[[Categoria:%s]]', category))
end
return self
end
 
-- Aggiunge la categoria per Paese dopo aver individuato l'aggettivo
-- maschile o femminile, singolare o plurale adeguato
function CategoryManager:_addCategoryByCountry(country, gender, callback)
local frame = mw.getCurrentFrame()
local success, adj = pcall(frame.expandTemplate, frame, {
title = 'Template:AggNaz/' .. delink(country),
args = { gender }
})
if success then
callback(self, adj)
else
self.error_category = true
end
end
 
-- Aggiunge una o più categorie per genere dell'opera
function CategoryManager:_addCategoryByGenre(value)
for categories in extract_genres(value, self.genre_table_name) do
if type(categories) == 'table' then
for _, category in ipairs(categories) do
self:_addCategory(category)
end
else
self.error_category = true
end
end
end
 
Riga 304 ⟶ 366:
end
 
-- Individua la tabella dei generi idonei al tipo di opera nella configurazione
-- Restituisce la categoria per Paese dopo aver individuato l'aggettivo
function CategoryManager:_getGenreTableName()
-- maschile o femminile, singolare o plurale adeguato
local table_name
function CategoryManager:_getCategoryByCountry(country, gender, callback)
if self.media:inArray('anime', 'manga') then
local frame = mw.getCurrentFrame()
table_name = 'genere_animanga'
local success, adj = pcall(frame.expandTemplate, frame, {
elseif self.media.tipo == 'light novel' then
title = 'Template:AggNaz/' .. country,
table_name = 'genere_lightnovel'
args = { gender }
elseif self.media:inArray('fumetto', 'manhua', 'manhwa') then
})
table_name = 'genere_fumetto'
if success then
return callback(self, adj)
end
return table_name
end
 
-- Ricava l'anno dell'opera dalle date di inizio e fine
function CategoryManager:_getYear()
if not (self.args['data inizio'] or self.args['data fine']) then return end
local start_date = self.args['data inizio'] or ''
local end_date = self.args['data fine'] or ''
returnlocal year = (start_date .. end_date):match('%d%d%d%d')
if year then
return year
else
self.errory_category = true
end
end
 
Riga 352 ⟶ 420:
local p = {}
 
-- Funzione per il template FeA, categorizza trannela che per generevoce
function p.categorie(frame)
if mw.title.getCurrentTitle().namespace ~= 0 then return end
Riga 358 ⟶ 426:
end
 
-- Funzione per il template FeA, elabora categorie ei wikilink dei generi
function p.generi(frame)
local ret = {}
local args = getArgs(frame, {parentOnly = not frame.args[1]})
localfor genre, =tail in extract_genres(frame.args[1], 'genere_wikilink') do
table.insert(ret, genre .. tail)
if genre == '' then return end
local current_page = mw.title.getCurrentTitle()
local current_namespace = current_page.namespace
local category
local result = {}
for name, alias_name in pairs(cfg['alias_genere']) do
local piped = mw.ustring.match(genre, '%[%[[^%[]*|%s*' .. name .. '%s*%]%]')
local pattern = (piped and '%[%[[^%[]*|%s*' or '%[%[%s*') .. name .. '%s*%]%]%a*'
genre = mw.ustring.gsub( genre, pattern, '[[' .. alias_name .. ']]' )
end
for name, correct_name in pairs(cfg['genere']) do
if current_namespace == 0 and args.categorie ~= 'no' then
local tipo = {
['anime'] = correct_name[1],
['fumetto'] = correct_name[2],
['light novel'] = correct_name[3],
['manga'] = correct_name[1],
['manhua'] = correct_name[2],
['manhwa'] = correct_name[2]
}
tipo = tipo[args.tipo]
tipo = tipo and tipo[1] and tipo[1] .. ']][[Categoria:' .. tipo[2] or tipo
category = tipo and tipo ~= '' and '[[Categoria:' .. tipo .. ']]'
end
local piped = mw.ustring.match(genre, '%[%[[^%[]*|%s*' .. name .. '%s*%]%]')
local pattern = (piped and '%[%[[^%[]*|%s*' or '%[%[%s*') .. name .. '%s*%]%]%a*'
genre = mw.ustring.gsub( genre, pattern, correct_name[4] .. (category or '') )
-- Categoria d'errore in caso di genere senza corrispondenza
local list = mw.text.split( genre, ',' )
for i = 1, #list do
local s = replace_braces(list[i])
local p = replace_braces(correct_name[4])
if mw.ustring.match( s, p ) then
result[i] = ''
elseif result[i] == nil and current_namespace == 0 then
result[i] = '[[Categoria:' .. error_category .. ']]'
end
end
end
genre = genre ..return table.concat(resultret, ', ')
return genre
end
 
-- Funzione per il manuale, restituiscemostra lal'elenco configurazionedi per aziendevalori e targetrispettivi alias per
-- una data tabella nella configurazione
function p.tabella_configurazione(frame)
local argstable_name = getArgs(frame).args[1]
localif not table_name or table_name == '' then return args[1]end
if not(table_name) then return '' end
local reverse_alias = build_reverse_alias(table_name)
local root = mw.html.create('table')