Saltar para o conteúdo

Módulo:Barra de portal

Origem: Wikipédia, a enciclopédia livre.

-- This module implements {{barra de portal}} {{portal3}}.

require('strict')

local portalModule = require('Módulo:Portal')
local getImageName = portalModule.image
local checkPortals = portalModule._checkPortals
local processPortalArgs = portalModule._processPortalArgs
local yesno = require( 'Módulo:Yesno' )
local getArgs = require('Módulo:Arguments').getArgs
local p = {}

-- these 3 functions were imported from de main portal module
-- so we can print link and portal name
local function matchImagePage(s)
    -- Finds the appropriate image subpage given a lower-case
    -- portal name plus the first letter of that portal name.
    if type(s) ~= 'string' or #s < 1 then return end
    local firstLetter = mw.ustring.sub(s, 1, 1)
    local imagePage, banners
    if mw.ustring.find(firstLetter, '^[a-z]') then
        imagePage = 'Module:Portal/images/' .. firstLetter
    else
        imagePage = 'Module:Portal/images/other'
    end
    local banners = mw.loadData ( imagePage )
        if type(banners[s]) == 'string' or #s < 1 then
       return -- not found
        end
    return banners [s]
end

local function getAlias(s)
    -- Gets an alias from the image alias data page.
    local aliasData = mw.loadData('Module:Portal/images/aliases')
    for portal, aliases in pairs(aliasData) do
        for _, alias in ipairs(aliases) do
            if alias == s then
                return portal
            end
        end
    end
end

local function getImageName1(s)
    -- Gets the image name for a given string.
    local default = {}
    default['imagem'] = 'Portal-puzzle.svg|link=|alt='
        default['link'] = 'Wikipédia'
        default['nome'] = 'Portal da Wikipédia'
    if type(s) ~= 'string' or #s < 1 then
        return default
    end
    s = mw.ustring.lower(s)
    return matchImagePage(s) or matchImagePage(getAlias(s)) or default
end

-- Builds the portal bar used by {{portal bar}}.
function p._main( portals, args )
    
    -- check for sensible args
    args = type(args) == "table" and args or {}
    
    -- Normalize arguments
    for key, default in pairs({border=true,borda=true,redlinks=false,tracking=true}) do
        if args[key] == nil then args[key] = default end
        args[key] = yesno(args[key], default)
    end
    local border =  args.border or args.borda
    local nav = mw.html.create( 'div' )
        :addClass( 'noprint metadata noviewer' )
        :attr( 'role', 'navigation' )
        :attr( 'aria-label' , 'Portals' )
        :css( 'background-color', '#F9F9F9' )
        :css( 'text-align', 'center' )
        :css( 'margin-top', '10px' )
        :css( 'margin-left', '0' )
        :css( 'clear', 'both' )
    if yesno( border ) == false then
        nav
            :css( 'padding', '0.3em 1.7em 0.1em' )
    else
        nav
            :css( 'border-top', 'solid silver 1px' )
            :css( 'border-bottom', 'solid silver 1px' )
            :css( 'padding', '3px' )
    end
    
    local trackingCat = ''
    -- Allow any number of portals
    args.minPortals = 0
    args.maxPortals = -1
    -- Check to see whether there are redlinks, filter out unless args.redlink is true
    portals, trackingCat = checkPortals(portals, args)
    nav:wikitext(trackingCat)
    if #portals == 0 then
        return trackingCat
    end

    local list = mw.html.create( 'ul' )
        :css( 'margin', '0.1em 0 0' )
    for _, portal in ipairs( portals ) do
        local banners = getImageName1(portal)
        local image = banners['imagem']
        local link = banners['link']
        local name = banners['nome']
        list
            :tag( 'li' )
                :css( 'display', 'inline' )
                :tag( 'span' ) -- Inline-block on inner span for IE6-7 compatibility.
                    :css( 'display', 'inline-block' )
                    :css( 'white-space', 'nowrap' )
                    :tag( 'span' )
                    :css( 'margin', 'auto 1.5em' )
                    :tag( 'span' )
                    :css( 'margin-right', '0.5em' )
                    :wikitext( string.format(
                        '[[File:%s|25x30px|alt=]]', image ) )
                    :done()
                    :done()
                    :tag( 'span' )
                    :css( 'font-weight', 'bold' )
                    :wikitext( string.format('[[Portal:%s|%s]]', link, name))
                    :done()
    end
    nav
        :node( list )
    
    return tostring( nav )
end

-- Processes external arguments and sends them to the other functions.
function p.main( frame )
    local origArgs = getArgs(frame)
    local portals, args = processPortalArgs(origArgs)
    return p._main( portals, args )
end

return p