Saltar para o conteúdo

Módulo:RawImage

Wikisource, a biblioteca livre
Documentação do módulo[ver] [editar] [histórico] [purgar]

Implementa a predefinição {{raw image}}. Consulte a documentação da predefinição para instruções de uso.

--[[
rawimage
 
Main entry point for Lua function to replace {{raw image}}
 
Usage:
    To invoke the module directly, use
        {{ #Invoke:RawImage | rawimage | pagename }}
    But generally it is preferable to go through the template:
        {{ raw image | pagename }}
     
    'pagename' should be the name of a page in Page: namespace, with or without the "Page:" in front.
    
    
NOTE FOR EDITORS: rawimage displays images centred in the user's standard thumb size.
It is not recommended to offer further display options, as this would disincentivise
the replacement of raw images.
]]

RawImage = {};

function RawImage.rawimage(frame)
    inpage = mw.title.getCurrentTitle():inNamespace("Página")

    pagename = frame.args["pagename"]
    
    -- check if the pagename hasn't been passed
    if pagename == nil then
        return '<span style="color:red;">\'\'\'Error: invoke with {{raw image|' .. mw.title.getCurrentTitle().text .. '}}\'\'\'</span>'
    end

    if pagename == '' then
        return '<span style="color:red;">\'\'\'Error: invoke with {{raw image|' .. mw.title.getCurrentTitle().text .. '}}\'\'\'</span>'
    end

    -- check if the pagename has been given with "Page:" in front. If so, strip it
    if pagename:sub(1,5) == 'Página:' then
        pagename = pagename:sub(6)
    end

    -- find the last / in the pagename
    slash = string.reverse(pagename):find('/')
    if slash ~= nil then
        slash = #pagename - slash + 1
    end
    
    if slash == nil then
        -- there is no slash, this page corresponds to a single-page image
        if inpage then
            category = '!Páginas com imagens brutas'
        else
            category = 'Textos com imagens brutas'
        end
        return '[[File:' .. pagename .. '|frameless|center|360px]][[Category:' .. category .. ']]'
    else
        -- this page title contains a slash, so compose the name of the hi-res file.
        pagebase = pagename:sub(0,slash-1)
        pagenum = pagename:sub(slash+1)
        hiRes = 'File:' .. pagebase .. '-' .. pagenum .. '.png'
        
        -- check if the hi-ref version exists
        if mw.title.new(hiRes).exists then
            -- hi-res version exists, let's link to it
            if inpage then
                category = '!Páginas com imagens brutas (alta resolução disponível)'
            else
                category = 'Textos com imagens brutas (alta resolução disponível)'
            end
            
            return frame:expandTemplate{title='Bloco centro',args={'[[' .. hiRes .. '|frameless|center|360px]]' .. frame:expandTemplate{title='Alinhado à direita', args={frame:expandTemplate{title='x-smaller block', args={'([[:' .. hiRes .. '|Improve this image]])'}}}}}} .. '[[Categoria:' .. category .. ']]'
        else
            -- hi-res version doesn't exists, let's link to source page
            if inpage then
                category = '!Páginas com imagens brutas'
            else
                category = '!Textos com imagens brutas'
            end

            return frame:expandTemplate{title='Bloco centro',args={'[[Arquivo:' .. pagebase .. '|página=' .. pagenum .. '|frameless|center|360px]]' .. frame:expandTemplate{title='Alinhado à direita', args={frame:expandTemplate{title='x-smaller block', args={'(Upload an image to replace this placeholder.)'}}}}}} .. '[[Categoria:' .. category .. ']]'
        end
    end
end

return RawImage