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 error_category = 'Errori di compilazione del template Fumetto e animazione'
Riga 33:
end
-- Restituisce
local function extract_values(args, base_name)
local index = 0
return function()
local value = args[base_name .. ' ' .. tostring(index)]
end
return value
end
end
-- 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(
return self
Riga 160 ⟶ 185:
-- Avvia la categorizzazione automatica dell'opera
function CategoryManager:_addCategories(
if self.genre_table_name then
self:
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 == '
self:
else
value = delink(value)
if table_name == 'paese' then
self:_addCategoryByCountry(value, gender, callback)
else
if
value = callback(self, value)
end
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(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
function CategoryManager:_getGenreTableName()
local table_name
if self.media:inArray('anime', 'manga') then
table_name = 'genere_animanga'
elseif self.media.tipo == 'light novel' then
table_name = 'genere_lightnovel'
elseif self.media:inArray('fumetto', 'manhua', 'manhwa') then
table_name = 'genere_fumetto'
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 ''
if year then
return year
else
self.errory_category = true
end
end
Riga 352 ⟶ 420:
local p = {}
-- Funzione per il template FeA, categorizza
function p.categorie(frame)
if mw.title.getCurrentTitle().namespace ~= 0 then return end
Riga 358 ⟶ 426:
end
-- Funzione per il template FeA, elabora
function p.generi(frame)
local ret = {}
table.insert(ret, genre .. tail)
end
end
-- Funzione per il manuale,
-- una data tabella nella configurazione
function p.tabella_configurazione(frame)
local
local reverse_alias = build_reverse_alias(table_name)
local root = mw.html.create('table')
|