Modulo:Sandbox/Nastoshka

Da Wikivoyage.

La documentazione per questo modulo può essere creata in Modulo:Sandbox/Nastoshka/man

local p = {}

function pagesInCat(cat,type) 
	-- type può essere 'all', 'pages', 'files', 'subcats'
	return mw.site.stats.pagesInCategory(cat, type)
end


function p.render(frame)
    local cat = frame.args.cat or ''
    local title = frame.args.title or ''
    local mouseover = frame.args.mouseover or ''
    local template = frame.args.template or ''
    local abbrInfo = frame.args.abbr or 'info'

    -- Numero di pagine nella categoria (solo pagine, non sottocategorie o files)
    local pagesInCategory = pagesInCat(cat, 'pages')
	local iconFile = (pagesInCategory == 0) and 'Circle 33c748.svg' or 'Rea mark.svg'
    
    local output = mw.html.create()

    -- Creo la prima cella (titolo)
    local iconCellContent = string.format('&nbsp;<sup>[[File:%s|5px|link=|alt=]]&nbsp;&nbsp;</sup>', iconFile)
    
    -- Tooltip sul titolo
    if mouseover ~= '' then
        iconCellContent = iconCellContent .. string.format('<abbr title="%s">%s</abbr>', mouseover, title)
    else
        iconCellContent = iconCellContent .. title
    end
    
    -- Creo la seconda cella (numero pagine nella categoria)
    local infoCellContent = string.format('[[:Categoria:%s|%d]]&nbsp;', cat, pagesInCategory)

    -- Aggiungo tooltip con info sul template, se necessario
    if template ~= '' then
        infoCellContent = infoCellContent .. string.format('<small><sup>[[:Template:%s|<abbr title="Questa categoria è popolata dal template {{%s}}.">%s</abbr>]]</sup></small>&nbsp;', template, template, abbrInfo)
    end

    -- Costruisco l'output
    output
        :tag('tr')
            :tag('td')
            	:attr('style', 'text-align:left;')
                :wikitext(iconCellContent)
                :done()
            :tag('td')
                :attr('style', 'text-align:right;')
                :wikitext(infoCellContent)

    return tostring(output)
end

return p