Saltar para o conteúdo

Módulo:Portal-inline

Permanently protected module
Origem: Wikipédia, a enciclopédia livre.

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local portalModule = require('Module:Portal')
local getImageName = portalModule.image
local checkPortals = portalModule._checkPortals
local processPortalArgs = portalModule._processPortalArgs

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

-- Function to format error message and tracking category
-- Arguments:
--   errMsg: string, or nil/false if no error
--   trackingCat: string for tracking category (or empty string)
local function formatError(errMsg, trackingCat)
    local result = trackingCat or ''
    if errMsg then
        local errTag = mw.html.create('span')
        errTag:addClass("error")
        errTag:css("font-size",'100%')
        errTag:wikitext("Error: "..errMsg)
        result = tostring(errTag)..result
    end
    return result
end

local function image(portal, args)
        local tamanho = args.size or args.tamanho
    local size = tamanho == "tiny" and "16x16px" or "32x28px"
    return string.format('[[File:%s|class=noviewer|%s]]',getImageName{portal}, size)
end

local function link(portal, args)
    local displayName = ""
    local banners = getImageName1(portal)
    --local image = banners['imagem']
    local link = banners['link']
    local name = banners['nome']
    local text = args.text or args.texto
    if not (text == "" or text == nil) then
        displayName = text
    elseif args.short or args.curto then
        displayName = link
    else
        displayName = name
    end
    return string.format('[[Portal:%s|%s]]',link,displayName)
end

function p._main(portals, args)
    mw.logObject(args)
    
    -- Normalize all arguments
    if args.redlinks == 'include' then args.redlinks = true end
    for key, default in pairs({tracking=true,redlinks=false,short=false}) do
        if args[key] == nil then args[key] = default end
        args[key] = yesno(args[key], default)
    end
    
    local trackingCat = ''
    local errMsg = nil
    
    -- Check for existing categories, drop if not.
    -- Possible generate tracking category & error message if needed
    args.minPortals = args.minPortals or 1
    args.maxPortals = args.maxPortals or 1
    portals, trackingCat, errMsg = checkPortals(portals,args)
    -- use more specific tracking cat for inline portal
    --trackingCat = mw.ustring.gsub(trackingCat,"Portal templates","Portal-inline template")
    -- either too many/few portals, or no portals left after filtering, then return
    if errMsg or #portals == 0 then
        return formatError(errMsg, trackingCat)
    end
    return image(portals[1],args)..'&nbsp;'..link(portals[1],args)..(trackingCat or '')
end

function p.main(frame)
    local origArgs = getArgs(frame)
    local portals, args = processPortalArgs(origArgs)
    return p._main(portals, args)
end

return p