Modulo:Fumetto e animazione/sandbox: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
+ |
m Bot: rimuovo no globals obsoleto |
||
(49 versioni intermedie di un altro utente non mostrate) | |||
Riga 3:
le funzioni di categorizzazione automatica
]]
require('strict')
local cfg = mw.loadData("Modulo:Fumetto e animazione/Configurazione/sandbox")
local error_category = 'Errori di compilazione del template Fumetto e animazione'
Riga 11 ⟶ 13:
-- =============================================================================
-- Converte la lettera iniziale di una stringa in maiuscola
local function
return str:gsub('^%l', string.upper)
end
-- Restituisce un valore senza eventuale wikilink a inizio stringa
local function delink(value)
if not value then return end
local pattern
if value:find('|') then
Riga 29 ⟶ 27:
pattern = "^'*%[%[ *([^%[%]]-) *%]%]"
end
return ucfirst(value:match(pattern) or value)
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
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())
end
return cfg.alias_genere[genre] or genre, tail, substrings[index]
end
end
-- Parsifica i parametri rimuovendo le stringhe vuote
local function parse_args(args)
local ret = {}
for key, value in pairs(args) do
if end
end
return
end
Riga 84 ⟶ 87:
setmetatable(self, { __index = Media })
self.
self.
self.sottotipo = self:_getSubtype()
self.name = self:_getName()
self.alias = self:_getAlias()
Riga 110 ⟶ 114:
function Media:_getName()
return self:_getValue('tipo', 'sottotipo')
end
-- Verifica che il sottotipo di medium sia riconosciuto
function Media:_getSubtype()
if cfg.sottotipo[self.tipo] and cfg.sottotipo[self.tipo][self.args.sottotipo] then
return self.args.sottotipo
end
end
-- Verifica che il tipo di medium sia riconosciuto
function Media:_getType()
if cfg.tipo[self.args.tipo] or cfg.sottotipo[self.args.tipo] then
return self.args.tipo
end
end
-- Cerca il nome o l'alias del medium nella configurazione
function Media:_getValue(
cfg[
cfg[
end
Riga 131 ⟶ 148:
setmetatable(self, { __index = CategoryManager })
self.args =
self.media = Media:new(
self.error_category = false
self.year = self:_getYear()
self.genre_table_name = self:_getGenreTableName()
self.categories = {}
self.tables_matched = {}
self:_addCategories(
return self
Riga 148 ⟶ 167:
-- 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 160 ⟶ 177:
:_addCategoriesBy('paese', 'fp', self._categorizeTVSeriesByCountry)
elseif self.media.name == "film d'animazione direct-to-video" then
self:_addCategoriesBy('paese', 'mp', self._categorizeDTVFilmByCountry)
elseif self.media.name == "webserie d'animazione" then
self:_addCategoriesBy('paese', 'fp', self._categorizeWebseriesByCountry)
Riga 171 ⟶ 186:
:_addCategoriesBy('editore_manga')
:_addCategoriesBy('editore')
:_addCategoriesBy('target'
elseif self.media:inArray('manhua', 'manhwa') then
self:_addCategoriesBy('editore')
Riga 180 ⟶ 195:
:_addCategoriesBy('etichetta')
:_addCategoriesBy('editore')
elseif self.media.tipo == 'fumetto' and self.media.
self
:_addCategoriesBy('etichetta_sottotipo', nil, self.
:_addCategoriesBy('etichetta')
:_addCategoriesBy('editore_sottotipo', nil, self.
:_addCategoriesBy('editore')
:_addCategoriesBy('paese', 'mp', self._categorizeComicByCountry)
Riga 199 ⟶ 214:
if self.media:inArray('anime', 'cartone') then
self:_addCategoriesBy('studio')
local broadcaster
local network = self.args['rete 1'] or self.args.rete
if self.media:inArray('film TV', 'serie TV') then
broadcaster = self:_getCategory(network, 'rete')
elseif network and self.media
self.error_category = true
end
if not network and self.media:inArray('film TV', 'ONA', 'serie TV', 'webserie') then
local platform = self.args['streaming 1'] or self.args.streaming
broadcaster = self:_getCategory(platform, 'streaming')
end
if broadcaster then
self:_addCategory(mw.message.newRawMessage(broadcaster, self.media.alias
end
end
-- categorizzazione per tipo delle opere non suddivise per anno
if self.media:inArray('film direct-to-video', 'manhua', 'manhwa', 'ONA', 'webserie') then
self:_addCategory(self.media.name)
-- categorizzazione delle opere suddivise per anno
elseif self.
local media = self.media.
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 226 ⟶ 248:
-- Aggiunge categorie per una lista di valori
function CategoryManager:_addCategoriesBy(table_name, gender, callback)
local base_name = table_name:match('[^_]+')
for value in extract_values(self.args, base_name) do
if table_name == 'genere' then
self:_addCategoryByGenre(value)
elseif table_name == 'paese' then
self:_addCategoryByCountry(value, gender, callback)
elseif self:_isCategoryRedundant(table_name) == false then
if callback then
end
self.tables_matched[table_name] = true
self:_addCategory(category)
end
end
Riga 246 ⟶ 270:
-- Formatta e aggiunge una categoria
function CategoryManager:_addCategory(category)
if self.args.categorie ~= '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 not success then
self.error_category = true
elseif self:_isCategoryRedundant('paese') == false then
callback(self, adj)
end
end
-- Aggiunge una o più categorie per genere dell'opera
function CategoryManager:_addCategoryByGenre(value)
for genre in extract_genres(value) do
if cfg.genere_wikilink[genre] then
local categories = cfg[self.genre_table_name][genre]
if categories then
for _, category in ipairs(categories) do
self:_addCategory(category)
end
end
else
self.error_category = true
end
end
end
Riga 287 ⟶ 344:
-- Cerca il nome di una categoria nella configurazione
function CategoryManager:_getCategory(key, table_name)
key = delink(key)
key = cfg['alias_' .. table_name] and cfg['alias_' .. table_name][key] or key
return cfg[table_name][key]
end
-- Individua la tabella dei generi idonei al tipo di opera nella configurazione
function CategoryManager:
local table_name
if self.media:inArray('anime', 'manga') then
table_name = 'genere_animanga'
elseif self.media.tipo == 'light novel' then
table_name = 'genere_light_novel'
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.error_category = true
end
end
Riga 328 ⟶ 387:
end
--
function CategoryManager:
return
end
Riga 339 ⟶ 398:
local p = {}
-- Funzione per il template FeA, categorizza
function p.categorie(frame)
if mw.title.getCurrentTitle().namespace ~= 0 then return end
return CategoryManager:new(args):listCategories()
end
-- Funzione per il template FeA, elabora
function p.generi(frame)
local ret = {}
local
local wikilink = cfg.genere_wikilink[genre]
table.insert(ret, wikilink and (wikilink .. tail) or default)
end
return table.concat(ret, ', ')
end
-- Funzione per il manuale,
-- una data tabella nella configurazione
function p.tabella_configurazione(frame)
local
if not cfg[table_name] then return end
local root = mw.html.create('table')
root
:addClass('wikitable
:tag('tr')
:tag('th'):wikitext(
:tag('th'):wikitext('Alias'):done()
:tag('th'):wikitext('
local keys, reverse_alias = {}, {}
for alias, name in pairs(cfg['alias_' .. alias_table_name] or {}) do
if not reverse_alias[name] then reverse_alias[name] = {} end
table.insert(reverse_alias[name], alias)
end
for key in pairs(cfg[table_name]) do table.insert(keys, key) end
table.sort(keys)
local pre_table_name
if table_name == 'editore_sottotipo' or table_name == 'etichetta_sottotipo' then
pre_table_name = cfg.sottotipo.fumetto
elseif table_name == 'rete' or table_name == 'streaming' then
pre_table_name = cfg.alias_sottotipo.cartone
end
for _, key in ipairs(keys) do
local result, values = {}, {}
reverse_alias[key] = reverse_alias[key] or {}
if pre_table_name then
for _, subtype in pairs(pre_table_name) do
local msg = cfg[table_name][key]
subtype = ucfirst(subtype)
table.insert(values, mw.message.newRawMessage(msg, subtype):plain())
end
elseif type(cfg[table_name][key]) == 'string' then
values = { cfg[table_name][key] }
else
values = cfg[table_name][key]
end
for _, str in ipairs(values) do
if
str = string.format('[[:Categoria:%s]]', str)
end
end
table.sort(result)
result = table.concat(result, '<br />')
table.sort(reverse_alias[key])
reverse_alias[key] = table.concat(reverse_alias[key], '<br />')
root:tag('tr')
:tag('td'):wikitext(
:tag('td'):wikitext(
:tag('td'):wikitext(
end
return tostring(root)
|