文档图示 模块文档[创建]
--[[
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("Page")

    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) == 'Page:' 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 = 'Pages with raw images'
        else
            category = 'Texts with raw images'
        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 = 'Pages with raw images (hi-res scan available)'
            else
                category = 'Texts with raw images'
            end
            
            return frame:expandTemplate{title='block center',args={'[[' .. hiRes .. '|frameless|center|360px]]' .. frame:expandTemplate{title='right', args={frame:expandTemplate{title='x-smaller block', args={'([[:' .. hiRes .. '|Improve this image]])'}}}}}} .. '[[Category:' .. category .. ']]'
        else
            -- hi-res version doesn't exists, let's link to source page
            if inpage then
                category = 'Pages with raw images'
            else
                category = 'Texts with raw images'
            end

            return frame:expandTemplate{title='block center',args={'[[File:' .. pagebase .. '|page=' .. pagenum .. '|frameless|center|360px]]' .. frame:expandTemplate{title='right', args={frame:expandTemplate{title='x-smaller block', args={'(Upload an image to replace this placeholder.)'}}}}}} .. '[[Category:' .. category .. ']]'
        end
    end
end

return RawImage