Modulo:Libro/sandbox

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca
local getArgs = require('Module:Arguments').getArgs
local numero_romano = require('Module:Numero romano').main
local p = {}

local tab_genere = {
    ["[[romanzo]]"] = "Romanzi",
    ["romanzo"] = "Romanzi",
    ["[[racconto]]"] = "Racconti",
    ["racconti"] = "Racconti",
    ["[[racconti]]"] = "Racconti",
    ["[[racconto|racconti]]"] = "Racconti",
    ["racconto"] = "Racconti",
    ["[[trattato]]"] = "Saggi",
    ["[[trattato (opera)|trattato]]"] = "Saggi",
    ["trattato"] = "Saggi",
    ["[[saggistica]]"] = "Saggi",
    ["saggistica"] = "Saggi",
    ["[[saggi]]"] = "Saggi",
    ["saggi"] = "Saggi",
    ["[[saggio]]"] = "Saggi",
    ["saggio"] = "Saggi"
}


local function get_genere(args)
    local gen_txt = args.genere or ''
    gen_txt = mw.ustring.lower(gen_txt)
    return tab_genere[gen_txt] or "Opere letterarie"
end

local function get_categories(year, args)

    if year_number then
        if year_number < 1 then
            return "Categoria:Scheda libro con anno di prima pubblicazione non riconosciuto| numero negativo"
        elseif year_number > 1800 then
            if year_number >= os.date("*t").year + 3 then
                return "Categoria:Scheda libro con anno di prima pubblicazione non riconosciuto| opera letteraria futura"
            else
                return mw.ustring.format("Categoria:%s del %s", get_genere(args), year)
            end
        else -- secoli dopo Cristo
            local century = numero_romano({math.floor( (year+99)/100 )})
            local preposizione = "del "
            if century == "VIII" or century == "XI" then
                preposizione = "dell'"
            end
            return mw.ustring.format("Categoria:%s %s%s secolo", get_genere(args), preposizione, century)
        end
    end
    return "Categoria:Scheda libro con anno di prima pubblicazione non riconosciuto"
end

function p.categories(frame)
    local args = getArgs(frame)
    local current_namespace =  mw.title.getCurrentTitle().namespace
    local cat = ''
    local force_year = args.forza_cat_anno
    local period = args.periodo
    local year = args.annoorig
    -- disattiva la categorizzazione automatica fuori da ns0
    if current_namespace ~= 0 and not args.debug then
        return period or year or ''
    end
    local wrap_category = "[[%s]]"
    if args.debug == "s" then
        wrap_category = ' [[:%s]]'
    end
    -- non definito ne periodo, ne annoorig
    if not (period or year) then
        return mw.ustring.format(wrap_category, "Categoria:Scheda libro con anno di prima pubblicazione mancante")
    end
    -- definito forza_cat_anno
    if force_year then
        -- disattivo la categorizzazione automatica se force_year = no
        if force_year:lower() == 'no' then
            return period or year
        end
        local cat = get_categories(force_year, args)
        return (period or year) .. mw.ustring.format(wrap_category, cat)
    end
    -- definito periodo
    if period then
        return period
    end
    local year_number = tonumber(year)
    if not(year_number) then
    	_,_,year = mw.ustring.find(year, "^%[%[%d+%]%]$", "%1")
        if year == nil then 
        	return year .. mw.ustring.format(wrap_category, "Categoria:Scheda libro con anno di prima pubblicazione non riconosciuto")       
    	end
    	year_number = tonumber(year)
    end
    local cat = get_categories(year_number, args)
    return "[[" .. year .. "]]" .. mw.ustring.format(wrap_category, cat)
end

return p